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

🐛 Change favicon rewrite to be build-time #494

Merged
merged 1 commit into from
Nov 6, 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
6 changes: 6 additions & 0 deletions .changeset/rich-kids-smile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@myst-theme/article': patch
'@myst-theme/book': patch
---

Update favicon links for build time fetches
18 changes: 12 additions & 6 deletions themes/article/app/utils/loaders.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,30 @@ import { responseNoArticle, responseNoSite, getDomainFromRequest } from '@myst-t
const CONTENT_CDN_PORT = process.env.CONTENT_CDN_PORT ?? '3100';
const CONTENT_CDN = process.env.CONTENT_CDN ?? `http://localhost:${CONTENT_CDN_PORT}`;

export async function getConfig(): Promise<SiteManifest> {
type LinkRewriteOptions = { rewriteStaticFolder?: boolean };

export async function getConfig(opts?: LinkRewriteOptions): Promise<SiteManifest> {
const url = `${CONTENT_CDN}/config.json`;
const response = await fetch(url).catch(() => null);
if (!response || response.status === 404) {
throw new Error(`No site configuration found at ${url}`);
}
const data = (await response.json()) as SiteManifest;
return updateSiteManifestStaticLinksInplace(data, updateLink);
return updateSiteManifestStaticLinksInplace(data, (url) => updateLink(url, opts));
}

function updateLink(url: string) {
function updateLink(
url: string,
{ rewriteStaticFolder = process.env.MODE === 'static' }: LinkRewriteOptions = {},
) {
if (!url) return url;
try {
const parsed = new URL(url);
if (parsed.protocol.startsWith('http')) return url;
} catch (error) {
// pass
}
if (process.env.MODE === 'static') {
if (rewriteStaticFolder) {
return `/myst_assets_folder${url}`;
}
return `${CONTENT_CDN}${url}`;
Expand Down Expand Up @@ -107,8 +112,9 @@ export async function getMystSearchJson(): Promise<Record<string, any> | null> {
}

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';
// We are always fetching this at run time, so we don't want the rewritten links
const config = await getConfig({ rewriteStaticFolder: false });
const url = 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() };
Expand Down
18 changes: 12 additions & 6 deletions themes/book/app/utils/loaders.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,30 @@ import { slugToUrl } from 'myst-common';
const CONTENT_CDN_PORT = process.env.CONTENT_CDN_PORT ?? '3100';
const CONTENT_CDN = process.env.CONTENT_CDN ?? `http://localhost:${CONTENT_CDN_PORT}`;

export async function getConfig(): Promise<SiteManifest> {
type LinkRewriteOptions = { rewriteStaticFolder?: boolean };

export async function getConfig(opts?: LinkRewriteOptions): Promise<SiteManifest> {
const url = `${CONTENT_CDN}/config.json`;
const response = await fetch(url).catch(() => null);
if (!response || response.status === 404) {
throw new Error(`No site configuration found at ${url}`);
}
const data = (await response.json()) as SiteManifest;
return updateSiteManifestStaticLinksInplace(data, updateLink);
return updateSiteManifestStaticLinksInplace(data, (url) => updateLink(url, opts));
}

function updateLink(url: string) {
function updateLink(
url: string,
{ rewriteStaticFolder = process.env.MODE === 'static' }: LinkRewriteOptions = {},
) {
if (!url) return url;
try {
const parsed = new URL(url);
if (parsed.protocol.startsWith('http')) return url;
} catch (error) {
// pass
}
if (process.env.MODE === 'static') {
if (rewriteStaticFolder) {
return `/myst_assets_folder${url}`;
}
return `${CONTENT_CDN}${url}`;
Expand Down Expand Up @@ -107,8 +112,9 @@ export async function getMystSearchJson(): Promise<MystSearchIndex | null> {
}

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';
// We are always fetching this at run time, so we don't want the rewritten links
const config = await getConfig({ rewriteStaticFolder: false });
const url = 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() };
Expand Down
Loading