mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2026-03-13 10:22:08 +08:00
24 lines
674 B
TypeScript
24 lines
674 B
TypeScript
/**
|
|
* When accessing the window, it is important
|
|
* to account for SSR applications where the
|
|
* window is not available. Code that accesses
|
|
* window when it is not available will crash.
|
|
* Even checking if `window === undefined` will cause
|
|
* apps to crash in SSR.
|
|
*
|
|
* Use win below to access an SSR-safe version
|
|
* of the window.
|
|
*
|
|
* Example 1:
|
|
* Before:
|
|
* if (window.innerWidth > 768) { ... }
|
|
*
|
|
* After:
|
|
* import { win } from 'path/to/this/file';
|
|
* if (win?.innerWidth > 768) { ... }
|
|
*
|
|
* Note: Code inside of this if-block will
|
|
* not run in an SSR environment.
|
|
*/
|
|
export const win: Window | undefined = typeof window !== 'undefined' ? window : undefined;
|