Ads by ProfitSence
Close

Detect script is by browser or server-side

Last Updated on Wednesday 5th Oct 2022
  • If you are working with server-side rendering, this could be very helpful.
			
					const isBrowser = () => ![typeof window, typeof document].includes('undefined');

			
	

Usage

			
					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');