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

Version 1.3.0 is broken on Nuxt, NextJS, Sveltekit SSR #41

Open
delcantao opened this issue Jun 29, 2024 · 10 comments
Open

Version 1.3.0 is broken on Nuxt, NextJS, Sveltekit SSR #41

delcantao opened this issue Jun 29, 2024 · 10 comments

Comments

@delcantao
Copy link

It is throwing an error stating 'document is not defined'. It may be attempting to access the DOM on the server side. Reverting to version 1.2.6 resolves the issue, but the version 1.3.0 is broken.

Stack trace shows the error is on date picker

I don't even use the DatePicker component in my project.

\node_modules\flowbite-datepicker\dist\main.cjs.js:595:13)
at Module._compile (node:internal/modules/cjs/loader:1376:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
at Module.load (node:internal/modules/cjs/loader:1207:32)
at Module._load (node:internal/modules/cjs/loader:1023:12)
at Module.require (node:internal/modules/cjs/loader:1235:19)
at require (node:internal/modules/helpers:176:18)
at Object. (D:\repos__repo\laundrybag.ch\LaundryBag.App\node_modules\flowbite\src\components\datepicker\index.ts:8:1)
at Module._compile (node:internal/modules/cjs/loader:1376:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)

@fhrancoo
Copy link

fhrancoo commented Jul 2, 2024

i have the same problem

@MrJoy
Copy link

MrJoy commented Jul 4, 2024

This also impacts static precompilation with Next.JS.

@20x-dz
Copy link

20x-dz commented Jul 10, 2024

Can confirm with latest nuxt 3.
Issue is related to https://github.com/themesberg/flowbite-datepicker/blob/master/js/lib/dom.js#L1

When commenting this out in the dist file, SSR works just fine.

I honestly fail to understand why this was added in a minor version in flowbite, especially considering the updated SSR-related docs, where everything should only be imported on the client. And then there's this dependency, that runs even though it's not used.

@oiluis
Copy link

oiluis commented Jul 10, 2024

Yep ... same error ... I had to downgrade to flowbite 2.3.0

@shreejalmaharjan-27
Copy link

+1

@rushkii
Copy link

rushkii commented Aug 14, 2024

this issue became a problematic on sveltekit SSR.

@delcantao delcantao changed the title Version 1.3.0 is broken on Nuxt 3 Version 1.3.0 is broken on Nuxt, NextJS, Sveltekit SSR Aug 14, 2024
@pReya
Copy link

pReya commented Aug 15, 2024

Same problem with Astro.

@20x-dz
Copy link

20x-dz commented Aug 15, 2024

Do you have insights on how we can work around this issue until it is fixed @zoltanszogyenyi ? Any help would be appreciated… tyvm! 🙏

@MrJoy
Copy link

MrJoy commented Aug 19, 2024

@20x-dz My workaround has been to forcibly pin relevant deps to older versions until the issue is resolved.

  "resolutions": {
    "flowbite-datepicker": "1.2.6",
    "flowbite": "2.3.0"
  },

@depeele
Copy link

depeele commented Aug 22, 2024

Prior to flowbite 2.4.0, flowbite-datepicker was included via a plugin that only included a portion of flowbite-datepicker in the final, minimized flowbite.min.js.
Starting with flowbite 2.4.0, the entire flowbite-datepicker package is included directly in the final, minimized flowbite.min.js.

The inclusion of flowbite-datepicker in the primary flowbite.min.js causes SSR code to fail with a "document is not defined" error due to lib/js/dom.js:

var range = document.createRange();

This issue can be easily resolved within flowbite-datepicker by changing the line to:

var range = (typeof document !== 'undefined' && document.createRange());

Alternatively, the problematic pre-initialization of range could be moved into code that is only executed if flowbite-datepicker is actually used:

var range = null;
export function parseHTML(html) {
  if (range == null) { range = document.createRange() }
  return range.createContextualFragment(html);
}

Without one of these changes, the upstream flowbite would need to exclude flowbite-datepicker from the final, minimized flowbite.min.js

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 added a commit to depeele/flowbite-datepicker that referenced this issue Sep 26, 2024
To enable inclusion in applications that *may also use* SSR, the
invocation of `document.createRange()` needs to be moved out of the
global scope. Moving it into the `parseHTML()` method resolves this
issue since it will only be executed if the component is actually used
(on the browser side).

Full inclusion of this component by flowbite 2.4.0 introduced
SSR-related bugs across multiple frameworks (Nuxt, NextJS, Sveltekit)
due to this unprotected use of `document`.

Prior to flowbite 2.4.0, flowbite-datepicker was included via a "plugin"
approach that mitigated this unprotected use of `document`,

This resolves issue themesberg#41
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

9 participants