ui icon indicating copy to clipboard operation
ui copied to clipboard

Vite-SSG + NuxtUI: `ReferenceError: document is not defined`

Open OrbisK opened this issue 6 months ago • 15 comments

Environment

System: OS: Linux 6.8 Ubuntu 24.04.1 LTS 24.04.1 LTS (Noble Numbat) CPU: (8) x64 11th Gen Intel(R) Core(TM) i5-1135G7 @ 2.40GHz Memory: 29.05 GB / 62.58 GB Container: Yes Shell: 5.9 - /usr/bin/zsh Binaries: Node: 23.5.0 - ~/.nvm/current/bin/node npm: 10.9.2 - ~/.nvm/current/bin/npm pnpm: 8.15.9 - ~/.nvm/current/bin/pnpm bun: 1.1.42 - ~/.nvm/current/bin/bun Browsers: Chrome: 130.0.6723.69

Is this bug related to Nuxt or Vue?

Vue

Version

3.2.0

Reproduction

https://github.com/OrbisK/vite-ssg-nuxt-ui-reproduction

  1. pnpm install && pnpm build

Description

✓ 558 modules transformed.
.vite-ssg-temp/o5d4vm833a/assets/style-B8cS4vCq.css  113.21 kB
.vite-ssg-temp/o5d4vm833a/main.mjs                   336.28 kB
✓ built in 970ms
file:///home/robin/WebstormProjects/nuxt-ui-vite-ssg/.vite-ssg-temp/o5d4vm833a/main.mjs:9959
    const style = document.createElement("style");
                  ^

ReferenceError: document is not defined
    at file:///home/robin/WebstormProjects/

Additional context

No response

Logs


OrbisK avatar Jul 10 '25 10:07 OrbisK

I am not sure, if import.meta.client should be true here:

https://github.com/nuxt/ui/blob/a813ea700e139e6b1de7a68da891b7d1ed079dc4/src/runtime/plugins/colors.ts#L52

OrbisK avatar Jul 10 '25 10:07 OrbisK

not sure if we need: if (import.meta.client && !import.meta.env.SSR && nuxtApp.isHydrating && !nuxtApp.payload.serverRendered) { instead. Or: if (import.meta.client && import.meta.env.VITE_SSG !== 'true' && nuxtApp.isHydrating && !nuxtApp.payload.serverRendered) {.

But I wonder if this is expected behavior of vite-ssg.

OrbisK avatar Jul 10 '25 10:07 OrbisK

related: https://github.com/antfu-collective/vite-ssg?tab=readme-ov-file#how-to-allow-rollup-tree-shake-your-client-code ?

OrbisK avatar Jul 10 '25 11:07 OrbisK

maybe this is related too: https://github.com/nuxt/nuxt/issues/32581

OrbisK avatar Jul 11 '25 08:07 OrbisK

maybe this is related too: nuxt/nuxt#32581

I do feel this is indeed related more upstream as @danielroe was referring in https://github.com/nuxt/nuxt/issues/32581#issuecomment-3058601669

this might be related to a nitro or rollup update instead of a nuxt change ...

and I also remember that removing !import.meta.env.SSR will lead to various errors, tho I don't remember them atm (so I might be wrong)

sandros94 avatar Jul 11 '25 15:07 sandros94

Hi! 👋 This issue has been open for 60 days without activity and will be closed in 7 days. Please comment if it's still relevant.

github-actions[bot] avatar Sep 10 '25 02:09 github-actions[bot]

still relevant

OrbisK avatar Sep 10 '25 06:09 OrbisK

Hi! 👋 This issue has been open for 60 days without activity and will be closed in 7 days. Please comment if it's still relevant.

github-actions[bot] avatar Nov 10 '25 02:11 github-actions[bot]

still relevant (tested with latest)

OrbisK avatar Nov 10 '25 12:11 OrbisK

@Barbapapazes Is your fix in https://github.com/nuxt/ui/pull/5347 related? 🤔

benjamincanac avatar Nov 11 '25 20:11 benjamincanac

@Barbapapazes Is your fix in #5347 related? 🤔

Yes, my PR fixes exactly this bug!

Barbapapazes avatar Nov 11 '25 22:11 Barbapapazes

@OrbisK, give a try to #5347, it should fix your issue!

Barbapapazes avatar Nov 11 '25 22:11 Barbapapazes

cc @thetutlage (pinging you so you leave a heart on one of the comments). The fix should allow you (I'm not sure if import.meta.env.SSR is injected when using Vite in middleware mode) to use Nuxt UI with Adonis + Inertia.js SSR.

Barbapapazes avatar Nov 11 '25 22:11 Barbapapazes

@Barbapapazes Thanks for the ping. I can surely give it a try and see if Vite in middleware mode sets that env variable or not

thetutlage avatar Nov 12 '25 05:11 thetutlage

@Barbapapazes Thanks for the ping. I can surely give it a try and see if Vite in middleware mode sets that env variable or not

Oops, I just realized that it won't work for Inertia, https://github.com/nuxt/ui/pull/5396

Barbapapazes avatar Nov 12 '25 06:11 Barbapapazes

I've just merged #5396, can we close this?

benjamincanac avatar Nov 17 '25 10:11 benjamincanac

https://github.com/nuxt/ui/pull/5396 fixed it

OrbisK avatar Nov 17 '25 13:11 OrbisK