Skip to content

Commit

Permalink
Rename utility isClient to isBrowser (#1389)
Browse files Browse the repository at this point in the history
* Rename utility `isClient` to `isBrowser`

* Switch to document check instead of window

* Apply suggestions from code review

Co-authored-by: Michelle Vinci <michelle.vinci@shopify.com>

* Update docs/utilities/isserver.md

Co-authored-by: Michelle Vinci <michelle.vinci@shopify.com>

Co-authored-by: Michelle Vinci <michelle.vinci@shopify.com>
  • Loading branch information
blittle and mcvinci authored Jun 1, 2022
1 parent c0167bc commit 9a21108
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 45 deletions.
9 changes: 9 additions & 0 deletions .changeset/chilled-baboons-deny.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
'@shopify/hydrogen': minor
---

**Breaking change**

The utility `isClient` has been renamed to `isBrowser`. This is because the utility really checks if the running context is a browser, _not_ if the context is a client component.

All client components by default also run on the server when they are server rendered. If you don't want that to happen, use the `isBrowser()` hook. Remember that anything not server rendered will be unavailable for SEO bots.
33 changes: 33 additions & 0 deletions docs/utilities/isbrowser.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
gid: 232fa7f6-5c17-4659-b8c8-4466be88d602
title: isBrowser
description: The isBrowser utility is a function that returns a Boolean indicating if the code was run in the browser.
---

The `isBrowser` utility is a function that returns a Boolean indicating if the code was run in the browser. This is useful because client components are server-rendered. Use the `isBrowser` utility if you don't want logic within your client component to be rendered on the server.

## Example code

```tsx
import {isBrowser} from '@shopify/hydrogen';

export function MyComponent() {
if (isBrowser()) {
return <p>I ran in the browser</p>;
}

return <p>I ran on the server</p>;
}
```

## Arguments

None

## Return type

A Boolean indicating if the code was run in the browser.

## Related utilities

- [`isServer`](https://shopify.dev/api/hydrogen/utilities/isserver)
34 changes: 0 additions & 34 deletions docs/utilities/isclient.md

This file was deleted.

2 changes: 1 addition & 1 deletion docs/utilities/isserver.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ A Boolean indicating if the code was run on the server.

## Related utilities

- [`isClient`](api/hydrogen/utilities/isclient)
- [`isBrowser`](https://shopify.dev/api/hydrogen/utilities/isbrowser)
2 changes: 1 addition & 1 deletion packages/hydrogen/src/utilities/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export {
export {loadScript} from './load_script';
export {wrapPromise} from './suspense';
export {flattenConnection} from './flattenConnection';
export {isClient} from './isClient';
export {isBrowser} from './isBrowser';
export {isServer} from './isServer';
export {getMeasurementAsParts, getMeasurementAsString} from './measurement';
export {parseMetafieldValue} from './parseMetafieldValue';
Expand Down
1 change: 1 addition & 0 deletions packages/hydrogen/src/utilities/isBrowser/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {isBrowser} from './isBrowser';
6 changes: 6 additions & 0 deletions packages/hydrogen/src/utilities/isBrowser/isBrowser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/** The `isClient` utility is a function that returns a boolean indicating
* if the code was run in the browser.
*/
export function isBrowser() {
return typeof document !== 'undefined';
}
1 change: 0 additions & 1 deletion packages/hydrogen/src/utilities/isClient/index.ts

This file was deleted.

6 changes: 0 additions & 6 deletions packages/hydrogen/src/utilities/isClient/isClient.ts

This file was deleted.

4 changes: 2 additions & 2 deletions packages/hydrogen/src/utilities/isServer/isServer.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {isClient} from '../isClient';
import {isBrowser} from '../isBrowser';

/** The `isServer` utility is a function that returns a `boolean` indicating
* if the code was run on the server.
*/
export function isServer() {
return !isClient();
return !isBrowser();
}

0 comments on commit 9a21108

Please sign in to comment.