0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-03-10 23:01:26 -05:00

feat(fonts): dev headers (#13275)

This commit is contained in:
Florian Lefebvre 2025-02-20 12:27:51 +01:00 committed by GitHub
parent 287b789ec6
commit 868e2ab9ed
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 41 additions and 15 deletions

View file

@ -7,7 +7,13 @@ import xxhash from 'xxhash-wasm';
import { isAbsolute } from 'node:path';
import { getClientOutputDirectory } from '../../prerender/utils.js';
import { mkdirSync, writeFileSync } from 'node:fs';
import { generateFontFace, createCache, type CacheHandler, createURLProxy } from './utils.js';
import {
generateFontFace,
createCache,
type CacheHandler,
createURLProxy,
extractFontType,
} from './utils.js';
import {
DEFAULTS,
VIRTUAL_MODULE_ID,
@ -272,16 +278,14 @@ export function fontsPlugin({ settings, sync, logger }: Options): Plugin {
const { cached, data } = await cache!(hash, () => fetchFont(url));
logManager.remove(hash, cached);
// TODO: add cache control back
// TODO: set content type and cache control manually
// const keys = ['cache-control', 'content-type', 'content-length'];
// const keys = ['content-type', 'content-length'];
// for (const key of keys) {
// const value = response.headers.get(key);
// if (value) {
// res.setHeader(key, value);
// }
// }
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);
});
},

View file

@ -2,6 +2,7 @@
import { after, before, describe, it } from 'node:test';
import { loadFixture } from './test-utils.js';
import assert from 'node:assert/strict';
import * as cheerio from 'cheerio';
describe('astro:fonts', () => {
/** @type {import('./test-utils.js').Fixture} */
@ -58,14 +59,35 @@ describe('astro:fonts', () => {
it('Includes styles', async () => {
const res = await fixture.fetch('/');
const body = await res.text();
assert.equal(body.includes('<style>'), true);
const html = await res.text();
assert.equal(html.includes('<style>'), true);
});
it('Includes links when preloading', async () => {
const res = await fixture.fetch('/preload');
const body = await res.text();
assert.equal(body.includes('<link rel="preload"'), true);
const html = await res.text();
assert.equal(html.includes('<link rel="preload"'), true);
});
it('Has correct headers in dev', async () => {
let res = await fixture.fetch('/preload');
const html = await res.text();
const $ = cheerio.load(html);
const href = $('link[rel=preload][type^=font/woff2]').attr('href');
if (!href) {
assert.fail();
}
const headers = await fixture.fetch(href).then((r) => r.headers);
assert.equal(headers.has('Content-Length'), true);
assert.equal(headers.get('Content-Type'), 'font/woff2');
assert.equal(
headers.get('Cache-Control'),
'no-store, no-cache, must-revalidate, max-age=0',
);
assert.equal(headers.get('Pragma'), 'no-cache');
assert.equal(headers.get('Expires'), '0');
});
});
});