Repository navigation
Description
Describe the bug
The normal content negotiation ability to be served html or json data based on the http accept header seems to break if the request comes from __layout.svelte. Instead of the expected content, it returns a 404 response.
Reproduction
Request root with accept: application/json header and expected data is returned
Request root with browser and error is produced (see log below - status from fetch is 404)
Changing url fetched in __layout to /__data.json OR renaming index.svelte "fixes" it (of course, you likely want a home page so the latter isn't really a fix, just thought it may help identify the cause).
Using __data.json works so it's not a show-stopper, but in case someone else comes across it this will hopefully be findable.
Logs
Unexpected token N in JSON at position 0
SyntaxError: Unexpected token N in JSON at position 0
at JSON.parse (<anonymous>)
at Proxy.<anonymous> (file:///Users/simon/dev/open-source/sveltekit-shadow-layout/.svelte-kit/runtime/server/index.js:2559:21)
at async load (__layout.svelte:9:14)
at async load_node (file:///Users/simon/dev/open-source/sveltekit-shadow-layout/.svelte-kit/runtime/server/index.js:2585:22)
at async respond$1 (file:///Users/simon/dev/open-source/sveltekit-shadow-layout/.svelte-kit/runtime/server/index.js:2986:15)
at async resolve (file:///Users/simon/dev/open-source/sveltekit-shadow-layout/.svelte-kit/runtime/server/index.js:3456:11)
at async respond (file:///Users/simon/dev/open-source/sveltekit-shadow-layout/.svelte-kit/runtime/server/index.js:3392:20)
at async file:///Users/simon/dev/open-source/sveltekit-shadow-layout/node_modules/.pnpm/@sveltejs+kit@1.0.0-next.396_svelte@3.49.0+vite@3.0.3/node_modules/@sveltejs/kit/dist/vite.js:1555:22System Info
System:
OS: macOS 12.4
CPU: (6) x64 Intel(R) Core(TM) i5-8500B CPU @ 3.00GHz
Memory: 29.02 MB / 32.00 GB
Shell: 5.8.1 - /bin/zsh
Binaries:
Node: 16.14.0 - /usr/local/bin/node
npm: 8.3.1 - /usr/local/bin/npm
Browsers:
Brave Browser: 102.1.39.111
Chrome: 103.0.5060.134
Chrome Canary: 106.0.5204.0
Edge: 103.0.1264.71
Firefox: 102.0.1
Safari: 15.5
Safari Technology Preview: 16.0
npmPackages:
@sveltejs/adapter-auto: next => 1.0.0-next.64
@sveltejs/kit: next => 1.0.0-next.396
svelte: ^3.44.0 => 3.49.0
vite: ^3.0.0 => 3.0.3Severity
annoyance
Additional Information
The scenario is having the root endpoint be the data for the site, in a SaaS setup where the sites are served based on host name.
Activity
mustofa-id commentedon Jul 28, 2022
it seem json parse is fail in ssr, try to check response status before returning value:
don't forget
loadis runs both during ssr and in the client.CaptainCodeman commentedon Jul 28, 2022
The point is it shouldn't fail, it should work. It works when calling the same endpoint from other places, or by calling the different (unnormalized? non-canonical?) form (
__data.json). Just not calling '/' from__layout.svelteThe reason JSON parse fails is because the response is a 404, which is what's wrong. It should get the JSON data. The exact same code inside
index.svelteworks, so it's a "surprise".CaptainCodeman commentedon Jul 31, 2022
endpoints and loading are both going to change, so this is likely going to be obsolete