frandiox / vite-ssr

Use Vite for server side rendering in Node
MIT License
829 stars 92 forks source link

Window not defined in vue/entry-client #141

Closed PascalAOMS closed 2 years ago

PascalAOMS commented 2 years ago

I cloned this repo to try out the Vue example.

Installed all the packages defined in package.json and was wondering why it didn't work out of the box.

The first error is this: image

So I went into vue/src/main.ts and changed import viteSSR, { ClientOnly } from 'vite-ssr' to import viteSSR, { ClientOnly } from 'vite-ssr/vue'.

Now the server starts but upon visiting localhost:1337 I get this: image

How shall I fix this?

Thank you.

frandiox commented 2 years ago

@PascalAOMS Can you downgrade to vite@2.7.x? I haven't had the chance yet to check 2.8 and it looks like something is not backwards compatible. I guess you used a package manager differnt from Yarn, right? Otherwise it should have respected yarn.lock.

PascalAOMS commented 2 years ago

Thank you for the quick response.

Oh wow, it worked with Yarn using the correct lock file.

Appreciate your help.

salqueng commented 2 years ago

https://github.com/frandiox/vite-ssr/discussions/143#discussioncomment-2294141 I just made a fix for vite 2.8.x. Can you check it out?

Lightnet commented 2 years ago

I got the same error. /node_modules/vite-ssr/vue/entry-client.js:24

ReferenceError: window is not defined

Did some debug a bit. There no check on client or server as window is null.

/node_modules/vite-ssr/vue/entry-client.js

let url;
if (import.meta.env.SSR) {
console.log("server")
url = import.meta.env.BASE_URL;//not sure if correct
}else{
console.log("client")
url = new URL(window.location.href);
}

Found another error that vue-router error on window is null.