diff --git a/docs/latest/concepts/islands.md b/docs/latest/concepts/islands.md index 3bd3d0c5..514f4841 100644 --- a/docs/latest/concepts/islands.md +++ b/docs/latest/concepts/islands.md @@ -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"; diff --git a/src/runtime/utils.ts b/src/runtime/utils.ts index 38ae909b..1e00e783 100644 --- a/src/runtime/utils.ts +++ b/src/runtime/utils.ts @@ -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"; /**