Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prerendered endpoint cannot be fetched by provided fetch in server load #8341

Closed
benlau6 opened this issue Jan 5, 2023 · 2 comments · Fixed by #8453
Closed

Prerendered endpoint cannot be fetched by provided fetch in server load #8341

benlau6 opened this issue Jan 5, 2023 · 2 comments · Fixed by #8453
Labels
bug Something isn't working

Comments

@benlau6
Copy link

benlau6 commented Jan 5, 2023

Describe the bug

A +page.svelte with +page.server.js using provided fetch in load cannot fetch prerendered endpoint (+server.js) after build

Click to the page will give 500 Internal Error Error: Not found: /api/something,
However, it will be okay only if you refresh the page

I tried different methods, found that

Methods that always work

  • +page.js
  • +page.server.js with native fetch and url.origin

Methods that does not work

  • +page.server.js with sveltekit fetch
  • +page.server.js with sveltekit fetch and url.origin

According to https://kit.svelte.dev/docs/load#making-fetch-requests, I suspect that the provided fetch somehow stripped out the origin. It turned the fetch into an internal request to a prerendered route /api/something, which did not exist, and should be localhost:4173/api/something instead

Note: It also happened using @sveltejs/adapter-vercel

Related issue (I suspect)
#8039
#8331

Reproduction

https://github.com/benlau6/sveltekit-fetch-prerendered-endpoint-issue

  1. pnpm build
  2. pnpm preview
  3. Goto website
  4. Goto any of failure pages at navigation
  5. (Optional) Refresh, somehow it works
  6. (Optional) Goto any of success pages at navigation, it always works

Logs

Error: Not found: /api/something
    at resolve (file:///Users/benyp/bull_butler/sveltekit-fetch-prerendered-endpoint-issue/.svelte-kit/output/server/index.js:3095:18)
    at resolve (file:///Users/benyp/bull_butler/sveltekit-fetch-prerendered-endpoint-issue/.svelte-kit/output/server/index.js:2982:34)
    at options.hooks.handle (file:///Users/benyp/bull_butler/sveltekit-fetch-prerendered-endpoint-issue/.svelte-kit/output/server/index.js:3217:59)
    at respond (file:///Users/benyp/bull_butler/sveltekit-fetch-prerendered-endpoint-issue/.svelte-kit/output/server/index.js:2980:42)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async fetch (file:///Users/benyp/bull_butler/sveltekit-fetch-prerendered-endpoint-issue/.svelte-kit/output/server/index.js:2785:20)
    at async file:///Users/benyp/bull_butler/sveltekit-fetch-prerendered-endpoint-issue/.svelte-kit/output/server/index.js:2714:12
    at async load (file:///Users/benyp/bull_butler/sveltekit-fetch-prerendered-endpoint-issue/.svelte-kit/output/server/entries/pages/failure/1/_page.server.js:2:15)
    at async load_server_data (file:///Users/benyp/bull_butler/sveltekit-fetch-prerendered-endpoint-issue/.svelte-kit/output/server/index.js:921:18)
SyntaxError: Unexpected token < in JSON at position 0
    at JSON.parse (<anonymous>)
    at packageData (/Users/benyp/bull_butler/sveltekit-fetch-prerendered-endpoint-issue/node_modules/.pnpm/[email protected]/node_modules/undici/lib/fetch/body.js:553:19)
    at specConsumeBody (/Users/benyp/bull_butler/sveltekit-fetch-prerendered-endpoint-issue/node_modules/.pnpm/[email protected]/node_modules/undici/lib/fetch/body.js:517:10)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async load (file:///Users/benyp/bull_butler/sveltekit-fetch-prerendered-endpoint-issue/.svelte-kit/output/server/entries/pages/failure/1/_page.server.js:3:16)
    at async load_server_data (file:///Users/benyp/bull_butler/sveltekit-fetch-prerendered-endpoint-issue/.svelte-kit/output/server/index.js:921:18)
    at async Promise.all (index 1)
    at async render_data (file:///Users/benyp/bull_butler/sveltekit-fetch-prerendered-endpoint-issue/.svelte-kit/output/server/index.js:2194:19)
    at async resolve (file:///Users/benyp/bull_butler/sveltekit-fetch-prerendered-endpoint-issue/.svelte-kit/output/server/index.js:3067:22)
    at async respond (file:///Users/benyp/bull_butler/sveltekit-fetch-prerendered-endpoint-issue/.svelte-kit/output/server/index.js:2980:22)

System Info

System:
    OS: macOS 13.1
    CPU: (10) arm64 Apple M1 Pro
    Memory: 50.56 MB / 16.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 18.7.0 - ~/.nvm/versions/node/v18.7.0/bin/node
    npm: 8.15.0 - ~/.nvm/versions/node/v18.7.0/bin/npm
  Browsers:
    Chrome: 95.0.4638.69
    Firefox: 108.0.1
    Safari: 16.2
  npmPackages:
    @sveltejs/adapter-auto: ^1.0.0 => 1.0.0 
    @sveltejs/kit: ^1.0.0 => 1.0.1 
    svelte: ^3.54.0 => 3.55.0 
    vite: ^4.0.0 => 4.0.4

Severity

serious, but I can work around it

Additional Information

No response

@joeldrake
Copy link
Contributor

joeldrake commented Jan 5, 2023

I ran into the exact same problem. Im doing the fetch from a load function in +layout.server.ts (to an endpoint that prerender during build).

Thanks for the tip using native fetch and url.origin, this seem to work when I do npm run preview on the build. Will upload to Vercel later and check if that works too.

Edit: native fetch and inserting url.origin in the fetch URL worked fine on Vercel as well. Will run with that and keep an eye on this issue.

@dummdidumm dummdidumm self-assigned this Jan 10, 2023
@dummdidumm dummdidumm added the bug Something isn't working label Jan 10, 2023
@dummdidumm
Copy link
Member

This happens because the endpoint (+server.js) is prerendered and therefore removed from the manifest. The server does not know that however, and tries to find the endpoint in the manifest, where it's not found. state.initiator is not set, so the error is thrown instead of doing an actual fetch request - so the solution is probably to populate state.initiator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants