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

Fix add-to-cart session event in Live View #614

Merged
merged 21 commits into from
Mar 11, 2023
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
40 changes: 40 additions & 0 deletions .changeset/weak-sheep-sing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
'@shopify/hydrogen-react': patch
'@shopify/remix-oxygen': patch
'@shopify/hydrogen': patch
---

Fix active cart session event in Live View

Introducing `getStorefrontHeaders` that collects the required Shopify headers for making a
Storefront API call.

- Make cart constants available as exports from `@shopify/hydrogen-react`
- Deprecating `buyerIp` and `requestGroupId` props from `createStorefrontClient` from `@shopify/hydrogen`
- Deprecating `getBuyerIp` function from `@shopify/remix-oxygen`

```diff
+ import {getStorefrontHeaders} from '@shopify/remix-oxygen';
import {createStorefrontClient, storefrontRedirect} from '@shopify/hydrogen';

export default {
async fetch(
request: Request,
env: Env,
executionContext: ExecutionContext,
): Promise<Response> {

const {storefront} = createStorefrontClient({
cache,
waitUntil,
- buyerIp: getBuyerIp(request),
i18n: {language: 'EN', country: 'US'},
publicStorefrontToken: env.PUBLIC_STOREFRONT_API_TOKEN,
privateStorefrontToken: env.PRIVATE_STOREFRONT_API_TOKEN,
storeDomain: `https://${env.PUBLIC_STORE_DOMAIN}`,
storefrontApiVersion: env.PUBLIC_STOREFRONT_API_VERSION || '2023-01',
storefrontId: env.PUBLIC_STOREFRONT_ID,
- requestGroupId: request.headers.get('request-id'),
+ storefrontHeaders: getStorefrontHeaders(request),
});
```
7 changes: 7 additions & 0 deletions packages/hydrogen-react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ export type {
ShopifyPageViewPayload,
} from './analytics-types.js';
export {BuyNowButton} from './BuyNowButton.js';
export {
SHOPIFY_S,
SHOPIFY_STOREFRONT_ID_HEADER,
SHOPIFY_STOREFRONT_S_HEADER,
SHOPIFY_STOREFRONT_Y_HEADER,
SHOPIFY_Y,
} from './cart-constants.js';
export type {
Cart,
CartAction,
Expand Down
1 change: 0 additions & 1 deletion packages/hydrogen/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export const STOREFRONT_REQUEST_GROUP_ID_HEADER =
'Custom-Storefront-Request-Group-ID';
export const STOREFRONT_API_BUYER_IP_HEADER = 'Shopify-Storefront-Buyer-IP';
export const STOREFRONT_ID_HEADER = 'Shopify-Storefront-Id';
45 changes: 40 additions & 5 deletions packages/hydrogen/src/storefront.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import {
createStorefrontClient as createStorefrontUtilities,
getShopifyCookies,
type StorefrontApiResponseOk,
SHOPIFY_S,
SHOPIFY_Y,
SHOPIFY_STOREFRONT_ID_HEADER,
SHOPIFY_STOREFRONT_Y_HEADER,
SHOPIFY_STOREFRONT_S_HEADER,
} from '@shopify/hydrogen-react';
import type {ExecutionArgs} from 'graphql';
import {fetchWithServerCache, checkGraphQLErrors} from './cache/fetch';
import {
STOREFRONT_API_BUYER_IP_HEADER,
STOREFRONT_ID_HEADER,
STOREFRONT_REQUEST_GROUP_ID_HEADER,
} from './constants';
import {
Expand Down Expand Up @@ -73,14 +78,23 @@ export type Storefront<TI18n extends I18nBase = I18nBase> = {
export type CreateStorefrontClientOptions<TI18n extends I18nBase> = Parameters<
typeof createStorefrontUtilities
>[0] & {
storefrontHeaders?: StorefrontHeaders;
cache?: Cache;
/** @deprecated use storefrontHeaders instead */
buyerIp?: string;
/** @deprecated use storefrontHeaders instead */
requestGroupId?: string | null;
storefrontId?: string;
waitUntil?: ExecutionContext['waitUntil'];
i18n?: TI18n;
};

type StorefrontHeaders = {
requestGroupId: string | null;
buyerIp: string | null;
cookie: string | null;
};

type StorefrontCommonOptions = {
variables?: ExecutionArgs['variableValues'] & {
country?: CountryCode;
Expand Down Expand Up @@ -119,6 +133,7 @@ function minifyQuery(string: string) {
const defaultI18n: I18nBase = {language: 'EN', country: 'US'};

export function createStorefrontClient<TI18n extends I18nBase>({
storefrontHeaders,
cache,
waitUntil,
buyerIp,
Expand Down Expand Up @@ -148,11 +163,31 @@ export function createStorefrontClient<TI18n extends I18nBase>({
const defaultHeaders = getHeaders({contentType: 'json'});

defaultHeaders[STOREFRONT_REQUEST_GROUP_ID_HEADER] =
requestGroupId || generateUUID();
if (buyerIp) defaultHeaders[STOREFRONT_API_BUYER_IP_HEADER] = buyerIp;
if (storefrontId) defaultHeaders[STOREFRONT_ID_HEADER] = storefrontId;
storefrontHeaders?.requestGroupId || requestGroupId || generateUUID();

if (storefrontHeaders?.buyerIp || buyerIp)
defaultHeaders[STOREFRONT_API_BUYER_IP_HEADER] =
storefrontHeaders?.buyerIp || buyerIp || '';

if (storefrontId) defaultHeaders[SHOPIFY_STOREFRONT_ID_HEADER] = storefrontId;
if (LIB_VERSION) defaultHeaders['user-agent'] = `Hydrogen ${LIB_VERSION}`;

if (storefrontHeaders && storefrontHeaders.cookie) {
const cookies = getShopifyCookies(storefrontHeaders.cookie ?? '');

if (cookies[SHOPIFY_Y])
defaultHeaders[SHOPIFY_STOREFRONT_Y_HEADER] = cookies[SHOPIFY_Y];
if (cookies[SHOPIFY_S])
defaultHeaders[SHOPIFY_STOREFRONT_S_HEADER] = cookies[SHOPIFY_S];
}

// Deprecation warning
if (!storefrontHeaders) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since I guess storefrontHeaders will still be optional, perhaps better to warn only when the other properties are used?

Suggested change
if (!storefrontHeaders) {
if (!storefrontHeaders && (buyerIp || requestGroupId)) {

Copy link
Contributor Author

@wizardlyhel wizardlyhel Mar 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I initially was gonna detect by buyerIp and requestGroupId.. but both of these are null in dev mode

and .. don't we need to have one or the other?

warnOnce(
'"requestGroupId" and "buyerIp" will be deprecated in the next calendar release. Please use "getStorefrontHeaders"',
);
}

async function fetchStorefrontApi<T>({
query,
mutation,
Expand Down Expand Up @@ -183,7 +218,7 @@ export function createStorefrontClient<TI18n extends I18nBase>({
}

const url = getStorefrontApiUrl({storefrontApiVersion});
const requestInit = {
const requestInit: RequestInit = {
method: 'POST',
headers: {...defaultHeaders, ...userHeaders},
body: JSON.stringify({
Expand Down
2 changes: 1 addition & 1 deletion packages/remix-oxygen/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export {
createSessionStorage,
} from './implementations';

export {createRequestHandler, getBuyerIp} from './server';
export {createRequestHandler, getBuyerIp, getStorefrontHeaders} from './server';

export {
createSession,
Expand Down
15 changes: 15 additions & 0 deletions packages/remix-oxygen/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,18 @@ export function createRequestHandler<Context = unknown>({
export function getBuyerIp(request: Request) {
return request.headers.get('oxygen-buyer-ip') ?? undefined;
}

type StorefrontHeaders = {
requestGroupId: string | null;
buyerIp: string | null;
cookie: string | null;
};

export function getStorefrontHeaders(request: Request): StorefrontHeaders {
const headers = request.headers;
return {
requestGroupId: headers.get('request-id'),
buyerIp: headers.get('oxygen-buyer-ip'),
cookie: headers.get('cookie'),
};
}
8 changes: 5 additions & 3 deletions templates/demo-store/server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// Virtual entry point for the app
import * as remixBuild from '@remix-run/dev/server-build';
import {createRequestHandler, getBuyerIp} from '@shopify/remix-oxygen';
import {
createRequestHandler,
getStorefrontHeaders,
} from '@shopify/remix-oxygen';
import {createStorefrontClient, storefrontRedirect} from '@shopify/hydrogen';
import {HydrogenSession} from '~/lib/session.server';
import {getLocaleFromRequest} from '~/lib/utils';
Expand Down Expand Up @@ -34,14 +37,13 @@ export default {
const {storefront} = createStorefrontClient({
cache,
waitUntil,
buyerIp: getBuyerIp(request),
i18n: getLocaleFromRequest(request),
publicStorefrontToken: env.PUBLIC_STOREFRONT_API_TOKEN,
privateStorefrontToken: env.PRIVATE_STOREFRONT_API_TOKEN,
storeDomain: `https://${env.PUBLIC_STORE_DOMAIN}`,
storefrontApiVersion: env.PUBLIC_STOREFRONT_API_VERSION || '2023-01',
storefrontId: env.PUBLIC_STOREFRONT_ID,
requestGroupId: request.headers.get('request-id'),
storefrontHeaders: getStorefrontHeaders(request),
});

