next.js icon indicating copy to clipboard operation
next.js copied to clipboard

Disabling useFileSystemPublicRoutes prevents rendering of dynamic routes

Open rtrys opened this issue 2 years ago • 14 comments

Verify canary release

  • [X] 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 [email protected], 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>
}

rtrys avatar Sep 21 '22 15:09 rtrys

I couldn't reproduce the issue, this works fine for me. Maybe link a reproduction as a repository?

rubytree33 avatar Sep 25 '22 22:09 rubytree33

@rubytree33 sure, you can try with this example, see the readme instruccions, it only happens in production mode https://github.com/rtrys/custom-server-hapi-bug

rtrys avatar Sep 26 '22 19:09 rtrys

I'm a little confused, since this repo doesn't have a custom server like your original reproduction instructions does. It also didn't work until I added pages/_error.js. More importantly, this reproduction doesn't use the canary version, and the environment information you provided also doesn't.

Could you please provide a full reproduction? When I followed your original instructions, I used the reproduction template, which uses canary by default. You can create a repository like that with npm create-next-app -e reproduction-template.

rubytree33 avatar Sep 26 '22 21:09 rubytree33

@rubytree33 can you try this one? https://github.com/jonathansamines/nextjs-dynamic-routing-custom-server

rtrys avatar Sep 27 '22 15:09 rtrys

This seems to be missing an index page and a complete package file.

rubytree33 avatar Sep 27 '22 21:09 rubytree33

are you able to reproduce the bug with the instructions?

rtrys avatar Sep 30 '22 16:09 rtrys

AnyUpdates ? I'm facing it too.

a4amado avatar Oct 10 '22 19:10 a4amado

I'm facing this issue too ! .. Any idea how to resolve it ??

smeerkahoven avatar Oct 11 '22 21:10 smeerkahoven

any update about that?, I have the same issue.. does anyone know if there is a temp solution?

edwinaceitunor avatar Oct 11 '22 21:10 edwinaceitunor

I also facing with this issue, ist there any updates?

arevolorio avatar Oct 11 '22 21:10 arevolorio

+1 on this issue

robreinhard avatar Oct 13 '22 15:10 robreinhard

It's been a month and the maintainers didn't even bother to replay.

a4amado avatar Oct 20 '22 05:10 a4amado

😞

rtrys avatar Oct 21 '22 19:10 rtrys

is there any update, this ussue is still present in latest canary

rtrys avatar Nov 14 '22 18:11 rtrys

tested again with [email protected] and the result is the same see the instructions in this example: https://github.com/jonathansamines/nextjs-dynamic-routing-custom-server

LM-GUA-42000870:~ huortiz$ curl -v http://127.0.0.1:3000/sub/hello
*   Trying 127.0.0.1:3000...
* Connected to 127.0.0.1 (127.0.0.1) port 3000 (#0)
> GET /sub/hello HTTP/1.1
> Host: 127.0.0.1:3000
> User-Agent: curl/7.79.1
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 404 Not Found
< X-Powered-By: Next.js
< ETag: "qzkfzt8wjo1tc"
< Content-Type: text/html; charset=utf-8
< Content-Length: 2352
< Vary: Accept-Encoding
< Date: Fri, 25 Nov 2022 04:38:10 GMT

this issue is preventing we can go ahead with latest version, would be nice to have some support here

rtrys avatar Nov 25 '22 04:11 rtrys

maintainers, is there any update?

rtrys avatar Dec 16 '22 19:12 rtrys

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

scripter-co avatar Dec 23 '22 02:12 scripter-co

still waiting for any comment from developers

rtrys avatar Feb 10 '23 20:02 rtrys

Hi :wave: :smile:

I have still not found documentation where it explains how to use app.render in a Next Custom Server.

Fortunately I have found several issues like this one where people use app.render(request, response, pathname, query) and pass the pathname as /something/[variable] (so thank you).

Sadly it does not work for me... But when tried to pass it as /something/128, then it worked :tada:

The point is I do not know for sure what is supposed to be the good behavior and where we are supposed to find this kind of advanced information about Custom Server. Because the page https://nextjs.org/docs/advanced-features/custom-server is not detailed at all :disappointed:

Also these behavior are the same in both cases with or without useFileSystemPublicRoutes config... it does not seem to change anything on my side. I am not facing the same problem as you do, I do not understand anything :sweat_smile:

If someone can enlighten me :pray:

versions : Next v12.3.1, Express v4.18.1

heymath avatar Feb 22 '23 23:02 heymath

After a few tests with [email protected], this issue is not longer present

rtrys avatar May 08 '23 16:05 rtrys

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

github-actions[bot] avatar Jun 08 '23 00:06 github-actions[bot]