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

feat: add full page caching #13

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

serhalp
Copy link
Contributor

@serhalp serhalp commented Sep 19, 2024

Description

This may be more digestible if you review commit by commit, as it's really three separate changes that come together. See also #14 as a follow-up.

Make pages cacheable

This PR makes all pages load the cart and customer account data client side only. This results in all SSR page responses (except the /account/* pages) cacheable on a public CDN like Netlify's.

See https://shopify.dev/docs/storefronts/headless/hydrogen/cart/render-client-side for the approach (but note that there are a few errors on that page, for which I've submitted feedback).

See also https://shopify.dev/docs/storefronts/headless/hydrogen/caching/full-page-cache for more general information on full-page caching with Hydrogen.

Cache the pages

Cache most pages for 1 hour on the Netlify CDN with stale-while-revalidate (allowing 1 min for background revalidation to complete). Cache these also on browsers (and anything else upstream) but require a conditional request to revalidate.

On the search page, vary on the search input.

Disable this caching on pages that have un-deferred personalization, which is the /account/* pages. (Technically we could apply the same pattern here, but the trade-off calculation is pretty different.)

Make data routes cacheable

This also caches resource routes by default, with once again the account and cart opted out. This makes client-side navigation leverage caching as well.

@serhalp serhalp force-pushed the serhalp/frb-1314-hydrogen-demo-for-shopify-fine-grained-caching branch from dc85a50 to 5e75ac2 Compare September 19, 2024 15:29
Copy link

netlify bot commented Sep 19, 2024

Deploy Preview for hydrogen-remix-template ready!

Name Link
🔨 Latest commit 266f746
🔍 Latest deploy log https://app.netlify.com/sites/hydrogen-remix-template/deploys/66fb183b6bcd4200085f4f37
😎 Deploy Preview https://deploy-preview-13--hydrogen-remix-template.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@serhalp serhalp force-pushed the serhalp/frb-1314-hydrogen-demo-for-shopify-fine-grained-caching branch 3 times, most recently from b05ebd3 to 3489a29 Compare September 19, 2024 18:55
@serhalp serhalp marked this pull request as ready for review September 19, 2024 19:07
Comment on lines 1 to 23
export const NO_CACHE = {
'Cache-Control': 'private, no-store, no-cache, must-revalidate',
};

export const CACHE_1_DAY = {
'Cache-Control': 'public, max-age=0',
};

export const CACHE_1_HOUR_SWR = {
'Cache-Control': 'public, max-age=0, must-revalidate',
'Netlify-CDN-Cache-Control':
'public, max-age=3600, stale-while-revalidate=60',
};
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Could also be a good use case for Matt's https:/ascorbic/cdn-cache-control/


export const meta: MetaFunction = () => {
return [{title: 'Hydrogen | Home'}];
};

export const headers: HeadersFunction = () => ({
Copy link
Contributor Author

Choose a reason for hiding this comment

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

https://remix.run/docs/en/main/route/headers

This is inherited by all child routes, unless a child route (or... closer ancestor parent route) overrides it.

@serhalp serhalp force-pushed the serhalp/frb-1314-hydrogen-demo-for-shopify-fine-grained-caching branch 3 times, most recently from 779d700 to 30736b2 Compare September 26, 2024 22:34
@serhalp serhalp force-pushed the serhalp/frb-1314-hydrogen-demo-for-shopify-fine-grained-caching branch from 30736b2 to 5632931 Compare September 30, 2024 18:46
Cache most pages for 1 hour on the Netlify CDN with stale-while-revalidate (allowing 1 min for
background revalidation to complete). Cache these also on browsers (and anything else upstream) but
require a conditional request to revalidate.

On the search page, vary on the search input.

Disable this caching on pages that have personalization: account/*, cart.
@serhalp serhalp force-pushed the serhalp/frb-1314-hydrogen-demo-for-shopify-fine-grained-caching branch from 5632931 to 266f746 Compare September 30, 2024 21:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant