RFC: Hydrogen Caching #640
Replies: 3 comments 3 replies
-
Re: Sub-request CachingI feel it's a little confusing that the caching strategy helper export async function loader({context: {withCache}}: LoaderArgs) {
await withCache(
['some', 'key'],
async () => console.log('this should only show up once every 10 secs'),
{
strategy: storefront.CacheCustom({
mode: 'public',
maxAge: 10,
}),
},
); Should both // Example property names that came to mind
const cache = {
fn, // withCache
strategy // storefront.CacheCustom
} In practice it would look like this: export async function loader({context: {cache}}: LoaderArgs) {
await cache.fn(
['some', 'key'],
async () => console.log('this should only show up once every 10 secs'),
{
strategy: cache.strategy({
mode: 'public',
maxAge: 10,
}),
},
); |
Beta Was this translation helpful? Give feedback.
-
Oxygen has backported the Hydrogen's implementation with some minor adjustments and we're ready to give it to select merchants: Additionally, we are discussing how Oxygen could help users benefit from unique features, namely:
I'll link the corresponding issues once we have them available. Meanwhile, Oxygen wants to align with Hydrogen on how we could test it. |
Beta Was this translation helpful? Give feedback.
-
Any chance the full page caching will be supported in V2? |
Beta Was this translation helpful? Give feedback.
-
You can improve performance by caching either the full page of a request, or some of the individual queries that go into rendering a page. For example, the entire page
/products/hydrogen
can be cached, and every customer is served that cached page. Alternatively, the full page is re-rendered for every customer, but the underlying queries to the Storefront API and other third-party APIs are cached. Full-page caching isn’t appropriate for personalized or dynamic experiences, because the server rendered page needs to be different per user.Hydrogen v1 supports both forms of caching. This RFC explores how we can expand that support to v2.
Sub-request Caching
Hydrogen v2 only supports caching sub-requests to the Storefront API via our storefront client. If you need to make an API request to a third party API, we have recommended to directly use the cache API of whatever platform you deploy to. For example, an implementation using the cache API on Oxygen might look like:
The problem is that the cache API is complex. The above example also does not work with POST requests. Caching POSTs gets much more complex. It also has no stale-while-revalidate. We think we can do better.
We propose a new
withCache
utility that you can inject into your action/loader context:Track this proposal at: #600
Full-page caching
Hydrogen v1 supports full-page caching. Remix encourages developers to use Cache-Control headers. Those headers will be respected by the browser, so that a single browser won’t request the same page more than once. But separate browsers still make duplicate requests without caching on the server/worker.
We propose full-page caching as a feature available on Oxygen. Seamlessly using the Oxygen remix adapter, any
Cache-Control
headers returned from both your loaders or pages, will result in caching at the edge.Track this proposal at: #615
Caching off Oxygen
Different platforms (Netlify, Vercel, Cloudflare, Deno, etc) offer different caching solutions. Hydrogen works best when deployed to Oxygen, because we can automatically setup everything for you. But if you want
storefront.query
to cache properly on other platforms, you need to build your ownCache
implementation, and pass an instance tocreateStorefrontClient
:Beta Was this translation helpful? Give feedback.
All reactions