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
5 changes: 5 additions & 0 deletions .changeset/mean-lands-pull.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes a case where font sources with relative protocol URLs would fail when using the experimental fonts API
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@ export function normalizeRemoteFontFaces({
if ('name' in source) {
return source;
}
// We handle protocol relative URLs here, otherwise they're considered absolute by the font
// fetcher which will try to read them from the file system
const url = source.url.startsWith('//') ? `https:${source.url}` : source.url;
const proxied = {
...source,
originalURL: source.url,
originalURL: url,
url: urlProxy.proxy({
url: source.url,
url,
// We only collect the first URL to avoid preloading fallback sources (eg. we only
// preload woff2 if woff is available)
collectPreload: index === 0,
Expand Down
48 changes: 48 additions & 0 deletions packages/astro/test/units/assets/fonts/logic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,54 @@ describe('fonts logic', () => {
},
]);
});

it('turns relative protocols into https', () => {
const { collected, urlProxy } = createSpyUrlProxy();
const fonts = normalizeRemoteFontFaces({
urlProxy,
fonts: [
{
weight: '400',
style: 'normal',
src: [{ url: '//example.com/font.woff2' }, { url: 'http://example.com/font.woff' }],
},
],
});

assert.deepStrictEqual(
fonts,
[
{
src: [
{
originalURL: 'https://example.com/font.woff2',
url: 'https://example.com/font.woff2',
},
{
originalURL: 'http://example.com/font.woff',
url: 'http://example.com/font.woff',
},
],
style: 'normal',
weight: '400',
},
],
);
assert.deepStrictEqual(collected, [
{
url: 'https://example.com/font.woff2',
collectPreload: true,
data: { weight: '400', style: 'normal' },
init: null,
},
{
url: 'http://example.com/font.woff',
collectPreload: false,
data: { weight: '400', style: 'normal' },
init: null,
},
]);
});
});

describe('optimizeFallbacks()', () => {
Expand Down