flowbite-datepicker icon indicating copy to clipboard operation
flowbite-datepicker copied to clipboard

Version 1.3.0 is broken on Nuxt, NextJS, Sveltekit SSR

Open delcantao opened this issue 1 year ago • 12 comments

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)

delcantao avatar Jun 29 '24 19:06 delcantao

i have the same problem

fhrancoo avatar Jul 02 '24 00:07 fhrancoo

This also impacts static precompilation with Next.JS.

MrJoy avatar Jul 04 '24 21:07 MrJoy

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.

20x-dz avatar Jul 10 '24 13:07 20x-dz

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

oiluis avatar Jul 10 '24 19:07 oiluis

+1

shreejalmaharjan-27 avatar Jul 18 '24 12:07 shreejalmaharjan-27

this issue became a problematic on sveltekit SSR.

rushkii avatar Aug 14 '24 04:08 rushkii

Same problem with Astro.

pReya avatar Aug 15 '24 15:08 pReya

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

20x-dz avatar Aug 15 '24 21:08 20x-dz

@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"
  },

MrJoy avatar Aug 19 '24 19:08 MrJoy

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 avatar Aug 22 '24 16:08 depeele

Merged the PR! I'll do some tests and release 1.3.1. Thanks @depeele.

zoltanszogyenyi avatar Dec 18 '24 07:12 zoltanszogyenyi

https://github.com/themesberg/flowbite-datepicker/releases/tag/v1.3.1

zoltanszogyenyi avatar Dec 18 '24 07:12 zoltanszogyenyi