kit: CORS errors during server rendering

Describe the bug

When fetching an endpoint using CORS from a +page.js file, SvelteKit throws a CORS error when trying to render the page on the server, such as during development mode or prerendering for production. CORS doesn’t seem to make sense in this context, since the fetch request is being performed on the server, which has no origin.

This seems related to https://github.com/sveltejs/kit/issues/7441, but that issue involved +page.server.js files. In this issue, we want to perform the fetch on the client AND the server (during SSR/prerendering).

Reproduction

https://stackblitz.com/edit/cors-error-during-server-render-repro?file=src%2Froutes%2Ftest%2F%2Bpage.js

Using the client-side navigation from / to /test fetches data on the client perfectly fine, but doing a browser navigation/reload on the /test page throws a CORS error on the server. Errors are also thrown when trying to run a build.

Logs

No response

System Info

StackBlitz WebContainer

Binaries:
    Node: 16.14.2 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 7.17.0 - /usr/local/bin/npm
  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.3

Severity

serious, but I can work around it

Additional Information

No response

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 7
  • Comments: 16 (4 by maintainers)

Commits related to this issue

Most upvoted comments

I am facing same issue. During production there would be different hosts, event.url.origin matches to a k8s container and not the user facing hostname. This creates CORS on server side because event.url.origin is never going to match user facing hostname, which BE api would be accepting in the origin header. We cannot use no-cors on server as universal fetch returns an empty body response which is not useful at all. We need a way to set the initial event.url.origin which is going to be used on all the cors requests going forward. Or better would be to not use ‘cors’ during server side rendering.

Hi, I think that we are missing something. If load is running twice (server and client) if we use global fetch instead of fetch by param we loose SSR. Because the fetch will be not saved and restore in hydration instance. So we have CORS solved but the case makes irrelevant, because the data is not reused.

Why in PageServerLoad is working and a load is not? Should we not have at least a config to prevent to “browser cors simulation”?

I understand is a cool trick to prevent a page works in SSR but not in CSR for a Universal Rendering. But for multiple multiple archs builds above containers the event.origin will not be the same as the browser perseve

The recommended workaround for now is to use the global fetch function instead of the one provided by the load function.

Global fetch doesn’t have access to cookies unfortunately, making authenticated requests impossible without passing cookies from +page.server.ts first.

What I don’t understand is why fetch in +page.server.ts works, but not +page.ts during SSR 🙄

See #8314 (comment) for a full explanation. The recommended workaround for now is to use the global fetch function instead of the one provided by the load function.

@itssumitrai does the documentation below help resolve your issue? Does setting ORIGIN to your user facing hostname help? https://kit.svelte.dev/docs/adapter-node#environment-variables-origin-protocolheader-and-hostheader

I have the same exact issue on a GitPod repo. I needed to disable csrf in svelte.config.js in order to continue working on it, and will need to remember resetting it in production. I guess I could something like csrf: env.prod === 'dev', but it feels weird to disable it completely during development.

Perhaps an ORIGIN=... variable in the .env file could solve this?

I am also developing on Gitpod and am running into this issue. I use the same workaround as @Haffi921.

It seems that the browser removes the access-control-allow-origin prior to handing it to the user’s JavaScript - when doing response.headers.get('access-control-allow-origin'), it doesn’t show up. I assume that Stackblitz and GitPod either have no control to forward these headers (because they can’t access them either) or are removing them before passing it on.

We can’t fix this without removing the sensible checks to ensure things run on the client and server the same, so I think the best we can do is document this. A workaround (which SvelteKit will warn you about, but you can ignore it in this case) is to use the global fetch in these cases.

I have the same exact issue on a GitPod repo. I needed to disable csrf in svelte.config.js in order to continue working on it, and will need to remember resetting it in production. I guess I could something like csrf: env.prod === 'dev', but it feels weird to disable it completely during development.

Perhaps an ORIGIN=... variable in the .env file could solve this?