nuxt icon indicating copy to clipboard operation
nuxt copied to clipboard

Running `nuxt preview` results in 404 response code on `/:wildcard` routes

Open davidmyersdev opened this issue 2 years ago • 2 comments

Environment

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

davidmyersdev avatar Feb 10 '23 00:02 davidmyersdev

In your case, you can instead run npx serve -s .output/public instead of nuxi preview

danielroe avatar Feb 12 '23 09:02 danielroe

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.

davidmyersdev avatar Feb 13 '23 00:02 davidmyersdev

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.

danielroe avatar May 18 '23 10:05 danielroe

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

bmulholland avatar May 19 '23 11:05 bmulholland

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.

bmulholland avatar May 19 '23 11:05 bmulholland