Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion apps/web/staticFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ export function sendStatic(request: http.IncomingMessage, response: http.ServerR
// Each encoding is its own representation, so it gets its own validator.
const etag = encoding ? `${item.etag.slice(0, -1)}-${encoding}"` : item.etag;
// no-transform keeps proxies from rewriting pages; Cloudflare would otherwise inject an analytics script the CSP blocks.
const immutable = relative && (versionedEntryFile.test(relative) || bundledChunk.test(relative) || relative.startsWith('assets/katex/'));
const immutable = relative && (versionedEntryFile.test(relative)
|| /^assets\/(?:mark|favicon)\.[a-f0-9]{12}\.svg$/u.test(relative)
|| bundledChunk.test(relative) || relative.startsWith('assets/katex/'));
const cacheableAsset = relative?.startsWith('assets/');
const headers: http.OutgoingHttpHeaders = { 'Content-Type': contentType, ETag: etag,
'Cache-Control': immutable ? 'public, max-age=31536000, immutable, no-transform'
Expand Down
28 changes: 25 additions & 3 deletions scripts/build-web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,30 @@ fs.writeFileSync(path.join(output, 'research.css'), ['research.css', 'collaborat
const fonts = ['Geist-latin.woff2', 'Geist-OFL.txt', 'TheoremOSSerif-latin.woff2', 'Lora-OFL.txt', 'JetBrainsMono.woff2', 'JetBrainsMono-OFL.txt',
'TheoremOSWorkbenchHeading.woff2',
'NotoSerifSC-ui.woff2', 'NotoSerifSC-ui-extra.woff2', 'NotoSerifSC-common.woff2', 'NotoSerifSC-OFL.txt', 'NotoSansSC-ui.woff2', 'NotoSansSC-ui-extra.woff2', 'NotoSansSC-common.woff2', 'NotoSansSC-OFL.txt'];
for (const file of ['mark.svg', 'favicon.svg', ...fonts.map(font => `fonts/${font}`)])
fs.copyFileSync(path.join(root, 'docs/assets', file), path.join(output, 'assets', file));
for (const name of fs.readdirSync(path.join(output, 'assets'))) {
if (/^(?:mark|favicon)\.[a-f0-9]{12}\.svg(?:\.br)?$/u.test(name)) fs.rmSync(path.join(output, 'assets', name));
}
const hashedBrandAssets = new Map<string, string>();
for (const file of ['mark.svg', 'favicon.svg', ...fonts.map(font => `fonts/${font}`)]) {
const target = path.join(output, 'assets', file);
fs.copyFileSync(path.join(root, 'docs/assets', file), target);
if (file === 'mark.svg' || file === 'favicon.svg') {
const hash = crypto.createHash('sha256').update(fs.readFileSync(target)).digest('hex').slice(0, 12);
const versionedName = `assets/${path.basename(file, '.svg')}.${hash}.svg`;
fs.copyFileSync(target, path.join(output, versionedName));
hashedBrandAssets.set(`assets/${file}`, versionedName);
}
}
const versionBrandRefs = (text: string) => {
let out = text;
for (const [source, versioned] of hashedBrandAssets) out = out.replaceAll(source, versioned);
return out;
};
for (const file of fs.readdirSync(output, { recursive: true, encoding: 'utf8' })) {
if (!file.endsWith('.js')) continue;
const full = path.join(output, file);
fs.writeFileSync(full, versionBrandRefs(fs.readFileSync(full, 'utf8')));
}
fs.mkdirSync(path.join(output, 'assets/licenses'), { recursive: true });
fs.cpSync(path.join(root, 'node_modules/katex/dist/fonts'), path.join(output, 'assets/katex/fonts'), { recursive: true });
fs.copyFileSync(path.join(root, 'node_modules/katex/dist/katex.min.css'), path.join(output, 'assets/katex/katex.min.css'));
Expand Down Expand Up @@ -93,7 +115,7 @@ for (const page of pages) {
}
return `${attribute}${asset}"`;
});
fs.writeFileSync(file, html);
fs.writeFileSync(file, page === 'explain.html' || page === 'formalization.html' ? html : versionBrandRefs(html));
}
const buildDigest = crypto.createHash('sha256');
// Identify the complete shipped frontend, including page text, fonts and classic records.
Expand Down
8 changes: 8 additions & 0 deletions tests/webStaticFiles.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,15 @@ describe('static workbench files', () => {

it('caches static assets at edges while keeping HTML uncached', async () => {
const katex = path.join(directory, 'assets/katex/katex.min.css');
const hashedMark = path.join(directory, 'assets/mark.0123456789ab.svg');
const hashedFavicon = path.join(directory, 'assets/favicon.0123456789ab.svg');
const font = path.join(directory, 'assets/fonts/NotoSansSC-ui.woff2');
const html = path.join(directory, 'index.html');
fs.mkdirSync(path.dirname(katex), { recursive: true });
fs.mkdirSync(path.dirname(font), { recursive: true });
fs.writeFileSync(katex, 'body{}');
fs.writeFileSync(hashedMark, '<svg/>');
fs.writeFileSync(hashedFavicon, '<svg/>');
fs.writeFileSync(font, Buffer.alloc(64, 1));
fs.writeFileSync(html, '<!doctype html>');
const assetServer = http.createServer((request, response) => {
Expand All @@ -104,9 +108,13 @@ describe('static workbench files', () => {
const assetOrigin = `http://127.0.0.1:${(assetServer.address() as { port: number }).port}`;
try {
const katexRes = await fetch(`${assetOrigin}/assets/katex/katex.min.css`);
const markRes = await fetch(`${assetOrigin}/assets/mark.0123456789ab.svg`);
const faviconRes = await fetch(`${assetOrigin}/assets/favicon.0123456789ab.svg`);
const fontRes = await fetch(`${assetOrigin}/assets/fonts/NotoSansSC-ui.woff2`);
const htmlRes = await fetch(`${assetOrigin}/index.html`);
expect(katexRes.headers.get('cache-control')).toBe('public, max-age=31536000, immutable, no-transform');
expect(markRes.headers.get('cache-control')).toBe('public, max-age=31536000, immutable, no-transform');
expect(faviconRes.headers.get('cache-control')).toBe('public, max-age=31536000, immutable, no-transform');
expect(fontRes.headers.get('cache-control')).toBe('public, max-age=86400, stale-while-revalidate=604800, no-transform');
expect(htmlRes.headers.get('cache-control')).toBe('no-cache, no-transform');
} finally {
Expand Down
Loading