Detect script is by browser or server-side

Last Updated On Thursday 22nd Jul 2021
  • If you are working with server-side rendering, this could be very helpful.
	const isBrowser = () => ![typeof window, typeof document].includes('undefined');


	console.log(isBrowser()) // true (csr || browser)
console.log(isBrowser()) // false (ssr || Node)

With Svelte

  • We Can import the browser bool from the app variables.
	import { browser } from '$app/env';
!browser ? console.log(result, 'ssr') : console.log(result, 'csr');