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

SvelteKit - Function called outside component initialization #7

Closed
inzanez opened this issue Jul 15, 2021 · 15 comments
Closed

SvelteKit - Function called outside component initialization #7

inzanez opened this issue Jul 15, 2021 · 15 comments

Comments

@inzanez
Copy link

inzanez commented Jul 15, 2021

Hi

I'm trying to use sswr with a newly created SvelteKit project, building with the node adapter.
I have added some simple test after having had issues:

	import { useSWR } from 'sswr';

	try {
		const { data } = useSWR('http://jsonplaceholder.typicode.com/posts/1');
	} catch(e) {
		console.log(e);
	}

test to a

  • route
  • component

and I always get back the same error:

Error: Function called outside component initialization
    at get_current_component (index.mjs:813)
    at onMount (index.mjs:820)
    at l.useSvelte (index.js:1)
    at useSWR (index.js:1)
    at instance ([dialogId].svelte:42)
    at init (index.mjs?v=99bae134:1660)
    at new U5BdialogIdu5D ([dialogId].svelte:153)
    at createComponent (svelte-hooks.js:136)
    at U5BdialogIdu5D.targetCmp.$replace (svelte-hooks.js:183)
    at refreshComponent (proxy.js:170)

Any idea why that might be?

@ConsoleTVs
Copy link
Owner

What is this node adapter?

@inzanez
Copy link
Author

inzanez commented Jul 16, 2021

Basically a build adapter that will result in an application build for NodeJs instead of a static file build.

@inzanez
Copy link
Author

inzanez commented Jul 29, 2021

Ok, looking a bit more into this, it does seem that this doesn't work with SvelteKit yet. Any idea why that might be? I just started a new project using

npm init svelte@next my-app
cd my-app
npm install
npm run dev

and tried to run the sample code, resulting in the same error described above.

@frederikhors
Copy link
Contributor

Same here with SvelteKit.

I'm not using node adapter.

So, the adapter is not the problem here.

I think we should initialize sswr before using it and use it with setContext/getContext.

@frederikhors
Copy link
Contributor

Using the steps in https://kit.svelte.dev/docs#introduction-getting-started

you can try yourself: it doesn't work.

@ConsoleTVs
Copy link
Owner

Strange, last time I checked it worked... I'll check

@ConsoleTVs
Copy link
Owner

ConsoleTVs commented Jul 31, 2021

I just tried and it does work... 😕

Fresh install and everything...

@frederikhors
Copy link
Contributor

Can you please create an example directory with the new Svelte Kit Demo project and sswr?

@ConsoleTVs
Copy link
Owner

Wait, I just double checked by installing the remote pkg and you're right it fails. I'm going to see what's going on here... Thanks for the error report, seems it worked fine with my local pkg :D

@frederikhors
Copy link
Contributor

Do you have any news on this, @ConsoleTVs?

@ConsoleTVs
Copy link
Owner

I tries to fix it and i was unable to. I got to the conclusion thst it might either be a bundle issue or something related. I am not an expert but sveltekit api is not yet stable either and it is due change, ao i might either wait for some PR from people who know more about the internals, wait for sveltekit to be stable or in short, have some more free time to allocate to this

@benbender
Copy link
Contributor

benbender commented Aug 9, 2021

It's not a bug in this package, but in sveltekit. I saw the same problem on my own tinkering with react-query - so I opened a bug in sveltekit. See sveltejs/kit#2147.

Btw, it only happens when both preconditions are true: a) lifecycle-method called inside an installed package. b) in dev-mode.

It works in svelte-repl though. So its likely a problem with the vite-dev-server.

PS: You can circumvent the bug for now by installing swrev and copy sswr to your local $lib-folder and import it from there.

@ConsoleTVs
Copy link
Owner

It's not a bug in this package, but in sveltekit. I saw the same problem on my own tinkering with react-query - so I opened a bug in sveltekit. See sveltejs/kit#2147.

Btw, it only happens when both preconditions are true: a) lifecycle-method called inside an installed package. b) in dev-mode.

It works in svelte-repl though. So its likely a problem with the vite-dev-server.

PS: You can circumvent the bug for now by installing swrev and copy sswr to your local $lib-folder and import it from there.

That could explain why my local symlinked pkg was working lol

@frederikhors
Copy link
Contributor

I think this fix it (for now): #11.

Can we close this?

@ConsoleTVs
Copy link
Owner

Yes, I hope this will be fixed in the future by svelte plugin authors. Great work!

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

4 participants