Repository: withastro/astro
File: packages/astro/src/assets/fonts/vite-plugin-fonts.ts (TypeScript)

Code before:
				res.setHeader('Pragma', 'no-cache');
				res.setHeader('Expires', 0);

				res.end(data);
			});
		},
		resolveId(id) {
			if (id === VIRTUAL_MODULE_ID) {
				return RESOLVED_VIRTUAL_MODULE_ID;
			}
		},
		load(id, opts) {
			if (id === RESOLVED_VIRTUAL_MODULE_ID && opts?.ssr) {
				return `
				export const fontsData = new Map(${JSON.stringify(Array.from(resolvedMap?.entries() ?? []))})
				`;
			}
		},
		async buildEnd() {
			resolvedMap = null;

			if (sync) {
				hashToUrlMap = null;
				storage = null;
				return;
			}

			// TODO: properly cleanup in case of failure

			const logManager = createLogManager(logger);
			const dir = getClientOutputDirectory(settings);
			const fontsDir = new URL('.' + baseUrl, dir);
			try {
				mkdirSync(fontsDir, { recursive: true });
			} catch (e) {
				throw new AstroError(AstroErrorData.UnknownFilesystemError, { cause: e });
			}
			if (hashToUrlMap) {
				await Promise.all(
					Array.from(hashToUrlMap.entries()).map(async ([hash, url]) => {
						logManager.add(hash);
						const { cached, data } = await cache(storage!, hash, () => fetchFont(url));
						logManager.remove(hash, cached);
						try {
							writeFileSync(new URL(hash, fontsDir), data);
						} catch (e) {
							throw new AstroError(AstroErrorData.UnknownFilesystemError, { cause: e });
						}
					}),
				);
			}

Review comment (suggestion):
Just confirming that we can be sure `font` is always defined. Non-null assertions always make me nervous.

Code after:

				res.setHeader('Content-Length', data.length);
				res.setHeader('Content-Type', `font/${extractFontType(hash)}`);
				// We don't want the request to be cached in dev because we cache it already internally,
				// and it makes it easier to debug without needing hard refreshes
				res.setHeader('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0');
				res.setHeader('Pragma', 'no-cache');
				res.setHeader('Expires', 0);

				res.end(data);
			});
		},
		resolveId(id) {
			if (id === VIRTUAL_MODULE_ID) {
				return RESOLVED_VIRTUAL_MODULE_ID;
			}
		},
		load(id, opts) {
			if (id === RESOLVED_VIRTUAL_MODULE_ID && opts?.ssr) {
				return `
				export const fontsData = new Map(${JSON.stringify(Array.from(resolvedMap?.entries() ?? []))})
				`;
			}
		},
		async buildEnd() {
			if (sync || settings.config.experimental.fonts!.families.length === 0) {
				cleanup();
				return;
			}

			try {
				const logManager = createLogManager(logger);
				const dir = getClientOutputDirectory(settings);
				const fontsDir = new URL('.' + baseUrl, dir);
				try {
					mkdirSync(fontsDir, { recursive: true });
				} catch (e) {
					throw new AstroError(AstroErrorData.UnknownFilesystemError, { cause: e });
				}
				if (hashToUrlMap) {
					await Promise.all(
						Array.from(hashToUrlMap.entries()).map(async ([hash, url]) => {
							logManager.add(hash);
							const { cached, data } = await cache(storage!, hash, () => fetchFont(url));
							logManager.remove(hash, cached);
							try {
								writeFileSync(new URL(hash, fontsDir), data);
							} catch (e) {
								throw new AstroError(AstroErrorData.UnknownFilesystemError, { cause: e });
							}
						}),