Skip to content

Commit

Permalink
⭐️ Add custom favicon (#361)
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanc1 authored Apr 11, 2024
1 parent 762fa83 commit 5ecc18e
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .changeset/gold-pens-jump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@myst-theme/common': patch
'@myst-theme/article': patch
'@myst-theme/book': patch
---

Add favicon
1 change: 1 addition & 0 deletions packages/common/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ export function updateSiteManifestStaticLinksInplace(
}
if (data.options.logo) data.options.logo = updateUrl(data.options.logo);
if (data.options.logo_dark) data.options.logo_dark = updateUrl(data.options.logo_dark);
if (data.options.favicon) data.options.favicon = updateUrl(data.options.favicon);
// Update the thumbnails to point at the CDN
data.projects?.forEach((project) => {
if (project.banner) project.banner = updateUrl(project.banner);
Expand Down
4 changes: 4 additions & 0 deletions themes/article/app/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ export const meta: MetaFunction = ({ data }) => {

export const links: LinksFunction = () => {
return [
{
rel: 'icon',
href: '/favicon.ico',
},
{ rel: 'stylesheet', href: tailwind },
{ rel: 'stylesheet', href: thebeCoreCss },
{
Expand Down
11 changes: 11 additions & 0 deletions themes/article/app/routes/[favicon.ico].tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type { LoaderFunction } from '@remix-run/node';
import { getFavicon } from '~/utils/loaders.server';

export const loader: LoaderFunction = async (): Promise<Response> => {
const favicon = await getFavicon();
if (!favicon) return new Response('No favicon found', { status: 404 });
return new Response(
favicon.buffer,
favicon.contentType ? { headers: { 'Content-Type': favicon.contentType } } : undefined,
);
};
8 changes: 8 additions & 0 deletions themes/article/app/utils/loaders.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,11 @@ export async function getObjectsInv(): Promise<Buffer | null> {
if (!response || response.status === 404) return null;
return response.buffer();
}

export async function getFavicon(): Promise<{ contentType: string | null; buffer: Buffer } | null> {
const config = await getConfig();
const url = updateLink(config.options?.favicon) || 'https://mystmd.org/favicon.ico';
const response = await fetch(url).catch(() => null);
if (!response || response.status === 404) return null;
return { contentType: response.headers.get('Content-Type'), buffer: await response.buffer() };
}
Binary file removed themes/article/public/favicon.ico
Binary file not shown.
3 changes: 3 additions & 0 deletions themes/article/template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ options:
- type: string
id: twitter
description: Twitter handle related to the site
- type: file
id: favicon
description: Local path to favicon image
- type: file
id: logo
description: Local path to logo image
Expand Down
4 changes: 4 additions & 0 deletions themes/book/app/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ export const meta: MetaFunction = ({ data }) => {

export const links: LinksFunction = () => {
return [
{
rel: 'icon',
href: '/favicon.ico',
},
{ rel: 'stylesheet', href: tailwind },
{ rel: 'stylesheet', href: thebeCoreCss },
{
Expand Down
11 changes: 11 additions & 0 deletions themes/book/app/routes/[favicon.ico].tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type { LoaderFunction } from '@remix-run/node';
import { getFavicon } from '~/utils/loaders.server';

export const loader: LoaderFunction = async (): Promise<Response> => {
const favicon = await getFavicon();
if (!favicon) return new Response('No favicon found', { status: 404 });
return new Response(
favicon.buffer,
favicon.contentType ? { headers: { 'Content-Type': favicon.contentType } } : undefined,
);
};
8 changes: 8 additions & 0 deletions themes/book/app/utils/loaders.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,11 @@ export async function getObjectsInv(): Promise<Buffer | null> {
if (!response || response.status === 404) return null;
return response.buffer();
}

export async function getFavicon(): Promise<{ contentType: string | null; buffer: Buffer } | null> {
const config = await getConfig();
const url = updateLink(config.options?.favicon) || 'https://mystmd.org/favicon.ico';
const response = await fetch(url).catch(() => null);
if (!response || response.status === 404) return null;
return { contentType: response.headers.get('Content-Type'), buffer: await response.buffer() };
}
Binary file removed themes/book/public/favicon.ico
Binary file not shown.
3 changes: 3 additions & 0 deletions themes/book/template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ options:
- type: string
id: twitter
description: Twitter handle related to the site
- type: file
id: favicon
description: Local path to favicon image
- type: file
id: logo
description: Local path to logo image
Expand Down

0 comments on commit 5ecc18e

Please sign in to comment.