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 5 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
30 changes: 30 additions & 0 deletions .changeset/weak-sheep-sing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
'@shopify/hydrogen': patch
---

Fix active cart session event in Live View

In order for active cart to work, make sure to pass `request` into `createStorefrontClient`

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

const {storefront} = createStorefrontClient({
+ request,
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'),
});
```
16 changes: 15 additions & 1 deletion packages/hydrogen/src/storefront.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
createStorefrontClient as createStorefrontUtilities,
getShopifyCookies,
type StorefrontApiResponseOk,
} from '@shopify/hydrogen-react';
import type {ExecutionArgs} from 'graphql';
Expand Down Expand Up @@ -73,6 +74,7 @@ export type Storefront<TI18n extends I18nBase = I18nBase> = {
export type CreateStorefrontClientOptions<TI18n extends I18nBase> = Parameters<
typeof createStorefrontUtilities
>[0] & {
request?: Request;
wizardlyhel marked this conversation as resolved.
Show resolved Hide resolved
cache?: Cache;
buyerIp?: string;
requestGroupId?: string | null;
Expand Down Expand Up @@ -116,9 +118,12 @@ function minifyQuery(string: string) {
.trim();
}

const SHOPIFY_Y = '_shopify_y';
const SHOPIFY_S = '_shopify_s';
wizardlyhel marked this conversation as resolved.
Show resolved Hide resolved
const defaultI18n: I18nBase = {language: 'EN', country: 'US'};

export function createStorefrontClient<TI18n extends I18nBase>({
request,
cache,
waitUntil,
buyerIp,
Expand Down Expand Up @@ -153,6 +158,15 @@ export function createStorefrontClient<TI18n extends I18nBase>({
if (storefrontId) defaultHeaders[STOREFRONT_ID_HEADER] = storefrontId;
if (LIB_VERSION) defaultHeaders['user-agent'] = `Hydrogen ${LIB_VERSION}`;

if (request && request.headers.has('cookie')) {
const cookies = getShopifyCookies(request.headers.get('cookie') || '');

if (cookies[SHOPIFY_Y])
defaultHeaders['Shopify-Storefront-Y'] = cookies[SHOPIFY_Y];
if (cookies[SHOPIFY_S])
defaultHeaders['Shopify-Storefront-S'] = cookies[SHOPIFY_S];
}

async function fetchStorefrontApi<T>({
query,
mutation,
Expand Down Expand Up @@ -183,7 +197,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 templates/demo-store/.env
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
SESSION_SECRET="foobar"
PUBLIC_STOREFRONT_API_TOKEN="3b580e70970c4528da70c98e097c2fa0"
PUBLIC_STOREFRONT_API_VERSION="2023-01"
PUBLIC_STORE_DOMAIN="hydrogen-preview.myshopify.com"
PUBLIC_STORE_DOMAIN="checkout.hydrogen.shop"
wizardlyhel marked this conversation as resolved.
Show resolved Hide resolved
wizardlyhel marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions templates/demo-store/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export default {
* Create Hydrogen's Storefront client.
*/
const {storefront} = createStorefrontClient({
request,
cache,
waitUntil,
buyerIp: getBuyerIp(request),
Expand Down
1 change: 1 addition & 0 deletions templates/hello-world/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export default {
* Create Hydrogen's Storefront client.
*/
const {storefront} = createStorefrontClient({
request,
cache,
waitUntil,
buyerIp: getBuyerIp(request),
Expand Down
1 change: 1 addition & 0 deletions templates/skeleton/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export default {
* Create Hydrogen's Storefront client.
*/
const {storefront} = createStorefrontClient({
request,
cache,
waitUntil,
buyerIp: getBuyerIp(request),
Expand Down