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

datepicker causing error in SSR starting with version v2.4.0 (document is not defined) #950

Closed
depeele opened this issue Aug 22, 2024 · 5 comments

Comments

@depeele
Copy link

depeele commented Aug 22, 2024

Describe the bug
The removal of the datepicker-related webpack configuration and src/plugins/ between flowbite v2.3.0 and v2.4.0 introduced an error with SSR rendered code.

This removal causes the full flowbite-datepicker package to be included in the final, minimized version of flowbite. Within the full flowbite-datepicker package, the first line of js/lib/dom.js causes SSR code to fail with a "document is not defined" error:

const range = document.createRange();

For SSR to be able to properly use flowbite.min.js either:

  1. The webpack configuration and src/plugins/ need to be reverted to what was used for flowbite v2.3.0;
  2. OR flowbite-datepicker needs to update js/lib/dom.js to something like:
    const range = (typeof document !== 'undefined' && document.createRange());

Issue #921 has a comment suggesting the use of a composable to ensure flowbite is not included in SSR code, but there are cases where the way flowbite is included is not in the control of the end user (e.g. svelte-flowbite).

Given that this issue is related to a single unprotected use of document, requiring the end-user to ensure flowbite is not included in SSR code seems like over-kill.

@depeele
Copy link
Author

depeele commented Aug 22, 2024

I've also added a comment to a related flowbite-datepicker issue #41 recommending the typeof document solution.

depeele added a commit to oparkins/Ayia that referenced this issue Aug 22, 2024
Pin flowbite to v2.3.0 (actually, <2.4.0).

flowbite 2.4.0 changed the way it imported flowbite-datepicker:
- removed `src/plugins` and the related webpack configuration that
  restricted what was actually included from flowbite-datepicker in the
  final flowbite.min.js;
- now includes the entire flowbite-datepicker package in the final
  flowbite.min.js;

flowbite-datepicker has (always had) an issue that causes an SSR
"document is not defined" error due to
[lib/js/jdom.js](https://github.com/themesberg/flowbite-datepicker/blob/a343785aa01c7671f891cacc491040e238c4acc5/js/lib/dom.js#L1):
```javascript
var range = document.createRange();
```

This issue can be easily resolved within flowbite-datepicker by changing
the line to:
```javascript
var range = (typeof document !== 'undefined' && document.createRange());
```

Reported:
- flowbite: [issue 950](themesberg/flowbite#950);
- flowbite-datepicker: added a [comment](themesberg/flowbite-datepicker#41 (comment))
  to an existing, related issue;
@depeele depeele changed the title datepicker causing error in SSR in the version v2.4.1 (document is not defined) datepicker causing error in SSR in the version v2.4.0 (document is not defined) Aug 22, 2024
@depeele depeele changed the title datepicker causing error in SSR in the version v2.4.0 (document is not defined) datepicker causing error in SSR starting with version v2.4.0 (document is not defined) Aug 22, 2024
@zoltanszogyenyi
Copy link
Member

Hey @depeele,

For SSR you just need to make sure the client (aka. browser) is available.

Here's a guide for Angular's SSR, for example:

https://flowbite.com/docs/getting-started/angular/#using-with-angular-ssr

Can you please try this? Technically you can use the datepicker plugin directly from the core JS Flowbite.

@samsonc89
Copy link

I'm running into this issue when trying to deploy a project with Astro. We're not even using a datepicker anywhere and it's causing the build error

@depeele
Copy link
Author

depeele commented Sep 13, 2024

@zoltanszogyenyi : This approach does NOT resolve the issue I have reported. I believe this issue should be reopened because my problem has not been resolved. Below are the details of the ongoing problem:

@zoltanszogyenyi suggested:

For SSR you just need to make sure the client (aka. browser) is available.

Here's a guide for Angular's SSR, for example:

https://flowbite.com/docs/getting-started/angular/#using-with-angular-ssr

Can you please try this? Technically you can use the datepicker plugin directly from the core JS Flowbite.

For my use-case:

  1. I have no control over how flowbite is loaded since it is being loaded by the svelte-flowbite module that I am using;
  2. I am not making use of the date picker but it is being included due to the way flowbite was changed between v2.3.0 and 2.4.0 — removal of the datepicker-related webpack configuration and src/plugins/;
  3. The date picker component includes initialization that it performs whether or not the date picker is actually used — js/lib/dom.js : const range = document.createRange();

Item 3 is the root cause of the issue which was effectively mitigated by the "plugin" approach previously used by flowbite, specifically for this date picker component.

To enable the use of newer versions of flowbite in projects such as mine, the webpack configuration and src/plugins/ need to be reverted to what was used for flowbite v2.3.0.

Again, given that this issue is related to a single unprotected use of document within a sub-module that flowbite has chosen to include, requiring the end-user to ensure flowbite is not included in SSR code seems like over-kill — particularly in cases where this approach is not an option.

Every thing else in flowbite works fine within SSR code. This single date picker problem is the cause of this issue.

Thank you!

@depeele
Copy link
Author

depeele commented Sep 13, 2024

This entire issue seems related to pull request 907

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

3 participants