Open History to see the last commit
1// The published site as a Neon Function: built client files are served from the bundle, and every
2// other request (pages, plus the /api and /v1 proxies to the App API) goes to the TanStack Start
3// server. Packaged by scripts/package.ts after `vite build`.
4import { readFile } from 'node:fs/promises';
5import { extname, join, resolve, sep } from 'node:path';
6import { fileURLToPath } from 'node:url';
7// @ts-expect-error Built by `vite build` before packaging.
8import server from './dist/server/server.js';
9
10const client = resolve(fileURLToPath(new URL('../client/', import.meta.url)));
11const types: Record<string, string> = {
12 '.js': 'text/javascript; charset=utf-8', '.css': 'text/css; charset=utf-8', '.svg': 'image/svg+xml', '.png': 'image/png',
13 '.ico': 'image/x-icon', '.webmanifest': 'application/manifest+json', '.json': 'application/json', '.woff2': 'font/woff2', '.txt': 'text/plain; charset=utf-8',
14};
15
16async function asset(pathname: string): Promise<Response | null> {
17 if (!extname(pathname)) return null;
18 const file = resolve(join(client, decodeURIComponent(pathname)));
19 if (!file.startsWith(client + sep)) return null;
20 try {
21 const body = await readFile(file);
22 const immutable = pathname.startsWith('/assets/');
23 return new Response(new Uint8Array(body), { headers: { 'Content-Type': types[extname(file)] ?? 'application/octet-stream', 'Cache-Control': immutable ? 'public, max-age=31536000, immutable' : 'public, max-age=3600', 'X-Content-Type-Options': 'nosniff' } });
24 } catch { return null; }
25}
26
27export default {
28 async fetch(request: Request): Promise<Response> {
29 if (request.method === 'GET' || request.method === 'HEAD') {
30 const file = await asset(new URL(request.url).pathname);
31 if (file) return file;
32 }
33 return server.fetch(request);
34 },
35};