@shopify/cli-hydrogen 6.1.1 → 7.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (100) hide show
  1. package/dist/commands/hydrogen/build.js +40 -78
  2. package/dist/commands/hydrogen/codegen.js +8 -3
  3. package/dist/commands/hydrogen/deploy.js +311 -21
  4. package/dist/commands/hydrogen/deploy.test.js +410 -0
  5. package/dist/commands/hydrogen/dev.js +30 -15
  6. package/dist/commands/hydrogen/init.js +1 -1
  7. package/dist/commands/hydrogen/init.test.js +155 -53
  8. package/dist/commands/hydrogen/link.js +5 -21
  9. package/dist/commands/hydrogen/link.test.js +10 -10
  10. package/dist/commands/hydrogen/preview.js +7 -6
  11. package/dist/commands/hydrogen/setup.js +17 -23
  12. package/dist/commands/hydrogen/setup.test.js +0 -1
  13. package/dist/commands/hydrogen/upgrade.js +15 -0
  14. package/dist/generator-templates/starter/.graphqlrc.yml +12 -1
  15. package/dist/generator-templates/starter/CHANGELOG.md +66 -0
  16. package/dist/generator-templates/starter/README.md +23 -0
  17. package/dist/generator-templates/starter/app/components/Cart.tsx +1 -1
  18. package/dist/generator-templates/starter/app/components/Header.tsx +5 -1
  19. package/dist/generator-templates/starter/app/components/Layout.tsx +1 -1
  20. package/dist/generator-templates/starter/app/components/Search.tsx +1 -1
  21. package/dist/generator-templates/starter/app/graphql/customer-account/CustomerAddressMutations.ts +61 -0
  22. package/dist/generator-templates/starter/app/graphql/customer-account/CustomerDetailsQuery.ts +39 -0
  23. package/dist/generator-templates/starter/app/graphql/customer-account/CustomerOrderQuery.ts +87 -0
  24. package/dist/generator-templates/starter/app/graphql/customer-account/CustomerOrdersQuery.ts +58 -0
  25. package/dist/generator-templates/starter/app/graphql/customer-account/CustomerUpdateMutation.ts +24 -0
  26. package/dist/generator-templates/starter/app/lib/fragments.ts +102 -0
  27. package/dist/generator-templates/starter/app/lib/session.ts +67 -0
  28. package/dist/generator-templates/starter/app/root.tsx +11 -45
  29. package/dist/generator-templates/starter/app/routes/account.$.tsx +8 -4
  30. package/dist/generator-templates/starter/app/routes/account._index.tsx +5 -0
  31. package/dist/generator-templates/starter/app/routes/account.addresses.tsx +215 -206
  32. package/dist/generator-templates/starter/app/routes/account.orders.$id.tsx +56 -163
  33. package/dist/generator-templates/starter/app/routes/account.orders._index.tsx +32 -109
  34. package/dist/generator-templates/starter/app/routes/account.profile.tsx +40 -180
  35. package/dist/generator-templates/starter/app/routes/account.tsx +20 -135
  36. package/dist/generator-templates/starter/app/routes/account_.authorize.tsx +5 -0
  37. package/dist/generator-templates/starter/app/routes/account_.login.tsx +3 -140
  38. package/dist/generator-templates/starter/app/routes/account_.logout.tsx +5 -24
  39. package/dist/generator-templates/starter/app/routes/cart.tsx +7 -5
  40. package/dist/generator-templates/starter/app/routes/collections.$handle.tsx +1 -1
  41. package/dist/generator-templates/starter/app/routes/products.$handle.tsx +2 -2
  42. package/dist/generator-templates/starter/app/routes/search.tsx +1 -1
  43. package/dist/generator-templates/starter/customer-accountapi.generated.d.ts +506 -0
  44. package/dist/generator-templates/starter/package.json +11 -11
  45. package/dist/generator-templates/starter/remix.config.js +4 -0
  46. package/dist/generator-templates/starter/remix.env.d.ts +8 -11
  47. package/dist/generator-templates/starter/server.ts +24 -167
  48. package/dist/generator-templates/starter/storefrontapi.generated.d.ts +104 -881
  49. package/dist/hooks/init.js +4 -4
  50. package/dist/lib/auth.js +5 -10
  51. package/dist/lib/build.js +6 -1
  52. package/dist/lib/bundle/analyzer.js +36 -26
  53. package/dist/lib/codegen.js +59 -26
  54. package/dist/lib/defer.js +12 -0
  55. package/dist/lib/file.js +55 -3
  56. package/dist/lib/flags.js +15 -8
  57. package/dist/lib/get-oxygen-deployment-data.test.js +4 -2
  58. package/dist/lib/graphiql-url.js +3 -0
  59. package/dist/lib/graphql/admin/client.test.js +2 -2
  60. package/dist/lib/graphql/admin/get-oxygen-data.js +1 -0
  61. package/dist/lib/log.js +31 -14
  62. package/dist/lib/mini-oxygen/assets.js +17 -1
  63. package/dist/lib/mini-oxygen/index.js +4 -5
  64. package/dist/lib/mini-oxygen/mini-oxygen.test.js +214 -0
  65. package/dist/lib/mini-oxygen/node.js +4 -2
  66. package/dist/lib/mini-oxygen/workerd-inspector-logs.js +2 -2
  67. package/dist/lib/mini-oxygen/workerd.js +27 -10
  68. package/dist/lib/missing-routes.js +6 -3
  69. package/dist/lib/onboarding/common.js +44 -12
  70. package/dist/lib/onboarding/local.js +26 -18
  71. package/dist/lib/onboarding/remote.js +50 -29
  72. package/dist/lib/request-events.js +65 -31
  73. package/dist/lib/setups/css/assets.js +1 -46
  74. package/dist/lib/setups/css/css-modules.js +3 -2
  75. package/dist/lib/setups/css/postcss.js +4 -2
  76. package/dist/lib/setups/css/tailwind.js +4 -2
  77. package/dist/lib/setups/css/vanilla-extract.js +3 -2
  78. package/dist/lib/setups/i18n/replacers.test.js +54 -38
  79. package/dist/lib/shell.js +1 -1
  80. package/dist/lib/template-diff.js +99 -0
  81. package/dist/lib/template-downloader.js +3 -2
  82. package/dist/lib/transpile/project.js +1 -1
  83. package/dist/virtual-routes/assets/debug-network.css +592 -0
  84. package/dist/virtual-routes/assets/favicon-dark.svg +20 -0
  85. package/dist/virtual-routes/components/FlameChartWrapper.jsx +8 -10
  86. package/dist/virtual-routes/components/IconClose.jsx +38 -0
  87. package/dist/virtual-routes/components/IconDiscard.jsx +44 -0
  88. package/dist/virtual-routes/components/RequestDetails.jsx +179 -0
  89. package/dist/virtual-routes/components/RequestTable.jsx +92 -0
  90. package/dist/virtual-routes/components/RequestWaterfall.jsx +151 -0
  91. package/dist/virtual-routes/lib/useDebugNetworkServer.jsx +176 -0
  92. package/dist/virtual-routes/routes/subrequest-profiler.jsx +243 -0
  93. package/oclif.manifest.json +54 -61
  94. package/package.json +15 -12
  95. package/dist/generator-templates/starter/app/routes/account_.activate.$id.$activationToken.tsx +0 -161
  96. package/dist/generator-templates/starter/app/routes/account_.recover.tsx +0 -129
  97. package/dist/generator-templates/starter/app/routes/account_.register.tsx +0 -207
  98. package/dist/generator-templates/starter/app/routes/account_.reset.$id.$resetToken.tsx +0 -136
  99. package/dist/virtual-routes/routes/debug-network.jsx +0 -289
  100. /package/dist/generator-templates/starter/app/{utils.ts → lib/variants.ts} +0 -0
