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

Support setInterval #92

Closed
matthewmueller opened this issue May 28, 2022 · 1 comment · Fixed by #233
Closed

Support setInterval #92

matthewmueller opened this issue May 28, 2022 · 1 comment · Fixed by #233
Labels
help wanted We'd love your help!

Comments

@matthewmueller
Copy link
Contributor

Not sure if it's related but localStorage, window, document, setInterval, etc have a weird behavior too.

view: render error ReferenceError: setInterval is not defined

when wrapped inside a if (typeof document != "undefined") or anything that v8 will appreciate, everything is fine, console.log included

Originally posted by @ldlac in #87 (comment)

@matthewmueller
Copy link
Contributor Author

Hey, thanks for pointing this out @ldlac. I definitely need better docs here, but views are server-side rendered with V8, the hydrated on the client, so your top-level statements in Svelte have the same restrictions as you would have in SvelteKit, Next.js, etc.

The difference is that they use Node.js to do SSR, so it has things like setInterval, while Bud uses V8. So TL;DR:

  • localStorage, window, document cannot be used top-level right now and no plans currently to support them. Your views need to work outside of the browser environment for the initial render 1
  • setInterval: should be supported. Getting this working should be pretty easy thanks to this excellent library which looks like it has an interval library.

Footnotes

  1. I have thought about adding a minimal DOM implementation to V8 via https://www.npmjs.com/package/undom. I'm not sure if this is a good idea though because it may hide errors. At the same time it would be quite a bit more convenient for people that understand what's happening, you wouldn't need to wrap your addEventListener in a typeof document != undefined for example.

@matthewmueller matthewmueller changed the title setInterval and other browser APIs are not defined Support setInterval Jun 5, 2022
@matthewmueller matthewmueller added the help wanted We'd love your help! label Jun 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted We'd love your help!
Projects
None yet
1 participant