svelte-plugins / datepicker

A simple datepicker component designed for Svelte.
https://svelte-plugins.github.io/datepicker
MIT License
54 stars 12 forks source link

Document is not defined -> SSR #11

Closed ICTVTS3 closed 8 months ago

ICTVTS3 commented 8 months ago

I'm experiencing a bug where a lot of the times on refresh my localhost crashes with the error 'document is not defined'. It stems from this line in the datepicker.svelte component:

$: theme !== null && document.documentElement.setAttribute('data-picker-theme', theme);

Thanks for checking it out :)

dysfunc commented 8 months ago

https://github.com/svelte-plugins/datepicker/commit/b851d6f7adfaf86be55d4de2a47cee5ec7dc3044

ICTVTS3 commented 8 months ago

I updated to the new version but the changes weren't actually in the update as far as I can see. Adding the fix manually doesn't change anything related to the SSR problem, it's still throwing 'document is not defined'. Apparantly in combination with SSR it's a good idea to check 'browser' @ import { browser } from '$app/environment' before utilising the document variable.

Thanks for the help.

hilkeheremans commented 7 months ago

hi @dysfunc - just revisiting this since I am also seeing this issue.

I see your fix in /src of v1.0.4 but it appears the /dist does not have the most recently compiled code in the published npm package. You sure v1.0.4 was actually built before it was published?

dysfunc commented 7 months ago

Well, it looks like you both were right. I apologize for the delay on this fix. I just published 1.0.5. Can you please install the latest and let me know if your problem is resolved

ICTVTS3 commented 7 months ago

It's still causing the issues. You can solve it by adding

import { browser } from '$app/env';

And changing the line in question to

$: browser && theme !== null && document && document.documentElement.setAttribute('data-picker-theme', theme);

dysfunc commented 7 months ago

This has been fixed and a new version has been release 1.0.6. Please install the latest and let me know if this issue has been resolved for you.

ICTVTS3 commented 7 months ago

This has been fixed and a new version has been release 1.0.6. Please install the latest and let me know if this issue has been resolved for you.

It has, thank you :)