Declarative Routes for NextJS and React-Router: DRY Routing

2024 ж. 21 Мам.
25 252 Рет қаралды

Why settle for hard-coded link strings and URLs for fetch calls? Let's build a declarative routing system on top of NextJS's router (and the React-Router), to give us write once and never change routing.
Code: github.com/jherr/nextjs-decla...
NPM: www.npmjs.com/package/declara...
👉 Upcoming NextJS course: pronextjs.dev
👉 Don't forget to subscribe to this channel for more updates: bit.ly/2E7drfJ
👉 Discord server signup: / discord
👉 VS Code theme and font? Night Wolf [black] and Operator Mono
👉 Terminal Theme and font? oh-my-posh with powerlevel10k_rainbow and SpaceMono NF
00:00 Introduction
1:50 Example Application
3:13 Declarative Routing File System
4:11 Declarative Routing Indexing and Layout
5:39 Handling Query Strings
7:03 Builder Features
10:03 Search Routes
11:53 Outroduction

Пікірлер
  • You are the tech consultant of my company. 🤣

    @WilliamShrek@WilliamShrekАй бұрын
  • I did not realise how much react routing sucked until I watched this video, I have to try this immediately. Calling out that you can incrementally adopt is what really sold me 🔥

    @josiahayres737@josiahayres737Ай бұрын
    • This is not React, it's Next. If you are using just React try Tanstack Router.

      @airixxxx@airixxxxАй бұрын
  • Very elegant solution. Simple yet powerful.

    @jazsouf@jazsoufАй бұрын
  • As usual, every video from you is awesome, I become a better developer every week thank to your channel, I really appreciate your work.

    @rogerscript@rogerscript29 күн бұрын
  • Awesome video, Jack. Thank you!

    @mattwooddc@mattwooddcАй бұрын
  • Awesome, looking forward to digging in...

    @VincentFulco@VincentFulcoАй бұрын
  • WOooo amazing!! looking forward to check it

    @sebastianlpoliak@sebastianlpoliakАй бұрын
  • This looks great. Thank you!

    @highrankin@highrankinАй бұрын
  • This is a really good idea. Taking this for my projects ❤

    @joseandkris@joseandkrisАй бұрын
  • Really nice solution, I will give it a try! Thank you, great work here 👍

    @elldy001@elldy001Ай бұрын
  • Awsome!! I need this in my app. I am done writing links everywhere.

    @WilliamShrek@WilliamShrekАй бұрын
  • Nice. I'm surprised this doesn't exist already

    @alexleung842@alexleung842Ай бұрын
    • Right?!?

      @jherr@jherrАй бұрын
  • top tier content. who even needs trpc or tanstack router anymore

    @jackn@jacknАй бұрын
    • is that a joke?

      @vty4261@vty4261Ай бұрын
    • ​@@vty4261no. TS has enough features on its own to be a type-safety contract generator. both trpc and ts-router are basically just doing the same thing with more complexity

      @jackn@jacknАй бұрын
    • @@vty4261it definitely is cos wtf? Did he really said Tanstack for what?

      @Osirisdigitalagency@OsirisdigitalagencyАй бұрын
    • Wtf you mean??

      @Royalettus@RoyalettusАй бұрын
    • ​@@Osirisdigitalagency it's not a joke, this is basically all tanstack router and trpc are doing is generating typescript contracts. they basically serve no other purpose than what was shown in the video if you can achieve this with plain TS why would you need a library for it

      @jackn@jacknАй бұрын
  • I'll start using this on my app right a way thanks! 🎉

    @Vir7uaLGameplays@Vir7uaLGameplaysАй бұрын
  • I am looking to do the same thing for remix! Awesome video as always!

    @tidb1ts@tidb1tsАй бұрын
    • Feel free to jump in and contribute. There is a React-Router version there already, but it only works with the routes object declaration usage of React Router. So we'd need a version that would work with Remix's file routing. There are just so many variations of router setup out there.

      @jherr@jherrАй бұрын
  • Looks cool, will try it in my next todo app

    @rajthapa9006@rajthapa9006Ай бұрын
  • That’s another perfect video 🎉

    @fullstackcoder1181@fullstackcoder1181Ай бұрын
  • You are a godsend in the community

    @anonypoke7486@anonypoke7486Ай бұрын
  • Great content! 👏👏 thank you! 🙏

    @paweciosek489@paweciosek489Ай бұрын
  • That's actually very interesting

    @panteliselef239@panteliselef239Ай бұрын
  • Really nice! Thanks

    @AdamM@AdamMАй бұрын
  • this is VERY useful 🙌🏼🙌🏼🙌🏼

    @ilkou@ilkouАй бұрын
  • Thank you Jack!

    @raphauy@raphauyАй бұрын
  • This declarative routing really takes any project to the next level! I have a question about the example using getPokemonSearchAPI. Shouldn’t we first validate the backend response using zod’s parse or safeParse to prevent any backend model changes from breaking our application? Thank you!

    @dahermora5976@dahermora5976Ай бұрын
  • I know it’s zoomed but your terminal looks like a pack of m&ms. Nice video btw 🎉

    @lcarv20@lcarv20Ай бұрын
  • Do you have any video with your vscode theme, icons, font, terminal, etc. ?

    @ricardojorgealmeidarocha4880@ricardojorgealmeidarocha4880Ай бұрын
  • Dang this really is cool.

    @xorlop@xorlopАй бұрын
  • Great as always ❤.

    @yogeshvanzara5553@yogeshvanzara5553Ай бұрын
  • Ver nice, this is a real new approach right?! I like it!

    @codewithguillaume@codewithguillaumeАй бұрын
    • Kinda? It's built on top of what Flightcontrol did, but substantially improved. The scanner/builder is new, the whole API wrapping system is new, and the support for other types of routers is new. The React-Router support has some crazy hierarchich TypeScript generics in it that were fun to figure out. I should do a video on that. Arguably the React-Router implementation is even cooler since you can just change an entry in a table and all the routes change automagically. I don't know, or really even think, that this will be the end solution in this space. I just wanted to push the ball forward a little to see where other folks take it.

      @jherr@jherrАй бұрын
  • Amazing 🔥🔥

    @eliuddyn@eliuddynАй бұрын
  • Looking forward to contribute! Which repo is it? Found the demo repo but not the package one 😅

    @fernandosalazar8814@fernandosalazar8814Ай бұрын
  • You are awesome 👌

    @Royalettus@RoyalettusАй бұрын
  • Thanks for this. May I ask how you customized your Shell? Thanks!

    @incarnateTheGreat@incarnateTheGreatАй бұрын
    • It's oh-my-posh on top of oh-my-zsh.

      @jherr@jherrАй бұрын
    • @@jherr Thanks!

      @incarnateTheGreat@incarnateTheGreatАй бұрын
  • This reminds me of the work done by ethan on next-typesafe-url ( cant paste links ) :( ! Super awesome stuff and works pretty well in my projects to solve the same problem :)

    @MrJulzies@MrJulziesАй бұрын
  • I think it is awesome

    @user-tb4ig7qh9b@user-tb4ig7qh9bАй бұрын
  • Ok, this is dope. The one thing that I would've preferred is if all the links were exported from one object. So something like `Link.Home` and `Link.Search` while the APIs would be something like `API.Search` and `API.Edit`, that way everything feels a bit more centralized

    @aveshlutchman8573@aveshlutchman8573Ай бұрын
    • Hmmm... Could be a cool option.

      @jherr@jherrАй бұрын
  • I've always done that by myself on my projects, it's one of the reasons I don't like Next directory based router. In the React standalone days, I would create all the routing from an constant object. If one route needed to change you only had to change that object, with Next you can't do that, you need to change the names and location of folders and in the code as well.

    @airixxxx@airixxxxАй бұрын
  • This is awesome! But one thing I’m thinking is that generally the routing doesn’t need to change often on the app, and if it does then imo it’s a bigger issue with the application

    @GrigorMinasyan@GrigorMinasyanАй бұрын
    • In evolved apps I think you are right. In early apps for small teams this could be a real ass-saver.

      @jherr@jherrАй бұрын
  • Awesome! Can you please make a video about Vercel's Generative UI. 🙏

    @jessmondnazarrea1142@jessmondnazarrea1142Ай бұрын
  • Any vidoe or course on how we can build such thing it doesn't have to be a strong as the original library... That can be the ultimate senior front end video ever posted on KZhead Whag a flex having such project sitting on my portfolio. Hope my comment get noticed by the author.

    @djibrilm__-@djibrilm__-Ай бұрын
  • This is pretty cool! How do you handle error handling with API routes?

    @ThEldeRS@ThEldeRSАй бұрын
    • It's a promise, you can do a catch on it to catch the errors.

      @jherr@jherrАй бұрын
  • Hi Jack, great tutorial, one thing I noticed the package doesn't take into account is folder groups. I have grouped features of the app under folder like `(marketing)` or `(users)` under app folder and the routes created are `/(marketing)/(home)` even though it should be `/` - so perhaps ignoring the folder groups would be a great addition to the package

    @hassan.tl94@hassan.tl94Ай бұрын
    • Feel free to contribute that. Or add an issue and someone else might pick that up. I'll definitely be working on this more, but at the moment the course is my daily top priority.

      @jherr@jherrАй бұрын
    • @@jherr yes I can look into that, I couldn't find the repository for declarative routing - can you provide the link?

      @hassan.tl94@hassan.tl94Ай бұрын
    • @@hassan.tl94 github.com/ProNextJS/declarative-routing

      @jherr@jherrАй бұрын
  • That's a really nice system, I was curious to dig into the project's code to better understand it but it seems you don't ship the non uglified code of declarative-router, that's a curious choice..? 🤔

    @xela92@xela92Ай бұрын
    • Here is a link to the repo: github.com/ProNextJS/declarative-routing

      @jherr@jherrАй бұрын
    • @@jherr thanks, totally missed that 😅👍

      @xela92@xela92Ай бұрын
  • what's wrong with a recursive sed -i substitute command? It wil take much longer to figure out what's going on if I find this declarative routing stuff in an old project two years from now... AND to remember to have to run some binary with a specific pnpm version and AAARGH...

    @dontwanttousemyrealnametol6765@dontwanttousemyrealnametol6765Ай бұрын
    • Well, it's not a binary, and all of the source is in your source, but if you don't want to use it that's totally fine by me.

      @jherr@jherrАй бұрын
    • ​@@jherr thanks... sorry I had a stressful day and may have overreacted

      @dontwanttousemyrealnametol6765@dontwanttousemyrealnametol6765Ай бұрын
  • I used to do an object in a config file which contained all routes

    @iamcasted@iamcastedАй бұрын
    • That's how we do it,, has always worked well. This package does neat things, I think it would be useful for taking over maintenance of an app that has all of the links hardcoded. Not sure I'd ever use it in a new project, though.

      @coherentpanda7115@coherentpanda7115Ай бұрын
  • I like it, I will try it in my project, but can it be used on server actions?

    @garasipemula@garasipemulaАй бұрын
    • I don't see why you couldn't use it in a server action, but server actions don't need them since NextJS handle the routing on that automatically.

      @jherr@jherrАй бұрын
    • Oh I get it 🤣

      @garasipemula@garasipemulaАй бұрын
  • He is GOLD 🪙

    @ThEGeEeK@ThEGeEeKАй бұрын
  • Hi Jack, my index.ts is not correctly making the imports because it's using backslashes instead of forward slashes.

    @Vir7uaLGameplays@Vir7uaLGameplaysАй бұрын
    • Are you on windows?

      @jherr@jherrАй бұрын
    • @@jherr yes I am Jack

      @Vir7uaLGameplays@Vir7uaLGameplaysАй бұрын
  • I usually put my routes in variables... then I do just const userRoute = 'user/:id'; and then userRoute.replace(':id', user.id)

    @zanzaraloggan3713@zanzaraloggan3713Ай бұрын
    • Why not create a function that returns the string? const createUserRoute = (id) => `user/${id}`;? Seems more sensible compared to userRoute.replace because you don't have to know what specific string you have to replace, you just pass in arguments.

      @rand0mtv660@rand0mtv660Ай бұрын
  • This is great work. Can it be used with tanstack-router with vite

    @kasujjamuhammed663@kasujjamuhammed663Ай бұрын
    • With Vite you can use React-Router. Tanstack is a little tricker, I would need to work with that team to figure out integration into TSR and their Vite plugin.

      @jherr@jherrАй бұрын
    • @@jherr thanks please

      @kasujjamuhammed663@kasujjamuhammed663Ай бұрын
    • @jherr I will check it out with react-router

      @kasujjamuhammed663@kasujjamuhammed663Ай бұрын
  • Sorry to highjack this video, Jack. I've got a request that I'm sure you're capable of answering. With TRPC and Prisma, do you have a recommendation on how to do multivariant products design, such as size, color, or any other custom variant group? I'm struggling a lot with how to handle it when not having concrete tables such as size, color, etc. I've looked into the EAV design theory, but can't seem to apply it with "pivot tables" using Prisma.

    @dylanstephens1222@dylanstephens1222Ай бұрын
    • Feel free to jump on the Discord server and ask your question there. Please read and follow the #rules before posting, it's an all volunteer support service.

      @jherr@jherrАй бұрын
  • 🔥LGTM

    @mikimaine@mikimaineАй бұрын
  • I prefer to use function that return url So if I need to give this url to custom component with href or to navigate, I can

    @untlsn@untlsnАй бұрын
    • Then just call the function directly and it will return the URL, as shown in the video.

      @jherr@jherrАй бұрын
  • I think something like this already exists for Next.js, IIRC it's called next-typesafe-url. As for pure React, tanstack router is (IMO) a more reliable option.

    @Tian-wi6qr@Tian-wi6qrАй бұрын
    • Interesting, I still have to manage that URL as a string every time I use the link though. Plus from the little video attached to the github page the resultant code to build the href is pretty long. Also, it only handles page routes and not API routes.

      @jherr@jherrАй бұрын
    • @@jherr Fair enough

      @Tian-wi6qr@Tian-wi6qrАй бұрын
  • Where is the repository link for this project?

    @Mong3@Mong3Ай бұрын
    • github.com/ProNextJS/declarative-routing

      @jherr@jherrАй бұрын
  • Links from the "Installation" section of NPM lead to a 404 page.

    @kinofan_org@kinofan_orgАй бұрын
  • What is optimal way to keep main process and watchers from all different tools active without opening 100500 terminals? Can you make video on this topic?

    @user-ik7rp8qz5g@user-ik7rp8qz5gАй бұрын
    • Use the concurrently library.

      @jherr@jherrАй бұрын
  • how about using it with Nx monorepo?

    @danielfrey1359@danielfrey1359Ай бұрын
    • I don't see why that would be a problem, you'd just have to launch it as in the context of the repo. It just runs `pnpm add` (or whatever your package manager is) so hopefully that works. I don't like that NX removes package.jsons.

      @jherr@jherrАй бұрын
    • I am gonna give it a go, keep you posted

      @danielfrey1359@danielfrey1359Ай бұрын
  • if you have to change your routing at a point where you have tons of references you have a problem anyways.. breaking links on the customer side

    @Hacking-Kitten@Hacking-KittenАй бұрын
  • The video is nice, but I don't see the need for the library. Typically, I store the endpoints in a standard JSON file and import them wherever necessary. If I need to change an endpoint, I only update the JSON file, and the changes are reflected everywhere. This keeps the syntax of routing components consistent and avoids confusion for anyone who works on the project later.

    @Anas_Alaqeel@Anas_AlaqeelАй бұрын
  • Who is your favorite pokemon and what makes it special?

    @user-ik7rp8qz5g@user-ik7rp8qz5gАй бұрын
    • Gumshoos. He's terrible and he looks a little like me.

      @jherr@jherrАй бұрын
  • Its failing with NextJS latest version 14.2

    @reeteshvirous1359@reeteshvirous1359Ай бұрын
  • Dr. Build

    @pmioduszewski@pmioduszewski28 күн бұрын
  • Rly you didn't left the link on NPM, why?

    @hasst9261@hasst9261Ай бұрын
    • Good point: www.npmjs.com/package/declarative-routing

      @jherr@jherrАй бұрын
    • @@jherr TY. I meant about description below the video Have a good one!

      @hasst9261@hasst9261Ай бұрын
    • I added it there too ty

      @jherr@jherrАй бұрын
  • I hate this in react, magic strings everywhere.. I just wanted the native router to support route tags so you define route strings once but links can point to route tags.. good alternative but think the main Devs should have done a lot better

    @chriswatts923@chriswatts923Ай бұрын
  • Your course status?

    @avishekdas4947@avishekdas4947Ай бұрын
    • Going really good. Release late this month or early next. I'm really proud of it.

      @jherr@jherrАй бұрын
    • @@jherr That's really exciting!

      @avishekdas4947@avishekdas4947Ай бұрын
  • How can this use dot notation in a server component wtf?

    @SanderCokart@SanderCokartАй бұрын
    • Crap, that's true. I forgot about that.

      @jherr@jherrАй бұрын
    • @@jherr so how is your example not crashing? 😅

      @SanderCokart@SanderCokartАй бұрын
    • @@SanderCokart I've forgotten the constraint. It might fail on build. I'll check it out. Either way, you can fall back on using the string variant. Or... you can change the code so that the component is the primary export, so it would be `` instead. The code is just copied into your repo like shadcn.

      @jherr@jherrАй бұрын
    • @@jherr it should've failed on dev too.

      @SanderCokart@SanderCokartАй бұрын
  • i am missing readme and examples

    @zbynekriha@zbynekrihaАй бұрын
  • The validation is a bit unnecessary given it's used in the same application. Typescript covers most of that use case

    @Joshua-dc4un@Joshua-dc4unАй бұрын
    • Which validation part?

      @jherr@jherrАй бұрын
  • This is cool, but for the love of god , do not add this to your company’s nextjs project. It’s like react devs are plagued with the curse of abstracting everything, on top of that this adds another build step and another dependency to the mammoth that is the node modules folder.

    @engine_man@engine_manАй бұрын
    • Except that this doesn't actually add to the node_modules folder beyond zod and and the URL parser.

      @jherr@jherrАй бұрын
    • @@jherr happy to be proven wrong.

      @engine_man@engine_manАй бұрын
  • Awesome solution Jack, I really appreciate you hard work🫡

    @Its-InderjeetSinghGill@Its-InderjeetSinghGillАй бұрын
KZhead