The `CacheLong` strategy instructs caches to store data for 1 hour, and `staleWhileRevalidate` data for an additional 23 hours. Note: these time values are subject to change. Learn more about [data fetching in Hydrogen](/docs/custom-storefronts/hydrogen/data-fetching/fetch-data).
import {json} from '@shopify/remix-oxygen';
import {CacheLong} from '@shopify/hydrogen';
export async function loader({context}) {
const data = await context.storefront.query(
`#grahpql
{
shop {
name
description
}
}`,
{
cache: CacheLong(),
},
);
return json(data);
}
import {json, type LoaderFunctionArgs} from '@shopify/remix-oxygen';
import {CacheLong} from '@shopify/hydrogen';
export async function loader({context}: LoaderFunctionArgs) {
const data = await context.storefront.query(
`#grahpql
{
shop {
name
description
}
}`,
{
cache: CacheLong(),
},
);
return json(data);
}
overrideOptions: AllCacheOptions
export function CacheLong(overrideOptions?: CachingStrategy): AllCacheOptions { guardExpirableModeType(overrideOptions); return { mode: PUBLIC, maxAge: 3600, // 1 hour staleWhileRevalidate: 82800, // 23 Hours ...overrideOptions, }; }
Override options for a cache strategy.
The maximum amount of time in seconds that a resource will be considered fresh. See `max-age` in the [MDN docs](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#:~:text=Response%20Directives-,max%2Dage,-The%20max%2Dage).
The caching mode, generally `public`, `private`, or `no-store`.
Similar to `maxAge` but specific to shared caches. See `s-maxage` in the [MDN docs](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#s-maxage).
Indicate that the cache should serve the stale response if an error occurs while revalidating the cache. See `stale-if-error` in the [MDN docs](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#stale-if-error).
Indicate that the cache should serve the stale response in the background while revalidating the cache. See `stale-while-revalidate` in the [MDN docs](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#stale-while-revalidate).