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

DO NOT MERGE: Add a caching test route #1712

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import {useRef, Suspense} from 'react';
import {Disclosure, Listbox} from '@headlessui/react';
import {defer, redirect, type LoaderFunctionArgs} from '@shopify/remix-oxygen';
import {

Check warning on line 3 in templates/demo-store/app/routes/($locale).products.$productHandle.tsx

View workflow job for this annotation

GitHub Actions / ⬣ ESLint

Import "MetaFunction" is only used as types
MetaFunction,
defer,
redirect,
type LoaderFunctionArgs,
} from '@shopify/remix-oxygen';
import {useLoaderData, Await} from '@remix-run/react';
import type {ShopifyAnalyticsProduct} from '@shopify/hydrogen';
import {
Expand Down Expand Up @@ -39,6 +44,14 @@

export const headers = routeHeaders;

export const meta: MetaFunction = () => [
{
tagName: 'link',
rel: 'canonical',
href: 'https://remix.run',
},
];

export async function loader({params, request, context}: LoaderFunctionArgs) {
const {productHandle} = params;
invariant(productHandle, 'Missing productHandle param, check route filename');
Expand Down
61 changes: 61 additions & 0 deletions templates/demo-store/app/routes/sub-request-cache.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import {type LoaderFunctionArgs, json} from '@remix-run/server-runtime';
import {CacheLong} from '@shopify/hydrogen';

import {PRODUCT_CARD_FRAGMENT} from '~/data/fragments';

export async function loader({context}: LoaderFunctionArgs) {
const time = new Date();

const data = await context.storefront.query(ALL_PRODUCTS_QUERY, {
variables: {
first: 200,
country: context.storefront.i18n.country,
language: context.storefront.i18n.language,
},
cache: CacheLong(),
});

const storefrontQueryTime = new Date().getTime() - time.getTime();

console.log('Storefront query sub-request time: ', storefrontQueryTime);

Check warning on line 20 in templates/demo-store/app/routes/sub-request-cache.tsx

View workflow job for this annotation

GitHub Actions / ⬣ ESLint

Unexpected console statement

const time2 = new Date();

await context.withCache('my-key', CacheLong(), async () => {
return new Promise((resolve) => {
setTimeout(() => {
resolve('hello');
}, 500);
});
});

const withCacheTime = new Date().getTime() - time2.getTime();

console.log('withCache sub-request time: ', withCacheTime);

Check warning on line 34 in templates/demo-store/app/routes/sub-request-cache.tsx

View workflow job for this annotation

GitHub Actions / ⬣ ESLint

Unexpected console statement

return json({storefrontQueryTime, withCacheTime});
}

const ALL_PRODUCTS_QUERY = `#graphql
query AllProducts(
$country: CountryCode
$language: LanguageCode
$first: Int
$last: Int
$startCursor: String
$endCursor: String
) @inContext(country: $country, language: $language) {
products(first: $first, last: $last, before: $startCursor, after: $endCursor) {
nodes {
...ProductCard
}
pageInfo {
hasPreviousPage
hasNextPage
startCursor
endCursor
}
}
}
${PRODUCT_CARD_FRAGMENT}
` as const;
2 changes: 1 addition & 1 deletion templates/demo-store/customer-accountapi.generated.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export type CustomerAddressUpdateMutation = {
customerAddressUpdate?: CustomerAccountAPI.Maybe<{
userErrors: Array<
Pick<
CustomerAccountAPI.UserErrorsAddressUserErrors,
CustomerAccountAPI.UserErrorsCustomerAddressUserErrors,
'code' | 'field' | 'message'
>
>;
Expand Down
1 change: 1 addition & 0 deletions templates/demo-store/remix.env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ declare module '@shopify/remix-oxygen' {
customerAccount: CustomerAccount;
cart: HydrogenCart;
env: Env;
withCache: WithCache;
}
}

Expand Down
4 changes: 4 additions & 0 deletions templates/demo-store/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
cartSetIdDefault,
createCartHandler,
createStorefrontClient,
createWithCache,
storefrontRedirect,
createCustomerAccountClient,
} from '@shopify/hydrogen';
Expand Down Expand Up @@ -70,6 +71,8 @@ export default {
setCartId: cartSetIdDefault(),
});

const withCache = createWithCache({cache, waitUntil});

/**
* Create a Remix request handler and pass
* Hydrogen's Storefront client to the loader context.
Expand All @@ -84,6 +87,7 @@ export default {
customerAccount,
cart,
env,
withCache,
}),
});

Expand Down