/**
Expand Down
5 changes: 2 additions & 3 deletions templates/hello-world/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as remixBuild from '@remix-run/dev/server-build';
import {createStorefrontClient, storefrontRedirect} from '@shopify/hydrogen';
import {
createRequestHandler,
getBuyerIp,
getStorefrontHeaders,
createCookieSessionStorage,
type SessionStorage,
type Session,
Expand Down Expand Up @@ -38,14 +38,13 @@ export default {
const {storefront} = createStorefrontClient({
cache,
waitUntil,
buyerIp: getBuyerIp(request),
i18n: {language: 'EN', country: 'US'},
publicStorefrontToken: env.PUBLIC_STOREFRONT_API_TOKEN,
privateStorefrontToken: env.PRIVATE_STOREFRONT_API_TOKEN,
storeDomain: `https://${env.PUBLIC_STORE_DOMAIN}`,
storefrontApiVersion: env.PUBLIC_STOREFRONT_API_VERSION || '2023-01',
storefrontId: env.PUBLIC_STOREFRONT_ID,
requestGroupId: request.headers.get('request-id'),
storefrontHeaders: getStorefrontHeaders(request),
});

/**
Expand Down
5 changes: 2 additions & 3 deletions templates/skeleton/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as remixBuild from '@remix-run/dev/server-build';
import {createStorefrontClient, storefrontRedirect} from '@shopify/hydrogen';
import {
createRequestHandler,
getBuyerIp,
getStorefrontHeaders,
createCookieSessionStorage,
type SessionStorage,
type Session,
Expand Down Expand Up @@ -38,14 +38,13 @@ export default {
const {storefront} = createStorefrontClient({
cache,
waitUntil,
buyerIp: getBuyerIp(request),
i18n: {language: 'EN', country: 'US'},
publicStorefrontToken: env.PUBLIC_STOREFRONT_API_TOKEN,
privateStorefrontToken: env.PRIVATE_STOREFRONT_API_TOKEN,
storeDomain: `https://${env.PUBLIC_STORE_DOMAIN}`,
storefrontApiVersion: env.PUBLIC_STOREFRONT_API_VERSION || '2023-01',
storefrontId: env.PUBLIC_STOREFRONT_ID,
requestGroupId: request.headers.get('request-id'),
storefrontHeaders: getStorefrontHeaders(request),
});

/**
Expand Down