question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Getting 404 on dynamic routes when not accessing from a <Link />

See original GitHub issue

Bug report

Describe the bug

I’m getting 404 on production build when trying to first load a dynamic route. If accessing the route from a <Link href="/blog/[slug]" as="/blog/test">[...]</Link>, the page is correctly rendered.

To Reproduce

  1. git clone https://github.com/zeit/next-learn-demo.git
  2. cd next-learn-demo
  3. cd 8-deploying
  4. yarn
  5. next build && next export
  6. cd out
  7. serve
  8. Navigate to http://localhost:5000/p/learn-nextjs
  9. See 404

Expected behavior

Load the dynamic route correctly, as if it was navigated from a previous page through <Link />.

System information

  • OS: macOS
  • Browser: Chrome
  • Version of Next.js: 9.2.1

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
jamesmosiercommented, Feb 5, 2020

According to the docs, depending on your use case, you may not need to use next export at all as next build is already enough thanks to Automatic Static Optimization.

Aside from that, try creating a next.config.js file and adding this exportPathMap:

module.exports = {
  exportPathMap: async function(
    defaultPathMap,
    { dev, dir, outDir, distDir, buildId }
  ) {
    return {
      '/': { page: '/' },
      '/about': { page: '/about' },
      '/p/hello-nextjs': { page: '/p/[id]' },
      '/p/learn-nextjs': { page: '/p/[id]' },
      '/p/deploy-nextjs': { page: '/p/[id]' },
    }
  },
}

that way Next will know which route maps to which dynamic route. This example may also be helpful.

0reactions
balazsorban44commented, Jan 28, 2022

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting 404 when first loading dynamic routes on nextjs
When navigating from Home page and clicking into a next/router <Link /> component, the blog post loads correctly, but if I get the...
Read more >
Getting 404 on dynamic routes when not accessing ... - GitHub
I'm getting 404 on production build when trying to first load a dynamic route. If accessing the route from a <Link href="/blog/[slug]" as="/blog ......
Read more >
Dynamic Routes Details - Next.js
Here is some essential information you should know about dynamic routes. ... then any paths not returned by getStaticPaths will result in a...
Read more >
How to return a 404 Not Found page for dynamic routes in ...
If the blog post is not found in the CMS, because it has e.g. been deleted or the slug has changed you can...
Read more >
Routing in Next.js – How to Set Up Dynamic Routing with Pre ...
In this tutorial, you'll learn how to set up dynamic routing in Next.js. You'll also learn about pre-rendering and why it's important.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found