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/better-zebras-notice.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes a bug where `experimental_getFontFileURL()` rejected valid font URLs when using the Cloudflare adapter
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import type { RuntimeFontFileUrlResolver } from '../definitions.js';
* During prerendering, a temporary Node HTTP server is started to
* serve font files.
*
* We send request to the provided server address. `requestUrl` on
* `fetch` is not implemented because we have the information from
* within the Vite plugin already.
* When possible, the resolver uses a statically known server
* {@link address}. When the address is not yet available (e.g. the
* virtual module was evaluated before the HTTP server started
* listening — see #17722), the resolver falls back to deriving the
* origin from the caller-supplied {@link requestUrl}.
*/
export class RemoteRuntimeFontFileUrlResolver implements RuntimeFontFileUrlResolver {
#urls: Set<string>;
Expand All @@ -25,19 +27,31 @@ export class RemoteRuntimeFontFileUrlResolver implements RuntimeFontFileUrlResol
this.#address = address;
}

resolve(url: string): string | null {
resolve(url: string, requestUrl: URL | undefined): string | null {
if (!this.#urls.has(url)) {
return null;
}
if (!this.#address) {
throw new Error('Server address unavailable, this should not happen. Open an issue.');
}
// assetsPrefix
if (!url.startsWith('/')) {
url = new URL(url).pathname;
if (this.#address) {
url = new URL(url).pathname;
} else {
return url;
}
}
if (this.#address) {
const host =
this.#address.family === 'IPv6'
? `[${this.#address.address}]`
: this.#address.address;
return `http://${host}:${this.#address.port}${url}`;
}
// Fallback when the server address was not available at module
// load time (e.g. an adapter's dep optimizer pre-bundled the
// font runtime before the HTTP server started listening, #17722).
if (requestUrl) {
return `${requestUrl.origin}${url}`;
}
const host =
this.#address.family === 'IPv6' ? `[${this.#address.address}]` : this.#address.address;
return `http://${host}:${this.#address.port}${url}`;
throw new Error('Server address unavailable, this should not happen. Open an issue.');
}
}
35 changes: 31 additions & 4 deletions packages/astro/test/units/assets/fonts/infra.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,7 @@ describe('fonts infra', () => {
address: { address: '127.0.0.1', family: 'IPv4', port: 3000 },
});

assert.equal(resolver.resolve('/_astro/fonts/bar.woff2'), null);
assert.equal(resolver.resolve('/_astro/fonts/bar.woff2', undefined), null);
});

it('works with ipv4', () => {
Expand All @@ -793,7 +793,7 @@ describe('fonts infra', () => {
});

assert.equal(
resolver.resolve('/test/_astro/fonts/foo.woff2'),
resolver.resolve('/test/_astro/fonts/foo.woff2', undefined),
'http://127.0.0.1:3000/test/_astro/fonts/foo.woff2',
);
});
Expand All @@ -805,7 +805,7 @@ describe('fonts infra', () => {
});

assert.equal(
resolver.resolve('/_astro/fonts/foo.woff2'),
resolver.resolve('/_astro/fonts/foo.woff2', undefined),
'http://[::]:3000/_astro/fonts/foo.woff2',
);
});
Expand All @@ -817,10 +817,37 @@ describe('fonts infra', () => {
});

assert.equal(
resolver.resolve('http://cdn.example.com/_astro/fonts/foo.woff2'),
resolver.resolve('http://cdn.example.com/_astro/fonts/foo.woff2', undefined),
'http://127.0.0.1:3000/_astro/fonts/foo.woff2',
);
});

it('falls back to requestUrl when address is null', () => {
const resolver = new RemoteRuntimeFontFileUrlResolver({
urls: new Set(['/_astro/fonts/foo.woff2']),
address: null,
});

assert.equal(
resolver.resolve(
'/_astro/fonts/foo.woff2',
new URL('http://localhost:4321/og.png'),
),
'http://localhost:4321/_astro/fonts/foo.woff2',
);
});

it('returns full url directly when address is null and url is absolute (assetsPrefix)', () => {
const resolver = new RemoteRuntimeFontFileUrlResolver({
urls: new Set(['http://cdn.example.com/_astro/fonts/foo.woff2']),
address: null,
});

assert.equal(
resolver.resolve('http://cdn.example.com/_astro/fonts/foo.woff2', undefined),
'http://cdn.example.com/_astro/fonts/foo.woff2',
);
});
});

describe('SsrRuntimeFontFileUrlResolver', () => {
Expand Down
Loading