Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix favicon support for query and fragment in URLs #2645

Merged
merged 6 commits into from
Dec 4, 2024
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/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
Loading