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.

Disabling useFileSystemPublicRoutes prevents rendering of dynamic routes

See original GitHub issue

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

    Operating System:
      Platform: darwin
      Arch: arm64
      Version: Darwin Kernel Version 21.6.0: Mon Aug 22 20:19:52 PDT 2022; root:xnu-8020.140.49~2/RELEASE_ARM64_T6000
    Binaries:
      Node: 16.15.1
      npm: 8.11.0
      Yarn: N/A
      pnpm: N/A
    Relevant packages:
      next: 12.3.1
      eslint-config-next: N/A
      react: 17.0.2
      react-dom: 17.0.2

What browser are you using? (if relevant)

Chrome, Safari

How are you deploying your application? (if relevant)

Custom Server

Describe the Bug

When I set useFileSystemPublicRoutes: false, dynamic routes are no longer handled by app.render and trigger a 404, even when I have an specific handler for this path.

Is worth to mention this issue only occurs on production build, the dev server works fine.

could this #33808 introduce the bug? this was introduced in next@12.1.0, previous versions of next works fine

Looks like this bug was alredy reported, but closed for inactivity #32004

Expected Behavior

App is able to resolve dynamic routes

Link to reproduction

To Reproduce

Run the follow code as production

next.config.js

module.exports = {
    useFileSystemPublicRoutes: false
};

server.js

'use strict';

const { createServer } = require('http')
const { parse } = require('url')
const next = require('next')

const dev = process.env.NODE_ENV !== 'production'
const hostname = 'localhost'
const port = 3000

// when using middleware `hostname` and `port` must be provided below
const app = next({ dev, hostname, port })
const handle = app.getRequestHandler()

app.prepare().then(() => {
  createServer(async (req, res) => {
    try {
      // Be sure to pass `true` as the second argument to `url.parse`.
      // This tells it to parse the query portion of the URL.
      const parsedUrl = parse(req.url, true)
      const { pathname, query } = parsedUrl

      if (pathname === '/sub/hello') {
        await app.render(req, res, '/sub/[id]', query)
      } else {
        await handle(req, res, parsedUrl)
      }
    } catch (err) {
      console.error('Error occurred handling', req.url, err)
      res.statusCode = 500
      res.end('internal server error')
    }
  }).listen(port, (err) => {
    if (err) throw err
    console.log(`> Ready on http://${hostname}:${port}`)
  })
})

pages/sub/[id].js

export default function Page() {
  return <span>Welcome to this dynamic page</span>
}

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:3
  • Comments:17 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
scripter-cocommented, Dec 23, 2022

spent quite a bit of time investigating and coming to the same conclusion as the above. created another reproducible repo here: https://github.com/scripter-co/express-nextjs-dynamic-routes

0reactions
rtryscommented, Dec 16, 2022

maintainers, is there any update?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Disabling useFileSystemPublicRoutes prevents rendering of ...
This issue only occurs on production build, the dev server works fine. When setting useFileSystemPublicRoutes to false, dynamic routes are no ...
Read more >
Next.js Disabling file-system routing - Stack Overflow
When useFileSystemPublicRoutes set to false these two routes won't be created at server side, so a user can't access it by typing example.com/ ......
Read more >
How to disable Server-Side Rendering in Next.js
There are some instances where you don't need SSR for your Next.js components. In this article, we'll go over how you can disable...
Read more >
Advanced Features: Custom Server - Next.js
To disable this behavior and prevent routing based on files in pages , open next.config.js and disable the useFileSystemPublicRoutes config:
Read more >
next613 - npm
Next.js is a minimalistic framework for server-rendered React applications. ... Disabling file-system routing; Dynamic assetPrefix.
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