Skip to content
This repository has been archived by the owner on Mar 3, 2023. It is now read-only.

Commit

Permalink
Show development warnings only once (#85)
Browse files Browse the repository at this point in the history
* Show client warnings only once

* Changeset

* Double space
  • Loading branch information
frandiox committed Nov 30, 2022
1 parent a776e01 commit f1ffd57
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/orange-donuts-sit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/hydrogen-react': patch
---

Show storefront development warnings only once.
18 changes: 13 additions & 5 deletions packages/react/src/storefront-client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import {SFAPI_VERSION} from './storefront-api-constants.js';

const warnings = new Set<string>();
const warnOnce = (string: string) => {
if (!warnings.has(string)) {
console.warn(string);
warnings.add(string);
}
};

/**
* The `createStorefrontClient()` function creates helpers that enable you to quickly query the Shopify Storefront API.
*
Expand All @@ -13,21 +21,21 @@ export function createStorefrontClient({
contentType,
}: StorefrontClientProps): StorefrontClientReturn {
if (storefrontApiVersion !== SFAPI_VERSION) {
console.warn(
warnOnce(
`StorefrontClient: The Storefront API version that you're using is different than the version this build of Hydrogen-UI is targeting. You may run into unexpected errors if these versions don't match. Received verion: "${storefrontApiVersion}"; expected version "${SFAPI_VERSION}"`
);
}

// only warn if not in a browser environment
if (__HYDROGEN_DEV__ && !privateStorefrontToken && !globalThis.document) {
console.warn(
`StorefrontClient: Using a private storefront token is recommended for server environments. Refer to the authentication https://shopify.dev/api/storefront#authentication documentation for more details. `
warnOnce(
`StorefrontClient: Using a private storefront token is recommended for server environments. Refer to the authentication https://shopify.dev/api/storefront#authentication documentation for more details.`
);
}

// only warn if in a browser environment and you're using the privateStorefrontToken
if (__HYDROGEN_DEV__ && privateStorefrontToken && globalThis) {
console.warn(
warnOnce(
`StorefrontClient: You are attempting to use a private token in an environment where it can be easily accessed by anyone. This is a security risk; please use the public token and the 'publicStorefrontToken' prop`
);
}
Expand All @@ -53,7 +61,7 @@ export function createStorefrontClient({
}

if (__HYDROGEN_DEV__ && !overrideProps?.buyerIp) {
console.warn(
warnOnce(
`StorefrontClient: it is recommended to pass in the 'buyerIp' property which improves analytics and data in the admin.`
);
}
Expand Down

0 comments on commit f1ffd57

Please sign in to comment.