Urara-Blog/node_modules/.pnpm-store/v3/files/3a/b8a89e7c885ee0c25eea25882d701a40dd40bb127d66c1cf11e7c739a9f246296a06b34ace9d37fb4d6d6757876ddd64f183e05b31a23ab8e7c4d2f535209a
2022-08-14 01:14:53 +08:00

53 lines
1.1 KiB
Text

import { Server } from '0SERVER';
import { manifest, prerendered } from 'MANIFEST';
const server = new Server(manifest);
const prefix = `/${manifest.appDir}/`;
/**
* @param { Request } request
* @param { any } context
* @returns { Promise<Response> }
*/
export default function handler(request, context) {
if (is_static_file(request)) {
// Static files can skip the handler
// TODO can we serve _app/immutable files with an immutable cache header?
return;
}
return server.respond(request, {
platform: { context },
getClientAddress() {
return context.ip;
}
});
}
/**
* @param {Request} request
*/
function is_static_file(request) {
const url = new URL(request.url);
// Assets in the app dir
if (url.pathname.startsWith(prefix)) {
return true;
}
// prerendered pages and index.html files
const pathname = url.pathname.replace(/\/$/, '');
let file = pathname.substring(1);
try {
file = decodeURIComponent(file);
} catch (err) {
// ignore
}
return (
manifest.assets.has(file) ||
manifest.assets.has(file + '/index.html') ||
prerendered.has(pathname || '/')
);
}