@sls-next/core
Version:
Handles Next.js routing independent of provider
35 lines (34 loc) • 1.7 kB
JavaScript
const firstRegenerateExpiryDate = (lastModifiedHeader, initialRevalidateSeconds) => {
return new Date(new Date(lastModifiedHeader).getTime() + initialRevalidateSeconds * 1000);
};
/**
* Function called within an origin response as part of the Incremental Static
* Regeneration logic. Returns required headers for the response, or false if
* this response is not compatible with ISR.
*/
export const getStaticRegenerationResponse = (options) => {
const { initialRevalidateSeconds } = options;
// ISR pages that were either previously regenerated or generated
// post-initial-build, will have an `Expires` header set. However ISR pages
// that have not been regenerated but at build-time resolved a revalidate
// property will not have an `Expires` header and therefore we check using the
// manifest.
if (!options.expiresHeader &&
!(options.lastModifiedHeader && typeof initialRevalidateSeconds === "number")) {
return false;
}
const expiresAt = options.expiresHeader
? new Date(options.expiresHeader)
: firstRegenerateExpiryDate(options.lastModifiedHeader, initialRevalidateSeconds);
const secondsRemainingUntilRevalidation = Math.ceil(Math.max(0, (expiresAt.getTime() - Date.now()) / 1000));
return {
secondsRemainingUntilRevalidation,
cacheControl: `public, max-age=0, s-maxage=${secondsRemainingUntilRevalidation}, must-revalidate`
};
};
export const getThrottledStaticRegenerationCachePolicy = (expiresInSeconds) => {
return {
secondsRemainingUntilRevalidation: expiresInSeconds,
cacheControl: `public, max-age=0, s-maxage=${expiresInSeconds}, must-revalidate`
};
};