Search code examples
reactjswindowserver-side-renderingastrojs

Astro: browser or server side?


How to detect if astro rendering happens in browser or on the server side?

Found no info in official reference and troubleshooting guide.

The only 'advice' I found is to use 'client:only'.

UPDATE Solution found


    export const isCLientSide = typeof window !== 'undefined'


Solution

  • You can check for the window object using globalThis. If window is present, you are on the client.

    if (globalThis.window) {
        console.log('Hello client-side!')
    }
    
    if (!globalThis.window) {
        console.log('Hello server-side!')
    }