@@ -1,5 +1,71 @@
1
1
  # skeleton
2
2
 
3
+ ## 1.0.3
4
+
5
+ ### Patch Changes
6
+
7
+ - ♻️ `CustomerClient` type is deprecated and replaced by `CustomerAccount` ([#1692](https://github.com/Shopify/hydrogen/pull/1692)) by [@michenly](https://github.com/michenly)
8
+
9
+ - Updated dependencies [[`02798786`](https://github.com/Shopify/hydrogen/commit/02798786bf8ae5c53f6430723a86d62b8e94d120), [`52b15df4`](https://github.com/Shopify/hydrogen/commit/52b15df457ce723bbc83ad594ded73a7b06447d6), [`a2664362`](https://github.com/Shopify/hydrogen/commit/a2664362a7d89b34835553a9b0eb7af55ca70ae4), [`eee5d927`](https://github.com/Shopify/hydrogen/commit/eee5d9274b72404dfb0ffef30d5503fd553be5fe), [`c7b2017f`](https://github.com/Shopify/hydrogen/commit/c7b2017f11a2cb4d280dfd8f170e65a908b9ea02), [`06320ee4`](https://github.com/Shopify/hydrogen/commit/06320ee48b94dbfece945461031a252f454fd0a3)]:
10
+ - @shopify/hydrogen@2024.1.1
11
+ - @shopify/cli-hydrogen@7.0.1
12
+
13
+ ## 1.0.2
14
+
15
+ ### Patch Changes
16
+
17
+ - Use new parameters introduced in Storefront API v2024-01 to fix redirection to the product's default variant when there are unknown query params in the URL. ([#1642](https://github.com/Shopify/hydrogen/pull/1642)) by [@wizardlyhel](https://github.com/wizardlyhel)
18
+
19
+ ```diff
20
+ - selectedVariant: variantBySelectedOptions(selectedOptions: $selectedOptions) {
21
+ + selectedVariant: variantBySelectedOptions(selectedOptions: $selectedOptions, ignoreUnknownOptions: true, caseInsensitiveMatch: true) {
22
+ ...ProductVariant
23
+ }
24
+ ```
25
+
26
+ - Update the GraphQL config in `.graphqlrc.yml` to use the more modern `projects` structure: ([#1577](https://github.com/Shopify/hydrogen/pull/1577)) by [@frandiox](https://github.com/frandiox)
27
+
28
+ ```diff
29
+ -schema: node_modules/@shopify/hydrogen/storefront.schema.json
30
+ +projects:
31
+ + default:
32
+ + schema: 'node_modules/@shopify/hydrogen/storefront.schema.json'
33
+ ```
34
+
35
+ This allows you to add additional projects to the GraphQL config, such as third party CMS schemas.
36
+
37
+ Also, you can modify the document paths used for the Storefront API queries. This is useful if you have a large codebase and want to exclude certain files from being used for codegen or other GraphQL utilities:
38
+
39
+ ```yaml
40
+ projects:
41
+ default:
42
+ schema: 'node_modules/@shopify/hydrogen/storefront.schema.json'
43
+ documents:
44
+ - '!*.d.ts'
45
+ - '*.{ts,tsx,js,jsx}'
46
+ - 'app/**/*.{ts,tsx,js,jsx}'
47
+ ```
48
+
49
+ - Improve resiliency of `HydrogenSession` ([#1583](https://github.com/Shopify/hydrogen/pull/1583)) by [@blittle](https://github.com/blittle)
50
+
51
+ - Update `@shopify/cli` dependency in `package.json`: ([#1579](https://github.com/Shopify/hydrogen/pull/1579)) by [@frandiox](https://github.com/frandiox)
52
+
53
+ ```diff
54
+ - "@shopify/cli": "3.51.0",
55
+ + "@shopify/cli": "3.52.0",
56
+ ```
57
+
58
+ - - Update example and template Remix versions to `^2.5.1` ([#1639](https://github.com/Shopify/hydrogen/pull/1639)) by [@wizardlyhel](https://github.com/wizardlyhel)
59
+
60
+ - Enable Remix future flags:
61
+ - [`v3_fetcherPersist`](https://remix.run/docs/en/main/hooks/use-fetchers#additional-resources)
62
+ - [`v3_relativeSplatpath`](https://remix.run/docs/en/main/hooks/use-resolved-path#splat-paths)
63
+
64
+ - Updated dependencies [[`810f48cf`](https://github.com/Shopify/hydrogen/commit/810f48cf5d55f0cfcac6e01fe481db8c76e77cd2), [`8c477cb5`](https://github.com/Shopify/hydrogen/commit/8c477cb565c3e018bf4e13bad01804c21611fb8a), [`42ac4138`](https://github.com/Shopify/hydrogen/commit/42ac4138553c7e1a438b075c4f9cb781edffebc4), [`0241b7d2`](https://github.com/Shopify/hydrogen/commit/0241b7d2dcb887d259ce9033aca356d391bc07df), [`6a897586`](https://github.com/Shopify/hydrogen/commit/6a897586bd0908db90736921d11e4b6bdf29c912), [`0ff63bed`](https://github.com/Shopify/hydrogen/commit/0ff63bed840f5b8a5eb9968b67bd9a5a57099253), [`6bc1d61c`](https://github.com/Shopify/hydrogen/commit/6bc1d61c17a9c9be13f52338d2ab940e64e73495), [`eb0f4bcc`](https://github.com/Shopify/hydrogen/commit/eb0f4bccb57966a00ecb2b88d17dd694599da340), [`400bfee6`](https://github.com/Shopify/hydrogen/commit/400bfee6836a51c6ab5e4804e8b1e9ad48856dcb), [`a69c21ca`](https://github.com/Shopify/hydrogen/commit/a69c21caa15dfedb88afd50f262f17bf86f74836), [`970073e7`](https://github.com/Shopify/hydrogen/commit/970073e78258880505e0de563136b5379d5d24af), [`772118ca`](https://github.com/Shopify/hydrogen/commit/772118ca6aefbd47841fffc6ce42856c2dc779bd), [`335375a6`](https://github.com/Shopify/hydrogen/commit/335375a6b1a512f70e169a82bc87a8392dc8c92c), [`335371ce`](https://github.com/Shopify/hydrogen/commit/335371ceb6e1bd5aebb6104f131d3f22798a245f), [`94509b75`](https://github.com/Shopify/hydrogen/commit/94509b750afefd686971198ed86277e2c70f3176), [`36d6fa2c`](https://github.com/Shopify/hydrogen/commit/36d6fa2c4fa54ff79f06ef17aa41f60478977bc0), [`3e7b6e8a`](https://github.com/Shopify/hydrogen/commit/3e7b6e8a3bf66bad7fc0f9c224f1c163dbe3e288), [`cce65795`](https://github.com/Shopify/hydrogen/commit/cce6579580f849bec9a28cf575f7130ba3627f6b), [`9e3d88d4`](https://github.com/Shopify/hydrogen/commit/9e3d88d498efaa20fe23de9837e0f444180bc787), [`ca1161b2`](https://github.com/Shopify/hydrogen/commit/ca1161b29ad7b4d0838953782fb114d5fe82193a), [`92840e51`](https://github.com/Shopify/hydrogen/commit/92840e51820e5c7822f731affd3f591c0099be10), [`952fedf2`](https://github.com/Shopify/hydrogen/commit/952fedf27b869164550954d1c15f53b32ec02675), [`1bc053c9`](https://github.com/Shopify/hydrogen/commit/1bc053c94ba1be14ddc28be9eb70be7219b295d1)]:
65
+ - @shopify/hydrogen@2024.1.0
66
+ - @shopify/cli-hydrogen@7.0.0
67
+ - @shopify/remix-oxygen@2.0.3
68
+
3
69
  ## 1.0.1
4
70
 
5
71
  ### Patch Changes
@@ -38,3 +38,26 @@ npm run build
38
38
  ```bash
39
39
  npm run dev
40
40
  ```
41
+
42
+ ## Setup for using Customer Account API (`/account` section)
43
+
44
+ ### Setup public domain using ngrok
45
+
46
+ 1. Setup a [ngrok](https://ngrok.com/) account and add a permanent domain (ie. `https://<your-ngrok-domain>.app`).
47
+ 1. Install the [ngrok CLI](https://ngrok.com/download) to use in terminal
48
+ 1. Start ngrok using `ngrok http --domain=<your-ngrok-domain>.app 3000`
49
+
50
+ ### Include public domain in Customer Account API settings
51
+
52
+ 1. Go to your Shopify admin => `Hydrogen` or `Headless` app/channel => Customer Account API => Application setup
53
+ 1. Edit `Callback URI(s)` to include `https://<your-ngrok-domain>.app/account/authorize`
54
+ 1. Edit `Javascript origin(s)` to include your public domain `https://<your-ngrok-domain>.app` or keep it blank
55
+ 1. Edit `Logout URI` to include your public domain `https://<your-ngrok-domain>.app` or keep it blank
56
+
57
+ ### Prepare Environment variables
58
+
59
+ Run [`npx shopify hydrogen link`](https://shopify.dev/docs/custom-storefronts/hydrogen/cli#link) or [`npx shopify hydrogen env pull`](https://shopify.dev/docs/custom-storefronts/hydrogen/cli#env-pull) to link this app to your own test shop.
60
+
61
+ Alternatly, the values of the required environment varaibles "PUBLIC_CUSTOMER_ACCOUNT_API_CLIENT_ID" and "PUBLIC_CUSTOMER_ACCOUNT_API_URL" can be found in customer account api settings in the Hydrogen admin channel.
62
+
63
+ 🗒️ Note that mock.shop doesn't supply these variables automatically.
@@ -2,7 +2,7 @@ import {CartForm, Image, Money} from '@shopify/hydrogen';
2
2
  import type {CartLineUpdateInput} from '@shopify/hydrogen/storefront-api-types';
3
3
  import {Link} from '@remix-run/react';
4
4
  import type {CartApiQueryFragment} from 'storefrontapi.generated';
5
- import {useVariantUrl} from '~/utils';
5
+ import {useVariantUrl} from '~/lib/variants';
6
6
 
7
7
  type CartLine = CartApiQueryFragment['lines']['nodes'][0];
8
8
 
@@ -93,7 +93,11 @@ function HeaderCtas({
93
93
  <nav className="header-ctas" role="navigation">
94
94
  <HeaderMenuMobileToggle />
95
95
  <NavLink prefetch="intent" to="/account" style={activeLinkStyle}>
96
- {isLoggedIn ? 'Account' : 'Sign in'}
96
+ <Suspense fallback="Sign in">
97
+ <Await resolve={isLoggedIn} errorElement="Sign in">
98
+ {(isLoggedIn) => (isLoggedIn ? 'Account' : 'Sign in')}
99
+ </Await>
100
+ </Suspense>
97
101
  </NavLink>
98
102
  <SearchToggle />
99
103
  <CartToggle cart={cart} />
@@ -19,7 +19,7 @@ export type LayoutProps = {
19
19
  children?: React.ReactNode;
20
20
  footer: Promise<FooterQuery>;
21
21
  header: HeaderQuery;
22
- isLoggedIn: boolean;
22
+ isLoggedIn: Promise<boolean>;
23
23
  };
24
24
 
25
25
  export function Layout({
@@ -211,7 +211,7 @@ function SearchResultArticleGrid({articles}: Pick<SearchQuery, 'articles'>) {
211
211
  <div>
212
212
  {articles?.nodes?.map((article) => (
213
213
  <div className="search-results-item" key={article.id}>
214
- <Link prefetch="intent" to={`/blog/${article.handle}`}>
214
+ <Link prefetch="intent" to={`/blogs/${article.handle}`}>
215
215
  {article.title}
216
216
  </Link>
217
217
  </div>
@@ -0,0 +1,61 @@
1
+ // NOTE: https://shopify.dev/docs/api/customer/latest/mutations/customerAddressUpdate
2
+ export const UPDATE_ADDRESS_MUTATION = `#graphql
3
+ mutation customerAddressUpdate(
4
+ $address: CustomerAddressInput!
5
+ $addressId: ID!
6
+ $defaultAddress: Boolean
7
+ ) {
8
+ customerAddressUpdate(
9
+ address: $address
10
+ addressId: $addressId
11
+ defaultAddress: $defaultAddress
12
+ ) {
13
+ customerAddress {
14
+ id
15
+ }
16
+ userErrors {
17
+ code
18
+ field
19
+ message
20
+ }
21
+ }
22
+ }
23
+ ` as const;
24
+
25
+ // NOTE: https://shopify.dev/docs/api/customer/latest/mutations/customerAddressDelete
26
+ export const DELETE_ADDRESS_MUTATION = `#graphql
27
+ mutation customerAddressDelete(
28
+ $addressId: ID!,
29
+ ) {
30
+ customerAddressDelete(addressId: $addressId) {
31
+ deletedAddressId
32
+ userErrors {
33
+ code
34
+ field
35
+ message
36
+ }
37
+ }
38
+ }
39
+ ` as const;
40
+
41
+ // NOTE: https://shopify.dev/docs/api/customer/latest/mutations/customerAddressCreate
42
+ export const CREATE_ADDRESS_MUTATION = `#graphql
43
+ mutation customerAddressCreate(
44
+ $address: CustomerAddressInput!
45
+ $defaultAddress: Boolean
46
+ ) {
47
+ customerAddressCreate(
48
+ address: $address
49
+ defaultAddress: $defaultAddress
50
+ ) {
51
+ customerAddress {
52
+ id
53
+ }
54
+ userErrors {
55
+ code
56
+ field
57
+ message
58
+ }
59
+ }
60
+ }
61
+ ` as const;
@@ -0,0 +1,39 @@
1
+ // NOTE: https://shopify.dev/docs/api/customer/latest/objects/Customer
2
+ export const CUSTOMER_FRAGMENT = `#graphql
3
+ fragment Customer on Customer {
4
+ firstName
5
+ lastName
6
+ defaultAddress {
7
+ ...Address
8
+ }
9
+ addresses(first: 6) {
10
+ nodes {
11
+ ...Address
12
+ }
13
+ }
14
+ }
15
+ fragment Address on CustomerAddress {
16
+ id
17
+ formatted
18
+ firstName
19
+ lastName
20
+ company
21
+ address1
22
+ address2
23
+ territoryCode
24
+ zoneCode
25
+ city
26
+ zip
27
+ phoneNumber
28
+ }
29
+ ` as const;
30
+
31
+ // NOTE: https://shopify.dev/docs/api/customer/latest/queries/customer
32
+ export const CUSTOMER_DETAILS_QUERY = `#graphql
33
+ query CustomerDetails {
34
+ customer {
35
+ ...Customer
36
+ }
37
+ }
38
+ ${CUSTOMER_FRAGMENT}
39
+ ` as const;
@@ -0,0 +1,87 @@
1
+ // NOTE: https://shopify.dev/docs/api/customer/latest/queries/order
2
+ export const CUSTOMER_ORDER_QUERY = `#graphql
3
+ fragment OrderMoney on MoneyV2 {
4
+ amount
5
+ currencyCode
6
+ }
7
+ fragment DiscountApplication on DiscountApplication {
8
+ value {
9
+ __typename
10
+ ... on MoneyV2 {
11
+ ...OrderMoney
12
+ }
13
+ ... on PricingPercentageValue {
14
+ percentage
15
+ }
16
+ }
17
+ }
18
+ fragment OrderLineItemFull on LineItem {
19
+ id
20
+ title
21
+ quantity
22
+ price {
23
+ ...OrderMoney
24
+ }
25
+ discountAllocations {
26
+ allocatedAmount {
27
+ ...OrderMoney
28
+ }
29
+ discountApplication {
30
+ ...DiscountApplication
31
+ }
32
+ }
33
+ totalDiscount {
34
+ ...OrderMoney
35
+ }
36
+ image {
37
+ altText
38
+ height
39
+ url
40
+ id
41
+ width
42
+ }
43
+ variantTitle
44
+ }
45
+ fragment Order on Order {
46
+ id
47
+ name
48
+ statusPageUrl
49
+ processedAt
50
+ fulfillments(first: 1) {
51
+ nodes {
52
+ status
53
+ }
54
+ }
55
+ totalTax {
56
+ ...OrderMoney
57
+ }
58
+ totalPrice {
59
+ ...OrderMoney
60
+ }
61
+ subtotal {
62
+ ...OrderMoney
63
+ }
64
+ shippingAddress {
65
+ name
66
+ formatted(withName: true)
67
+ formattedArea
68
+ }
69
+ discountApplications(first: 100) {
70
+ nodes {
71
+ ...DiscountApplication
72
+ }
73
+ }
74
+ lineItems(first: 100) {
75
+ nodes {
76
+ ...OrderLineItemFull
77
+ }
78
+ }
79
+ }
80
+ query Order($orderId: ID!) {
81
+ order(id: $orderId) {
82
+ ... on Order {
83
+ ...Order
84
+ }
85
+ }
86
+ }
87
+ ` as const;
@@ -0,0 +1,58 @@
1
+ // https://shopify.dev/docs/api/customer/latest/objects/Order
2
+ export const ORDER_ITEM_FRAGMENT = `#graphql
3
+ fragment OrderItem on Order {
4
+ totalPrice {
5
+ amount
6
+ currencyCode
7
+ }
8
+ financialStatus
9
+ fulfillments(first: 1) {
10
+ nodes {
11
+ status
12
+ }
13
+ }
14
+ id
15
+ number
16
+ processedAt
17
+ }
18
+ ` as const;
19
+
20
+ // https://shopify.dev/docs/api/customer/latest/objects/Customer
21
+ export const CUSTOMER_ORDERS_FRAGMENT = `#graphql
22
+ fragment CustomerOrders on Customer {
23
+ orders(
24
+ sortKey: PROCESSED_AT,
25
+ reverse: true,
26
+ first: $first,
27
+ last: $last,
28
+ before: $startCursor,
29
+ after: $endCursor
30
+ ) {
31
+ nodes {
32
+ ...OrderItem
33
+ }
34
+ pageInfo {
35
+ hasPreviousPage
36
+ hasNextPage
37
+ endCursor
38
+ startCursor
39
+ }
40
+ }
41
+ }
42
+ ${ORDER_ITEM_FRAGMENT}
43
+ ` as const;
44
+
45
+ // https://shopify.dev/docs/api/customer/latest/queries/customer
46
+ export const CUSTOMER_ORDERS_QUERY = `#graphql
47
+ ${CUSTOMER_ORDERS_FRAGMENT}
48
+ query CustomerOrders(
49
+ $endCursor: String
50
+ $first: Int
51
+ $last: Int
52
+ $startCursor: String
53
+ ) {
54
+ customer {
55
+ ...CustomerOrders
56
+ }
57
+ }
58
+ ` as const;
@@ -0,0 +1,24 @@
1
+ export const CUSTOMER_UPDATE_MUTATION = `#graphql
2
+ # https://shopify.dev/docs/api/customer/latest/mutations/customerUpdate
3
+ mutation customerUpdate(
4
+ $customer: CustomerUpdateInput!
5
+ ){
6
+ customerUpdate(input: $customer) {
7
+ customer {
8
+ firstName
9
+ lastName
10
+ emailAddress {
11
+ emailAddress
12
+ }
13
+ phoneNumber {
14
+ phoneNumber
15
+ }
16
+ }
17
+ userErrors {
18
+ code
19
+ field
20
+ message
21
+ }
22
+ }
23
+ }
24
+ ` as const;
@@ -0,0 +1,102 @@
1
+ // NOTE: https://shopify.dev/docs/api/storefront/latest/queries/cart
2
+ export const CART_QUERY_FRAGMENT = `#graphql
3
+ fragment Money on MoneyV2 {
4
+ currencyCode
5
+ amount
6
+ }
7
+ fragment CartLine on CartLine {
8
+ id
9
+ quantity
10
+ attributes {
11
+ key
12
+ value
13
+ }
14
+ cost {
15
+ totalAmount {
16
+ ...Money
17
+ }
18
+ amountPerQuantity {
19
+ ...Money
20
+ }
21
+ compareAtAmountPerQuantity {
22
+ ...Money
23
+ }
24
+ }
25
+ merchandise {
26
+ ... on ProductVariant {
27
+ id
28
+ availableForSale
29
+ compareAtPrice {
30
+ ...Money
31
+ }
32
+ price {
33
+ ...Money
34
+ }
35
+ requiresShipping
36
+ title
37
+ image {
38
+ id
39
+ url
40
+ altText
41
+ width
42
+ height
43
+
44
+ }
45
+ product {
46
+ handle
47
+ title
48
+ id
49
+ }
50
+ selectedOptions {
51
+ name
52
+ value
53
+ }
54
+ }
55
+ }
56
+ }
57
+ fragment CartApiQuery on Cart {
58
+ id
59
+ checkoutUrl
60
+ totalQuantity
61
+ buyerIdentity {
62
+ countryCode
63
+ customer {
64
+ id
65
+ email
66
+ firstName
67
+ lastName
68
+ displayName
69
+ }
70
+ email
71
+ phone
72
+ }
73
+ lines(first: $numCartLines) {
74
+ nodes {
75
+ ...CartLine
76
+ }
77
+ }
78
+ cost {
79
+ subtotalAmount {
80
+ ...Money
81
+ }
82
+ totalAmount {
83
+ ...Money
84
+ }
85
+ totalDutyAmount {
86
+ ...Money
87
+ }
88
+ totalTaxAmount {
89
+ ...Money
90
+ }
91
+ }
92
+ note
93
+ attributes {
94
+ key
95
+ value
96
+ }
97
+ discountCodes {
98
+ code
99
+ applicable
100
+ }
101
+ }
102
+ ` as const;
@@ -0,0 +1,67 @@
1
+ import type {HydrogenSession} from '@shopify/hydrogen';
2
+ import {
3
+ createCookieSessionStorage,
4
+ type SessionStorage,
5
+ type Session,
6
+ } from '@shopify/remix-oxygen';
7
+
8
+ /**
9
+ * This is a custom session implementation for your Hydrogen shop.
10
+ * Feel free to customize it to your needs, add helper methods, or
11
+ * swap out the cookie-based implementation with something else!
12
+ */
13
+ export class AppSession implements HydrogenSession {
14
+ #sessionStorage;
15
+ #session;
16
+
17
+ constructor(sessionStorage: SessionStorage, session: Session) {
18
+ this.#sessionStorage = sessionStorage;
19
+ this.#session = session;
20
+ }
21
+
22
+ static async init(request: Request, secrets: string[]) {
23
+ const storage = createCookieSessionStorage({
24
+ cookie: {
25
+ name: 'session',
26
+ httpOnly: true,
27
+ path: '/',
28
+ sameSite: 'lax',
29
+ secrets,
30
+ },
31
+ });
32
+
33
+ const session = await storage
34
+ .getSession(request.headers.get('Cookie'))
35
+ .catch(() => storage.getSession());
36
+
37
+ return new this(storage, session);
38
+ }
39
+
40
+ get has() {
41
+ return this.#session.has;
42
+ }
43
+
44
+ get get() {
45
+ return this.#session.get;
46
+ }
47
+
48
+ get flash() {
49
+ return this.#session.flash;
50
+ }
51
+
52
+ get unset() {
53
+ return this.#session.unset;
54
+ }
55
+
56
+ get set() {
57
+ return this.#session.set;
58
+ }
59
+
60
+ destroy() {
61
+ return this.#sessionStorage.destroySession(this.#session);
62
+ }
63
+
64
+ commit() {
65
+ return this.#sessionStorage.commitSession(this.#session);
66
+ }
67
+ }
@@ -60,21 +60,19 @@ export function links() {
60
60
  ];
61
61
  }
62
62
 
63
+ /**
64
+ * Access the result of the root loader from a React component.
65
+ */
63
66
  export const useRootLoaderData = () => {
64
67
  const [root] = useMatches();
65
68
  return root?.data as SerializeFrom<typeof loader>;
66
69
  };
67
70
 
68
71
  export async function loader({context}: LoaderFunctionArgs) {
69
- const {storefront, session, cart} = context;
70
- const customerAccessToken = await session.get('customerAccessToken');
72
+ const {storefront, customerAccount, cart} = context;
71
73
  const publicStoreDomain = context.env.PUBLIC_STORE_DOMAIN;
72
74
 
73
- // validate the customer access token is valid
74
- const {isLoggedIn, headers} = await validateCustomerAccessToken(
75
- session,
76
- customerAccessToken,
77
- );
75
+ const isLoggedInPromise = customerAccount.isLoggedIn();
78
76
 
79
77
  // defer the cart query by not awaiting it
80
78
  const cartPromise = cart.get();
@@ -100,10 +98,14 @@ export async function loader({context}: LoaderFunctionArgs) {
100
98
  cart: cartPromise,
101
99
  footer: footerPromise,
102
100
  header: await headerPromise,
103
- isLoggedIn,
101
+ isLoggedIn: isLoggedInPromise,
104
102
  publicStoreDomain,
105
103
  },
106
- {headers},
104
+ {
105
+ headers: {
106
+ 'Set-Cookie': await context.session.commit(),
107
+ },
108
+ },
107
109
  );
108
110
  }
109
111
 
@@ -173,42 +175,6 @@ export function ErrorBoundary() {
173
175
  );
174
176
  }
175
177
 
176
- /**
177
- * Validates the customer access token and returns a boolean and headers
178
- * @see https://shopify.dev/docs/api/storefront/latest/objects/CustomerAccessToken
179
- *
180
- * @example
181
- * ```js
182
- * const {isLoggedIn, headers} = await validateCustomerAccessToken(
183
- * customerAccessToken,
184
- * session,
185
- * );
186
- * ```
187
- */
188
- async function validateCustomerAccessToken(
189
- session: LoaderFunctionArgs['context']['session'],
190
- customerAccessToken?: CustomerAccessToken,
191
- ) {
192
- let isLoggedIn = false;
193
- const headers = new Headers();
194
- if (!customerAccessToken?.accessToken || !customerAccessToken?.expiresAt) {
195
- return {isLoggedIn, headers};
196
- }
197
-
198
- const expiresAt = new Date(customerAccessToken.expiresAt).getTime();
199
- const dateNow = Date.now();
200
- const customerAccessTokenExpired = expiresAt < dateNow;
201
-
202
- if (customerAccessTokenExpired) {
203
- session.unset('customerAccessToken');
204
- headers.append('Set-Cookie', await session.commit());
205
- } else {
206
- isLoggedIn = true;
207
- }
208
-
209
- return {isLoggedIn, headers};
210
- }
211
-
212
178
  const MENU_FRAGMENT = `#graphql
213
179
  fragment MenuItem on MenuItem {
214
180
  id
@@ -1,8 +1,12 @@
1
1
  import {redirect, type LoaderFunctionArgs} from '@shopify/remix-oxygen';
2
2
 
3
+ // fallback wild card for all unauthenticated routes in account section
3
4
  export async function loader({context}: LoaderFunctionArgs) {
4
- if (await context.session.get('customerAccessToken')) {
5
- return redirect('/account');
6
- }
7
- return redirect('/account/login');
5
+ await context.customerAccount.handleAuthStatus();
6
+
7
+ return redirect('/account', {
8
+ headers: {
9
+ 'Set-Cookie': await context.session.commit(),
10
+ },
11
+ });
8
12
  }