unjs / nitro

Next Generation Server Toolkit. Create web servers with everything you need and deploy them wherever you prefer.
https://nitro.unjs.io
MIT License
5.5k stars 472 forks source link

Deployment Overview page 404 #1394

Open ChrisScrumping opened 1 year ago

ChrisScrumping commented 1 year ago

Environment

https://nitro.unjs.io/

Reproduction

Try going to https://nitro.unjs.io/deploy

Describe the bug

When opening https://nitro.unjs.io/deploy or refreshing the page I get a 404 page not found.

Additional context

Initially I discovered this by clicking a link to the page from here: https://nuxt.com/docs/getting-started/deployment#deployment

Logs

No response

Hebilicious commented 1 year ago

Looks like an SSR bug, the same bug happens for these 2 pages :

@Atinux I've seen this happening a few times in Nuxt/UnJS websites. Is this docus / Nuxt content related?

ChrisScrumping commented 1 year ago

I have had it happen recently on a Nuxt app I am creating but it turned out to be a redirect issue on the server not Nuxt.

In this case though there does not appear to be any redirects happening, the server just returns a 404.

curl -vv https://nitro.unjs.io/deploy/workers

JamieCurnow commented 1 year ago

I started looking into this. I couldn't reproduce locally...

Interestingly, I can reproduce the bug on vercel: https://jamiecurnow-nitro-nomc9cbxu-jamiecurnow.vercel.app/deploy/workers

But when I deploy to Netlify it works perfectly: https://stately-donut-53286f.netlify.app/deploy/workers/

Both the builds are using npm run build and looking at the dist directory for the public assets. I'll do some more digging tomorrow on node versions etc...

JamieCurnow commented 1 year ago

@Hebilicious does nitro detect that it's in a vercel / netlify environment and apply build presets accordingly?

JamieCurnow commented 1 year ago

I think I've found the cause... When doing a vercel preset build, most paths in the .vercel/output/config.json do not have a trailing slash. BUT the paths that don't work do have a trailing slash...

{
  "version": 3,
  "overrides": {
    "index.html": {
      "path": ""
    },
    "guide/getting-started/index.html": {
      "path": "guide/getting-started"
    },
    // ...
    "deploy/workers/index.html": {
      "path": "deploy/workers/" // <-- trailing slash
    },
  }
}

I've tested this by adding the following to the nuxt config:

nitro: {
    vercel: {
      config: {
        version: 3,
        overrides: {
          "deploy/index.html": {
            path: "deploy",
          },
          "deploy/workers/index.html": {
            path: "deploy/workers",
          },
        },
      },
    },
  },

And it works! 🥳

Now to try to figure out why those trailing slashes are being added....

Hebilicious commented 1 year ago

Nice catch @JamieCurnow, you're on fire. I looks like it happens with the pre-rendered routes because we're removing leading slash, but not the trailing slash here https://github.com/unjs/nitro/blob/6bc50b2f9416dc9af6ba327440deea2ec8e35a33/src/presets/vercel.ts#L164

JamieCurnow commented 1 year ago

@Hebilicious

I've traced it down to this Object.fromEntries call here: https://github.com/unjs/nitro/blob/main/src/presets/vercel.ts#L159

Interestingly, nitro._prerenderedRoutes includes these two entries:

[
    { route: "/deploy/workers", fileName: "/deploy/workers/index.html" },
    // ... other paths....
    { route: "/deploy/workers/", fileName: "/deploy/workers/index.html" },
  ]

It doesn't do that for every route - only some (the ones that are broken.)

Then the object.entries is making the object and the last _prerenderedRoutes object to go through the loop ends up being the one used. And it turns out it's the one with the trailing slash in this case.

Question is - do we go further upstream and figure out why nitro's _prerenderedRoutes have multiple entries for these seemingly random routes, or shall we replace trailing slashes on the vercel preset end?

JamieCurnow commented 1 year ago

@Hebilicious it's not random - it's because of links. If I change this link here: https://github.com/unjs/nitro/blob/main/docs/content/2.deploy/2.workers.md?plain=1#L23

From: [limitations](/deploy/workers/#limitations) to [limitations](/deploy/workers#limitations)

Then we don't get multiple entries in nitro._prerenderedRoutes. And it all builds and works as it should...

So I guess it's kind of working as it should...? But this would be a nightmare of a bug for a user to figure out so it feels like it should be handled in some way?

It's not affecting the netlify preset, but I wonder if it is affecting others?

Do you want me to make a PR for fixing it on the Vercel preset and we can just fix others if/when they come up? Or is this a deeper issue that should be handled further back in nitro?

Hebilicious commented 1 year ago

Thanks for the great investigation ! I think we should remove the trailing slashes in the affected presets. Let's wait for what @pi0 has to say.

9mm commented 11 months ago

Seems good idea to fix this, im new to nitro and it is weird projects' own website doesnt even load 🤔

hecktarzuli commented 10 months ago

@pi0 this caused my whole site to be removed from Google. I have to think there are others out there too.

pi0 commented 10 months ago

Sorry for the troubles @hecktarzuli I will try to pick up this issue and investigate ASAP.

Hebilicious commented 10 months ago

@pi0 Could we remove the trailing slashes for the affected presets ?