-
-
Notifications
You must be signed in to change notification settings - Fork 115
NPM package using getContext/setContext doesn't work during SSR dev #360
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
Comments
This error usually happens when there are multiple versions of svelte, which means that the It's a strange coincidence but a similar error just happened today with alpha testing of vite-3 in a test for vite-plugin-svelte: https://github.com/sveltejs/vite-plugin-svelte/blob/main/packages/e2e-tests/kit-node Some information that can help track this down: stacktrace from vite-plugin-svelte test Error: Function called outside component initialization
at get_current_component (file:///home/dominikg/develop/vitejs/vite-ecosystem-ci/workspace/svelte/vite-plugin-svelte/node_modules/.pnpm/[email protected]/node_modules/svelte/internal/index.mjs:953:15)
at setContext (file:///home/dominikg/develop/vitejs/vite-ecosystem-ci/workspace/svelte/vite-plugin-svelte/node_modules/.pnpm/[email protected]/node_modules/svelte/internal/index.mjs:985:5)
at Proxy.setSomeContext (file:///home/dominikg/develop/vitejs/vite-ecosystem-ci/workspace/svelte/vite-plugin-svelte/packages/e2e-tests/_test_dependencies/svelte-api-only/index.js:4:2)
at index.svelte:35:1
at Object.$$render (/home/dominikg/develop/vitejs/vite-ecosystem-ci/workspace/svelte/vite-plugin-svelte/node_modules/.pnpm/[email protected]/node_modules/svelte/internal/index.js:1758:22)
at Object.default (root.svelte:43:39)
at eval (/.svelte-kit/runtime/components/layout.svelte:8:41)
at Object.$$render (/home/dominikg/develop/vitejs/vite-ecosystem-ci/workspace/svelte/vite-plugin-svelte/node_modules/.pnpm/[email protected]/node_modules/svelte/internal/index.js:1758:22)
at root.svelte:37:37
at $$render (/home/dominikg/develop/vitejs/vite-ecosystem-ci/workspace/svelte/vite-plugin-svelte/node_modules/.pnpm/[email protected]/node_modules/svelte/internal/index.js:1758:22) in this stacktrace svelte is referenced in node_modules, but another version of svelte exists in vite's optimizedDeps in |
@dominikg mh, yes I assumed that it had something to do with deduping not working, I'm just really stumped because the package doesn't have a Is there a workaround for this that you know of? |
you can try playing with vite-plugin-svelte should generate a working configuration for that, you can see it when you run with thanks for uploading the dependency tmp-context-issue to the npm registry, see comment by @gtm-nayan |
Thanks. I'll try to find a working solution with that and will report back. I also just realised that running |
I didn't get it to work with either |
In the Vite-only repro, the error is because it is actually being called outside component init, moving it inside App.svelte doesn't throw an error As for enyo's SK one, adding the package to ssr.noExternal works around it but that probably has other ramifications for actual packages |
Thanks @gtm-nayan Would you mind explaining why that works? (Edit: this actually caused other issues that I wasn't able to resolve) |
Unfortunately, I also found it by fiddling around with the config values so I don't have a definite answer as to why it works. My thought process was if it's force bundled the package would reference the same svelte instance as the app. |
I tried the same fix in other projects and it surfaced other bugs that I wasn't able to get rid of, so |
@dominikg so is it safe to say that this issue is cause by |
It may be improved by changing this code to also add js-libraries to ssr.noExternal during dev. Right now the else block of But you mention that you ran into other issues with noExternal. Can you elaborate on them? |
One thing that still confuses me a bit is that this seems to be causing an error only with packages from npm, a local workspace package with a very similar setup is working just fine: https://github.com/sveltejs/vite-plugin-svelte/blob/main/packages/e2e-tests/kit-node/src/routes/index.svelte#L26 cc @bluwy |
I am a bit stumped by this. Doesn't that mean that no npm dependency can use a context at the moment? If that's the case I'm surprised this isn't higher on the priority list with more people running into the issue. |
Adding it to ssr.noExternal fixes it for most people. You didn't share what issues this causes for you. |
I'm been checking this bug on and off, and it seems like this is a bug that we've missed in our test, even though we test the context api too, but we're not installing it locally as an npm package which hides the error. From what I can see, in Vite SSR, your source code that imports When letting Node use it's resolve algorithm, it resolves to I can re-confirm that Node uses the So how does Node still get the
So I think this is a Vite issue, and not something vite-plugin-svelte can fix at the moment. Other than the workaorund of |
@enyo please test this again with Edit: in case of SvelteKit, just update to it's latest version, that uses the above. |
I have an alternate confirmation of what @dominikg suggested above is working. I distribute a Svelte component library by NPM and while I'm still getting up to speed on Vite a consumer of my lib tried out Vite w/ slightly older versions. The same getContext / setContext issue described above popped up. In testing I upgraded their repo to |
Thanks for confirming |
Uh oh!
There was an error while loading. Please reload this page.
Describe the bug
I have distilled the issue to this:
setContext
orgetContext
svelte
as a direct dependency but only aspeerDependency
(or not even that)This only happens as npm package, using a local dependency works fine.
Reproduction
I tried reproducing in a REPL but it works fine, that's why I assume it's a Svelte Kit issue.
Here is the minimal reproduction repo: https://github.com/enyo/context-issue
See the issue immediately:
git clone https://github.com/enyo/context-issue.git cd context-issue/svelte-kit-app npm install npm run dev
The repo contains the package, where the
dist/index.js
has this content:and a svelte kit app that uses it.
Logs
No response
System Info
Severity
blocking all usage of SvelteKit
Additional Information
No response
The text was updated successfully, but these errors were encountered: