Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename utility isClient to isBrowser #1389

Merged
merged 4 commits into from
Jun 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() {
blittle marked this conversation as resolved.
Show resolved Hide resolved
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();
}