Skip to content

Commit

Permalink
Fix favicon support for query and fragment in URLs (#2645)
Browse files Browse the repository at this point in the history
Co-authored-by: Chris Swithinbank <[email protected]>
  • Loading branch information
techfg and delucis authored Dec 4, 2024
1 parent 38db4ec commit cf12beb
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/neat-deers-pretend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/starlight': patch
---

Fixes support for favicon URLs that contain a search query and/or hash
27 changes: 27 additions & 0 deletions packages/starlight/__tests__/basics/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,33 @@ describe('FaviconSchema', () => {
expect(favicon.type).toBe('image/jpeg');
});

test('returns the proper href and type attributes when contains query', () => {
const icon = '/custom-icon.gif?v=123456&x=987654';

const favicon = FaviconSchema().parse(icon);

expect(favicon.href).toBe(icon);
expect(favicon.type).toBe('image/gif');
});

test('returns the proper href and type attributes when contains fragment', () => {
const icon = '/custom-icon.png#favicon';

const favicon = FaviconSchema().parse(icon);

expect(favicon.href).toBe(icon);
expect(favicon.type).toBe('image/png');
});

test('returns the proper href and type attributes when contains query and fragment', () => {
const icon = '/custom-icon.ico?v=123456&x=987654#favicon';

const favicon = FaviconSchema().parse(icon);

expect(favicon.href).toBe(icon);
expect(favicon.type).toBe('image/x-icon');
});

test('throws on invalid favicon extensions', () => {
expect(() => FaviconSchema().parse('/favicon.pdf')).toThrow();
});
Expand Down
4 changes: 3 additions & 1 deletion packages/starlight/schemas/favicon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ export const FaviconSchema = () =>
.string()
.default('/favicon.svg')
.transform((favicon, ctx) => {
const ext = extname(favicon).toLowerCase();
// favicon can be absolute or relative url
const { pathname } = new URL(favicon, 'https://example.com');
const ext = extname(pathname).toLowerCase();

if (!isFaviconExt(ext)) {
ctx.addIssue({
Expand Down

0 comments on commit cf12beb

Please sign in to comment.