I need help with customizable dynamic url in Nextjs with React. I have created a Nextjs+React app which utilizes custom server.js to handle routing and 'static' dynamic url. Now I need to migrate this existing setup to be able to use whatever slug the user choose to define from -let's say- CMS and still render the correct page (content will be retrieved from api).
What I think I need to do is this:
Let's we have a user-defined url : www.host.com/some-blog-group/post-of-the-day
- Let all routes to be handled by
pages/index.js
inserver.js
.
server.get('*', (req, res) => handle(req, res));
- In
page/index.js
, I would need to send a request back to the backend ie.http://backend.com/get-page/some-blog-group/post-of-the-day
- Backend will give back a response basically consisting of a list of components to render for the page and possibly the type and other integral data of page (to help with formatting and retrieving content/layout of the component).
- Each of these component then will render render data given by point.3 as necessary.
- Tada! The page is rendered without a problem.
Was this train of thought is correct? The existing app is already have tens of pages (setup 'statically' by folder as Next.js recommended and rerouted as necessary in server.js for dynamic routing). Is there a better way to migrate this setup? Or maybe I do not need Nextjs in the beginning?