nuxt / nuxt

The Intuitive Vue Framework.
https://nuxt.com
MIT License
54.59k stars 4.99k forks source link

A link to a `public` `.json` file breaks prerender in `generate` mode #29202

Open yannbertrand opened 3 weeks ago

yannbertrand commented 3 weeks ago

Environment


Reproduction

  1. Fork CodeSandbox code
  2. npm ci
  3. npm run dev -> should work correctly
  4. npm run generate -> breaks due to 404 /file.json

Proof that it comes from the .json file only:

  1. rm public/file.json and the NuxtLink from pages/index.vue
  2. npm run generate -> should work correctly

Describe the bug

nuxt generate breaks on the prerender step when a .json file is linked in a page.

Additional context

Weirdly, it does not break the CodePen when the file containing the link is app.vue:

  1. mv pages/index.vue app.vue
  2. npm run generate -> should work correctly

Logs

nitro 5:33:19 PM
  │ ├── Error: [404] Page not found: /file.json
  │ └── Linked from /
  ├─ /_payload.json (2ms)                                                                                         nitro 5:33:19 PM
                                                                                                                  nitro 5:33:19 PM
Errors prerendering:
  ├─ /file.json (11ms)                                                                                            nitro 5:33:19 PM
  │ ├── Error: [404] Page not found: /file.json
  │ └── Linked from /
                                                                                                                  nitro 5:33:19 PM

 ERROR  Exiting due to prerender errors.                                                                                5:33:19 PM

  at prerender (node_modules/nitropack/dist/chunks/prerender.mjs:220:11)
  at async node_modules/nuxt/dist/index.mjs:3628:5
  at async build (node_modules/nuxt/dist/index.mjs:5778:3)
  at async Object.run (node_modules/nuxi/dist/chunks/build.mjs:101:5)
  at async Object.run (node_modules/nuxi/dist/chunks/generate.mjs:59:5)
  at async runCommand$1 (node_modules/nuxi/dist/shared/nuxi.3e201632.mjs:1648:16)
  at async runCommand$1 (node_modules/nuxi/dist/shared/nuxi.3e201632.mjs:1639:11)
  at async runMain$1 (node_modules/nuxi/dist/shared/nuxi.3e201632.mjs:1777:7) 

 ERROR  Exiting due to prerender errors.                                                                                5:33:19 PM
adamdehaven commented 1 week ago

I believe you can get around this by telling Nitro about the route:

  nitro: {
    prerender: {
      crawlLinks: true,
      // Specify routes that the crawler may not be able to discover for prerendering
      routes: [
        '/file.json',
      ],
    },
  },