Improve frontend performance and caching behavior
This commit is contained in:
@@ -14,7 +14,25 @@ const MIME_TYPES: Record<string, string> = {
|
||||
};
|
||||
|
||||
const HTML_CACHE_CONTROL = 'public, max-age=0, must-revalidate';
|
||||
const IMMUTABLE_ASSET_CACHE_CONTROL = 'public, max-age=31536000, immutable';
|
||||
const ASSET_404_CACHE_CONTROL = 'no-store';
|
||||
const LONG_CACHE_EXTENSIONS = new Set([
|
||||
'.js',
|
||||
'.mjs',
|
||||
'.css',
|
||||
'.json',
|
||||
'.svg',
|
||||
'.png',
|
||||
'.jpg',
|
||||
'.jpeg',
|
||||
'.webp',
|
||||
'.avif',
|
||||
'.ico',
|
||||
'.woff2',
|
||||
'.woff',
|
||||
'.ttf',
|
||||
'.otf'
|
||||
]);
|
||||
|
||||
export const handle: Handle = async ({ event, resolve }) => {
|
||||
const response = await resolve(event);
|
||||
@@ -36,11 +54,18 @@ export const handle: Handle = async ({ event, resolve }) => {
|
||||
// keep pointing to already-rotated files long after a deployment.
|
||||
if (response.status >= 400) {
|
||||
response.headers.set('cache-control', ASSET_404_CACHE_CONTROL);
|
||||
} else if (pathname.startsWith('/_app/immutable/')) {
|
||||
response.headers.set('cache-control', IMMUTABLE_ASSET_CACHE_CONTROL);
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
const extension = path.extname(pathname).toLowerCase();
|
||||
if (LONG_CACHE_EXTENSIONS.has(extension) && !contentType.includes('text/html')) {
|
||||
response.headers.set('cache-control', IMMUTABLE_ASSET_CACHE_CONTROL);
|
||||
}
|
||||
|
||||
// HTML documents should revalidate so they can reference the latest client
|
||||
// bundle hashes after each deployment.
|
||||
if (contentType.includes('text/html')) {
|
||||
|
||||
Reference in New Issue
Block a user