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
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
- fix(api): use different cors config to fix errors when frontend requests SSR Probably related: https://github.com/sveltejs/kit/issues/8314 — committed to ClayenKitten/itam-edu by ClayenKitten a year ago
I am facing same issue. During production there would be different hosts,
event.url.originmatches to a k8s container and not the user facing hostname. This creates CORS on server side becauseevent.url.originis never going to match user facing hostname, which BE api would be accepting in theoriginheader. We cannot useno-corson server as universal fetch returns an empty body response which is not useful at all. We need a way to set the initialevent.url.originwhich 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
Global
fetchdoesn’t have access to cookies unfortunately, making authenticated requests impossible without passing cookies from+page.server.tsfirst.See #8314 (comment) for a full explanation. The recommended workaround for now is to use the global
fetchfunction instead of the one provided by theloadfunction.@itssumitrai does the documentation below help resolve your issue? Does setting
ORIGINto your user facing hostname help? https://kit.svelte.dev/docs/adapter-node#environment-variables-origin-protocolheader-and-hostheaderI 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-originprior to handing it to the user’s JavaScript - when doingresponse.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
fetchin these cases.I have the same exact issue on a GitPod repo. I needed to disable
csrfinsvelte.config.jsin order to continue working on it, and will need to remember resetting it in production. I guess I could something likecsrf: env.prod === 'dev', but it feels weird to disable it completely during development.Perhaps an
ORIGIN=...variable in the.envfile could solve this?