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

[Bug]: Sveltekit with adapter-cloudflare is using the twind browser module in worker script #418

Closed
0x221A opened this issue Dec 14, 2022 · 2 comments · Fixed by #417
Closed
Labels
🐞 Bug Bug or Bug fixes with:sveltekit Affects @twind/with-sveltekit

Comments

@0x221A
Copy link

0x221A commented Dec 14, 2022

Reproduction

  1. Use Sveltekit with the adapter-cloudflare package
  2. Import the twind module in server code
  3. Call the twind install function
  4. Deploy to a production environment

Describe the bug

Sveltekit is using the twind browser module in worker script, which is causing an error where document is undefined. This error is preventing the twind install function from running properly.

ReferenceError: document is not defined
  at getStyleElement (/Users/.../node_modules/@twind/src/sheets.ts:6:15)
  at cssom (/Users/.../node_modules/@twind/src/sheets.ts:29:11)
  at getSheet (/Users/.../node_modules/@twind/src/sheets.ts:181:86)
  at sheet (/Users/.../node_modules/@twind/src/install.ts:33:11)
  at setup (/Users/.../node_modules/@twind/src/runtime.ts:130:67)
  at eV (/Users/.../node_modules/@twind/src/install.ts:27:10)
  at .svelte-kit/output/server/entries/pages/_layout.ts.js (/Users/.../.svelte-kit/output/server/entries/pages/_layout.ts.js:19:1)
  at __init (/private/var/folders/9t/gvy_vyd1721gjwcmrmlp03t80000gn/T/tmp-68294-pXtmS4gX2vT3/srrsik229vh.js:29:56)
  at .svelte-kit/output/server/nodes/0.js (/Users/.../.svelte-kit/output/server/nodes/0.js:1:1)
  at __init (/private/var/folders/9t/gvy_vyd1721gjwcmrmlp03t80000gn/T/tmp-68294-pXtmS4gX2vT3/srrsik229vh.js:29:56)

Severity

serious, but I can work around it

System Info

## Binaries:
 - Node: 16.18.1 - ~/Library/Caches/fnm_multishells/90960_1670456193433/bin/node
 - Yarn: 3.3.0 - /usr/local/bin/yarn
 - npm: 8.19.2 - ~/Library/Caches/fnm_multishells/90960_1670456193433/bin/npm
## Browsers:
 - Chrome: 108.0.5359.98
 - Safari: 16.2
## npmPackages:
 - @sveltejs/adapter-cloudflare: =1.0.0-next.43 => 1.0.0-next.43 
 - @sveltejs/kit: =1.0.0-next.587 => 1.0.0-next.587 
 - @twind/core: ^1.0.3 => 1.0.3 
 - @twind/preset-autoprefix: ^1.0.3 => 1.0.3 
 - @twind/preset-tailwind: ^1.0.3 => 1.0.3 
 - svelte: ^3.55.0 => 3.55.0 
 - vite: ^4.0.1 => 4.0.1

Additional Information

After the last version of @sveltejs/adapter-cloudflare and @sveltejs/kit was released, they changed to use esm-env to detect the browser environment. However, the esbuild target platform was set to browser, which caused the use of the twind browser module in server code. This led to the error where document is undefined and the failure of the twind install function.

@0x221A 0x221A added the 🐞 Bug Bug or Bug fixes label Dec 14, 2022
@0x221A 0x221A changed the title [Bug]: Sveltekit with adapter-cloudflare is using the twind browser module in server code [Bug]: Sveltekit with adapter-cloudflare is using the twind browser module in worker script Dec 14, 2022
@sastan
Copy link
Collaborator

sastan commented Dec 14, 2022

Thank you for the heads-up. I will look into it asap.

@sastan sastan added the with:sveltekit Affects @twind/with-sveltekit label Dec 14, 2022
@sastan sastan closed this as completed in d9d286f Dec 14, 2022
sastan added a commit that referenced this issue Dec 14, 2022
@sastan
Copy link
Collaborator

sastan commented Dec 14, 2022

I implemented a fix that will be available in the next release (#417). Until that is released you can use the pre-release version (for example @twind/with-sveltekit@next, @twind/core@next or @twind/preset-tailwind@next).

If you have any issues or questions please re-open this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐞 Bug Bug or Bug fixes with:sveltekit Affects @twind/with-sveltekit
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants