Next.js Discord

Discord Forum

Can nextjs handle dynamic routes with export true & page router without getStaticProps?

Unanswered
Alligator mississippiensis posted this in #help-forum
Open in Discord
Alligator mississippiensisOP
Hello, I'm migrating from SSR to SSG, and I have several routes such as /playlists/[playlistId] that are handled by the Next.js page router.

Everything works correctly—until I reload a page or navigate directly to a URL without using Next’s <Link>. In those cases, the 404 page is shown. To work around this, I configured the server to always return index.html instead of a 404. That, however, introduced a new problem: when I visit /playlists/10283819, the content of index.html is rendered even though the address bar still shows /playlists/10283819.

How can I force the router to take over on the initial page load so that /playlists/[playlistId] is rendered instead of index.html?

I don’t want to use getStaticProps to pre-generate every dynamic route, and I’ve already removed all getInitialProps calls.

Is Next.js able to handle this? I plan to host the site on AWS S3 behind CloudFront.

1 Reply

Alligator mississippiensisOP
My super hacky solution is this in the pages/_app.tsx

But I would need to list the dynamic routes during the build time I guess somehow and then inject them into the _app.tsx.