Server-side mutations with Route Handlers in Next.js
Next.js has introduced a new API Routes feature in the app directory, which allows for server-side mutations with route handlers.
The new API Routes feature in Next.js app directory provides a way to handle server-side mutations in a more organized and structured manner. With route handlers, developers can define custom logic for handling requests and processing data on the server.
By using route handlers, developers can create custom API endpoints that can be accessed from the client-side application. This allows for more flexibility and control over how data is processed and manipulated on the server.
How to use Route Handlers
To use route handlers in a Next.js app, developers can create a new file in the `pages/api` directory and define the API endpoint logic using JavaScript. For example, if you want to create a custom API endpoint for handling user authentication, you can create a file called `auth.js` in the `pages/api` directory and define the authentication logic in that file.
Route handlers can be used to handle various types of server-side mutations, such as user authentication, data processing, and database queries. By organizing API endpoint logic into separate files, developers can maintain a clean and structured codebase, making it easier to manage and maintain server-side code.
Benefits of using Route Handlers
Route Handlers in Next.js app directory offer several benefits for developers:
- Organized and structured codebase: Route handlers allow developers to organize API endpoint logic into separate files, making it easier to manage and maintain server-side code.
- More control over server-side mutations: Route handlers provide a way to define custom logic for handling requests and processing data on the server, giving developers more flexibility and control over how data is manipulated.
- Increased reusability: With route handlers, developers can create reusable API endpoints that can be accessed from multiple client-side applications, reducing the need to duplicate code.
Overall, the new API Routes feature in Next.js app directory provides a powerful way to handle server-side mutations with route handlers. By organizing API endpoint logic into separate files and defining custom logic for handling requests and processing data on the server, developers can create more flexible, reusable, and maintainable server-side code.
🚀 UPDATED VERSION: https://youtube.com/playlist?list=PL5S4mPUpp4OtMhpnp93EFSo42iQ40XjbF 🚀
coming from svelte + pocketbase … this melted my brain! so verbose!
Hello Everyone!
Thank you much for the video, really good guide!!
But I have a question, when you created the Fetch for the API you had to declare the full API URL, localhost:3000/api/…
When I deploy my app on Vercel, will I have to change the URL before sending it? But they have a dynamic url for branches.
How can i keep track of the BASE URL to update the call for the API ?
And if the Port 3000 changes for some reason, do I have to update all my API calls?
Before the APP_DIR you just needed to call '/api/example', why the new version needs the full base URL ??
Thanks for this. I think a full project that implements these new features in a some what real-world example. Loving this content and I need it!
How would you do optimistic updates? Waiting for the new data to make its way to the db and back seems like a bad UX…
I don't know why but I've followed the exact same step however todos are not added or updated in production server(yarn build && yarn start) 🙁
How can we upload a file to supabase using this new api routes ?
Will we be able to set the cache duration of .from() requests with supabase.js in the future?
Great video! I would love to to see a video with all tips together.
Thanks for this video 🎉
nice vid!
Thank you for this, I've been checking the channel daily for more info on using the App Directory!
Thanks John. Amazing content. Would you consider doing a realtime example? Thanks
I'd like to see a video about how to do a to-do the supabase way.