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

Content negotiation fails with fetch from __layout #5732

Closed
CaptainCodeman opened this issue Jul 27, 2022 · 3 comments
Closed

Content negotiation fails with fetch from __layout #5732

CaptainCodeman opened this issue Jul 27, 2022 · 3 comments

Comments

@CaptainCodeman
Copy link
Contributor

CaptainCodeman commented Jul 27, 2022

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

example repo

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/@[email protected][email protected][email protected]/node_modules/@sveltejs/kit/dist/vite.js:1555:22

System 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.3

Severity

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.

@mustofa-id
Copy link

it seem json parse is fail in ssr, try to check response status before returning value:

if (resp.ok) {
...

don't forget load is runs both during ssr and in the client.

@CaptainCodeman
Copy link
Contributor Author

CaptainCodeman commented 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.svelte

The 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.svelte works, so it's a "surprise".

@CaptainCodeman
Copy link
Contributor Author

endpoints and loading are both going to change, so this is likely going to be obsolete

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants