Running `nuxt preview` results in 404 response code on `/:wildcard` routes
Environment
- Operating System:
Darwin - Node Version:
v16.19.0 - Nuxt Version:
3.1.2 - Nitro Version:
2.1.2 - Package Manager:
[email protected] - Builder:
vite - User Config:
app,experimental,modules,postcss,router,runtimeConfig,sourcemap,ssr,tailwindcss - Runtime Modules:
@nuxtjs/[email protected],@pinia/[email protected],@vueuse/[email protected] - Build Modules:
-
Reproduction
https://stackblitz.com/edit/github-ax5qe4?file=README.md&initialpath=/asdf
Describe the bug
The following PR for nitro intentionally made this change. Now, Cypress tests are failing in my app due to 404 response codes.
https://github.com/unjs/nitro/pull/864
Change summary
This change is the cause of the issue.
-npx serve -s ./public
+npx serve ./public
Additional context
No response
Logs
No response
In your case, you can instead run npx serve -s .output/public instead of nuxi preview
Hey, @danielroe. Is there any way this can be made into a configuration option instead? I'd be happy to submit a PR if you think that'd be valuable to others.
Investigating, I think it might be that you have an outdated version of serve cached on your system: it works fine for me in this reproduction: https://stackblitz.com/edit/github-pz4zoe.
However, you can indeed configure this:
export default defineNuxtConfig({
nitro: {
commands: {
preview: 'npx serve -s ./public'
}
}
})
Other notes
A PR to document this would be very welcome - or if you have any suggests for a more ergonomic configuration option I think that would probably be welcome upstream at https://github.com/unjs/nitro.
Another option, which I think we would like to adopt, would be a built-in static preview within nitro which doesn't suffer from the same bug that serve does: https://github.com/vercel/serve/issues/722.
Finally, note that we can't fully replicate the behaviour of a static site host locally as it really very much depends on where you are hosting. (For example, some places will return a 404 if the file doesn't exist.) We do also have more advanced static providers upstream like vercel-static and netlify-static which could also be customised to have a different preview command.
FYI, I ran rm -rf ~/.npm/_npx and then npx serve again and it seems to work without the -s. It's odd, though: npx serve --help still shows -s / --single as an option. I'm also not sure I trust this, considering https://github.com/vercel/serve-handler#rendersingle-boolean says "This is disabled by default"
Pro-tip: If you're using Cypress, I found that the npx serve options would absolutely spam the test logs, resulting in six-figures of lines per test run. I used this nitro preview command to eliminate that: npx serve .output/public --no-request-logging
Maybe it was a fluke when it worked on my system. It fails on Github Actions -- and given that the documented behaviour is that the -s is needed to work here, I think that's correct.
Anyone who uses Cypress together with yarn start will need to configure this.