Hardening responsiveness and SEO
This commit is contained in:
@@ -13,18 +13,38 @@ const MIME_TYPES: Record<string, string> = {
|
||||
'.otf': 'font/otf'
|
||||
};
|
||||
|
||||
const HTML_CACHE_CONTROL = 'public, max-age=0, must-revalidate';
|
||||
const ASSET_404_CACHE_CONTROL = 'no-store';
|
||||
|
||||
export const handle: Handle = async ({ event, resolve }) => {
|
||||
const response = await resolve(event);
|
||||
if (event.url.pathname.startsWith('/_app/')) {
|
||||
const pathname = event.url.pathname;
|
||||
const contentType = response.headers.get('content-type') ?? '';
|
||||
|
||||
if (pathname.startsWith('/_app/')) {
|
||||
const existing = response.headers.get('content-type');
|
||||
const hasValidHeader = existing && existing.trim().length > 0;
|
||||
if (!hasValidHeader) {
|
||||
const extension = path.extname(event.url.pathname).toLowerCase();
|
||||
const extension = path.extname(pathname).toLowerCase();
|
||||
const mime = extension && MIME_TYPES[extension];
|
||||
if (mime) {
|
||||
response.headers.set('content-type', mime);
|
||||
}
|
||||
}
|
||||
|
||||
// Missing hashed assets should never be cached; otherwise stale HTML can
|
||||
// keep pointing to already-rotated files long after a deployment.
|
||||
if (response.status >= 400) {
|
||||
response.headers.set('cache-control', ASSET_404_CACHE_CONTROL);
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
// HTML documents should revalidate so they can reference the latest client
|
||||
// bundle hashes after each deployment.
|
||||
if (contentType.includes('text/html')) {
|
||||
response.headers.set('cache-control', HTML_CACHE_CONTROL);
|
||||
}
|
||||
|
||||
return response;
|
||||
|
||||
Reference in New Issue
Block a user