32 lines
987 B
TypeScript
32 lines
987 B
TypeScript
import path from 'node:path';
|
|
import type { Handle } from '@sveltejs/kit';
|
|
|
|
const MIME_TYPES: Record<string, string> = {
|
|
'.js': 'application/javascript; charset=utf-8',
|
|
'.mjs': 'application/javascript; charset=utf-8',
|
|
'.css': 'text/css; charset=utf-8',
|
|
'.json': 'application/json; charset=utf-8',
|
|
'.svg': 'image/svg+xml; charset=utf-8',
|
|
'.woff2': 'font/woff2',
|
|
'.woff': 'font/woff',
|
|
'.ttf': 'font/ttf',
|
|
'.otf': 'font/otf'
|
|
};
|
|
|
|
export const handle: Handle = async ({ event, resolve }) => {
|
|
const response = await resolve(event);
|
|
if (event.url.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 mime = extension && MIME_TYPES[extension];
|
|
if (mime) {
|
|
response.headers.set('content-type', mime);
|
|
}
|
|
}
|
|
}
|
|
|
|
return response;
|
|
};
|