/
($locale)._index.jsx
247 lines (226 loc) · 6.42 KB
/
($locale)._index.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import {defer} from '@shopify/remix-oxygen';
import {Suspense} from 'react';
import {Await, useLoaderData} from '@remix-run/react';
import {AnalyticsPageType} from '@shopify/hydrogen';
import {ProductSwimlane, FeaturedCollections, Hero} from '~/components';
import {MEDIA_FRAGMENT, PRODUCT_CARD_FRAGMENT} from '~/data/fragments';
import {getHeroPlaceholder} from '~/lib/placeholders';
import {seoPayload} from '~/lib/seo.server';
import {routeHeaders} from '~/data/cache';
import {choose} from '~/dyapi';
export const headers = routeHeaders;
export async function loader({params, request, context}) {
const {language, country} = context.storefront.i18n;
if (
params.locale &&
params.locale.toLowerCase() !== `${language}-${country}`.toLowerCase()
) {
// If the locale URL param is defined, yet we still are on `EN-US`
// the the locale param must be invalid, send to the 404 page
throw new Response(null, {status: 404});
}
const {shop, hero} = await context.storefront.query(HOMEPAGE_SEO_QUERY, {
variables: {handle: 'freestyle'},
});
const seo = seoPayload.home();
const pageContext = {type: 'HOMEPAGE'};
const apiResponse = await choose(request, context, pageContext, ['HP Hero']);
return defer({
shop,
primaryHero: apiResponse['HP Hero'] || hero,
// These different queries are separated to illustrate how 3rd party content
// fetching can be optimized for both above and below the fold.
featuredProducts: context.storefront.query(
HOMEPAGE_FEATURED_PRODUCTS_QUERY,
{
variables: {
/**
* Country and language properties are automatically injected
* into all queries. Passing them is unnecessary unless you
* want to override them from the following default:
*/
country,
language,
},
},
),
secondaryHero: context.storefront.query(COLLECTION_HERO_QUERY, {
variables: {
handle: 'backcountry',
country,
language,
},
}),
featuredCollections: context.storefront.query(FEATURED_COLLECTIONS_QUERY, {
variables: {
country,
language,
},
}),
tertiaryHero: context.storefront.query(COLLECTION_HERO_QUERY, {
variables: {
handle: 'winter-2022',
country,
language,
},
}),
analytics: {
pageType: AnalyticsPageType.home,
},
pageContext: pageContext,
seo,
});
}
export default function Homepage() {
const {
primaryHero,
secondaryHero,
tertiaryHero,
featuredCollections,
featuredProducts,
} = useLoaderData();
// TODO: skeletons vs placeholders
const skeletons = getHeroPlaceholder([{}, {}, {}]);
return (
<>
{primaryHero && (
<Hero {...primaryHero} height="full" top loading="eager" />
)}
{featuredProducts && (
<Suspense>
<Await resolve={featuredProducts}>
{({products}) => {
if (!products?.nodes) return <></>;
return (
<ProductSwimlane
products={products}
title="Featured Products"
count={4}
/>
);
}}
</Await>
</Suspense>
)}
{secondaryHero && (
<Suspense fallback={<Hero {...skeletons[1]} />}>
<Await resolve={secondaryHero}>
{({hero}) => {
if (!hero) return <></>;
return <Hero {...hero} />;
}}
</Await>
</Suspense>
)}
{featuredCollections && (
<Suspense>
<Await resolve={featuredCollections}>
{({collections}) => {
if (!collections?.nodes) return <></>;
return (
<FeaturedCollections
collections={collections}
title="Collections"
/>
);
}}
</Await>
</Suspense>
)}
{tertiaryHero && (
<Suspense fallback={<Hero {...skeletons[2]} />}>
<Await resolve={tertiaryHero}>
{({hero}) => {
if (!hero) return <></>;
return <Hero {...hero} />;
}}
</Await>
</Suspense>
)}
</>
);
}
const COLLECTION_CONTENT_FRAGMENT = `#graphql
fragment CollectionContent on Collection {
id
handle
title
descriptionHtml
heading: metafield(namespace: "hero", key: "title") {
value
}
byline: metafield(namespace: "hero", key: "byline") {
value
}
cta: metafield(namespace: "hero", key: "cta") {
value
}
spread: metafield(namespace: "hero", key: "spread") {
reference {
...Media
}
}
spreadSecondary: metafield(namespace: "hero", key: "spread_secondary") {
reference {
...Media
}
}
}
${MEDIA_FRAGMENT}
`;
const HOMEPAGE_SEO_QUERY = `#graphql
query seoCollectionContent($handle: String, $country: CountryCode, $language: LanguageCode)
@inContext(country: $country, language: $language) {
hero: collection(handle: $handle) {
...CollectionContent
}
shop {
name
description
}
}
${COLLECTION_CONTENT_FRAGMENT}
`;
const COLLECTION_HERO_QUERY = `#graphql
query heroCollectionContent($handle: String, $country: CountryCode, $language: LanguageCode)
@inContext(country: $country, language: $language) {
hero: collection(handle: $handle) {
...CollectionContent
}
}
${COLLECTION_CONTENT_FRAGMENT}
`;
// @see: https://shopify.dev/api/storefront/2023-04/queries/products
export const HOMEPAGE_FEATURED_PRODUCTS_QUERY = `#graphql
query homepageFeaturedProducts($country: CountryCode, $language: LanguageCode)
@inContext(country: $country, language: $language) {
products(first: 8) {
nodes {
...ProductCard
}
}
}
${PRODUCT_CARD_FRAGMENT}
`;
// @see: https://shopify.dev/api/storefront/2023-04/queries/collections
export const FEATURED_COLLECTIONS_QUERY = `#graphql
query homepageFeaturedCollections($country: CountryCode, $language: LanguageCode)
@inContext(country: $country, language: $language) {
collections(
first: 4,
sortKey: UPDATED_AT
) {
nodes {
id
title
handle
image {
altText
width
height
url
}
}
}
}
`;