frandiox / vitesse-ssr-template

🏕 Opinionated Vue + Vite Starter Template with SSR in Node.js
https://vitesse-ssr.vercel.app/
MIT License
187 stars 29 forks source link

Running in SSR dev is not working #14

Open dvlden opened 2 years ago

dvlden commented 2 years ago

As soon as I run pnpm run dev I get instant exit with error:

// Internal server error: render is not a function

const url = window.location;
//          ^
// ReferenceError: window is not defined

What am I doing wrong here?

karimdaghari commented 2 years ago

I may be a bit late but I'll put it here in case someone else might encounter the same issue.

the window object doesn't exist in server environment (the SS in SSR). To solve it simply add a guard:

const url = typeof window === "undefined" ? null : window.location;

Feel free to switch null with a value you're comfortable with. But just so you know, in JS, usually null means that we deliberately set the value as such whereas if we were to put "" (empty string) or undefined we can't really know whether it was the dev's choice or JS.