docs: add jsdoc for IS_BROWSER (#1991)

This commit is contained in:
Reed von Redwitz
2023-10-31 18:49:18 +01:00
committed by GitHub
parent 5de34aac93
commit ded71a934f
2 changed files with 18 additions and 1 deletions
+2 -1
View File
@@ -183,7 +183,8 @@ An error occurred during route handling or page rendering. ReferenceError: Event
....
```
Use the `IS_BROWSER` flag as a guard to fix the issue:
Use the [`IS_BROWSER`](https://deno.land/x/fresh/runtime.ts?doc=&s=IS_BROWSER)
flag as a guard to fix the issue:
```tsx islands/my-island.tsx
import { IS_BROWSER } from "$fresh/runtime.ts";
+16
View File
@@ -4,6 +4,22 @@ import { BUILD_ID } from "./build_id.ts";
export const INTERNAL_PREFIX = "/_frsh";
export const ASSET_CACHE_BUST_KEY = "__frsh_c";
/**
* Returns true when the current runtime is the browser and false otherwise. This is used for guard runtime-dependent code.
* Shorthand for the following:
* `typeof document !== "undefined"`
*
* @example
* ```
* if (IS_BROWSER) {
* alert('This is running in the browser!');
* } else {
* console.log('This code is running on the server, no access to window or alert');
* }
* ```
*
* Without this guard, alert pauses the server until return is pressed in the console.
*/
export const IS_BROWSER = typeof document !== "undefined";
/**