use of window libs or localStorage #43
-
Sometimes we need to call first libs that make use of window context. I tried to use vitedge with it but had no success since I couldn't separate the code from the render server of the client. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
@calebeaires Hi! If you have access to the code, you can do: if (!import.meta.env.SSR) {
window.anything
myFunctionThatCallsWindow()
} If it's a component that checks import { ClientOnly } from 'vitedge'
...
<ClientOnly>
<MyComponentThatCallsWindow />
</ClientOnly> If it's a library that calls window on import, you can use dynamic imports like this: let library
if (!import.meta.env.SSR) {
lib = await import('my-lib')
} If dynamic imports are an issue for any reason, add import lib from 'my-lib?client'
if (!import.meta.env.SSR) {
// use lib
} It's also possible to have 2 different entry points in Vitedge, one for server and one for client, if you need that. Keep the entry point for the client in your Hope that helps! |
Beta Was this translation helpful? Give feedback.
@calebeaires Hi! If you have access to the code, you can do:
If it's a component that checks
window
during rendering, there's a ClientOnly component wrapper to make it run only in the browser:If it's a library that calls window on import, you can use dynamic imports like this:
If dynamic imports are an issue for any reason, add
vite-plugin-iso-import
and use normal imports: