mirror of
https://github.com/Sevichecc/Urara-Blog.git
synced 2025-05-03 05:19:30 +08:00
53 lines
1.1 KiB
Text
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 || '/')
|
|
);
|
|
}
|