Skip to content

Commit a3e16ab

Browse files
fix(astro): invalid font url (#14440)
1 parent 47df8f2 commit a3e16ab

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

.changeset/cuddly-islands-cut.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Fixes a case where the URLs generated by the experimental Fonts API would be incorrect in dev

packages/astro/src/assets/fonts/implementations/url-proxy-hash-resolver.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,26 @@ export function createDevUrlProxyHashResolver({
2222
return {
2323
resolve(input) {
2424
const { cssVariable, data } = input;
25-
return [cssVariable.slice(2), data.weight, data.style, baseHashResolver.resolve(input)]
25+
return [
26+
cssVariable.slice(2),
27+
formatWeight(data.weight),
28+
data.style,
29+
baseHashResolver.resolve(input),
30+
]
2631
.filter(Boolean)
2732
.join('-');
2833
},
2934
};
3035
}
36+
37+
function formatWeight(
38+
weight: Parameters<UrlProxyHashResolver['resolve']>[0]['data']['weight'],
39+
): string | undefined {
40+
if (Array.isArray(weight)) {
41+
return weight.join('-');
42+
}
43+
if (typeof weight === 'number') {
44+
return weight.toString();
45+
}
46+
return weight;
47+
}

packages/astro/test/units/assets/fonts/implementations.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,5 +457,23 @@ describe('fonts implementations', () => {
457457
}),
458458
'foo-400-italic-whatever.woff2',
459459
);
460+
assert.equal(
461+
resolver.resolve({
462+
cssVariable: '--foo',
463+
data: { weight: '500', style: 'italic' },
464+
originalUrl: 'whatever',
465+
type: 'woff2',
466+
}),
467+
'foo-500-italic-whatever.woff2',
468+
);
469+
assert.equal(
470+
resolver.resolve({
471+
cssVariable: '--foo',
472+
data: { weight: [100, 900], style: 'italic' },
473+
originalUrl: 'whatever',
474+
type: 'woff2',
475+
}),
476+
'foo-100-900-italic-whatever.woff2',
477+
);
460478
});
461479
});

0 commit comments

Comments
 (0)