Skip to content

Commit 6f5cb4f

Browse files
committed
fix build by adding quotes to avatar img src in satori template-html
1 parent d0d9e0c commit 6f5cb4f

File tree

4 files changed

+502
-530
lines changed

4 files changed

+502
-530
lines changed

src/libs/api/open-graph/template-html.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ const templateHtml = ({ title, heroImageUrl, avatarImageUrl, siteUrl }: Template
1515

1616
const randomGradient = getRandomGradient();
1717

18+
// important: <img src="..." /> must have quotes, and all attributes too, or build will fail
19+
1820
return html`
1921
<div class="flex p-8 h-full" style="${randomGradient}">
2022
<div class="flex w-full flex-row justify-between text-slate-900">
@@ -26,7 +28,7 @@ const templateHtml = ({ title, heroImageUrl, avatarImageUrl, siteUrl }: Template
2628
<!-- avatar and site -->
2729
<div class="flex items-center ${isLongSiteUrl ? 'flex-col justify-end items-start' : ''}">
2830
<img
29-
src=${avatarImageUrl}
31+
src="${avatarImageUrl}"
3032
width="120"
3133
height="120"
3234
class="rounded-full mr-8 border-2 border-gray-300"

src/libs/api/open-graph/template-props.ts

Lines changed: 57 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -15,55 +15,12 @@ import image404 from '@/assets/images/pages/image404.jpg';
1515
const { SITE_URL } = CONFIG_CLIENT;
1616
const { AVATAR, OG_FOLDER, IMAGE_404 } = FILE_PATHS;
1717

18-
// todo: no urls at build time
19-
// ! must use absolute file paths
20-
export const getTemplatePropsUrl = (frontmatterProps: FrontmatterProps): TemplateProps => {
21-
const { title, heroImage, pageId } = frontmatterProps;
22-
23-
// avatarImage
24-
const avatarImageUrl = avatarImage.src;
25-
26-
// heroImage
27-
let heroImageUrl: string;
28-
29-
switch (true) {
30-
// from mdx frontmatter
31-
case Boolean(heroImage.src):
32-
heroImageUrl = `${SITE_URL}${heroImage.src}`; // todo: fix this, path from mdx frontmatter
33-
break;
34-
// hardcoded in 404.mdx frontmatter
35-
case pageId === 'page404':
36-
heroImageUrl = image404.src;
37-
break;
38-
39-
// fallback to random default image
40-
default:
41-
heroImageUrl = `${SITE_URL}${getRandomImageUrl()}`;
42-
break;
43-
}
44-
45-
// ? all fail
46-
// const dummyImageAbsolutePath =
47-
// '/home/username/Desktop/nemanjam.github.io/src/assets/images/default/open-graph/amfi1.jpg';
48-
// const dummyImageUrl = 'https://placehold.co/600x400.png';
49-
// const dummyImageBase64Url =
50-
// 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAusB9Y1bN+UAAAAASUVORK5CYII=';
51-
52-
const templateProps = {
53-
title,
54-
heroImageUrl: heroImageUrl,
55-
avatarImageUrl: avatarImageUrl,
56-
siteUrl: trimHttpProtocol(SITE_URL),
57-
};
58-
59-
return templateProps;
60-
};
61-
6218
// original base64 implementation
6319
export const getTemplatePropsBase64 = async (
6420
frontmatterProps: FrontmatterProps
6521
): Promise<TemplateProps> => {
6622
const { title, heroImage, pageId } = frontmatterProps;
23+
6724
// avatarImage
6825
const avatarImageBase64Url = await getBase64Image(AVATAR);
6926

@@ -97,26 +54,8 @@ export const getTemplatePropsBase64 = async (
9754
return templateProps;
9855
};
9956

100-
/*-------------------------------- url utils (ImageMetadata) ------------------------------*/
101-
102-
export const getOpenGraphImagesMetadata = (): ImageMetadata[] => {
103-
const imageModules = import.meta.glob<{ default: ImageMetadata }>(
104-
// cant be even variable
105-
'/src/assets/images/default/open-graph/*.jpg',
106-
{ eager: true }
107-
);
108-
109-
// convert map to array
110-
const imagesMetadata = Object.keys(imageModules).map((path) => imageModules[path].default);
111-
112-
return imagesMetadata;
113-
};
114-
115-
export const getRandomImageUrl = () => getRandomElementFromArray(getOpenGraphImagesMetadata()).src;
116-
11757
/*-------------------------------- base64 utils ------------------------------*/
11858

119-
// Satori started to fail with base64 url
12059
export const getBase64Image = async (imagePath: string): Promise<string> => {
12160
const imageData = await fs.readFile(imagePath);
12261

@@ -166,3 +105,59 @@ export const getRandomImagePath = async (folderPath: string): Promise<string> =>
166105

167106
return randomImageWithPath;
168107
};
108+
109+
/*-------------------------------- unused code bellow ------------------------------*/
110+
111+
// todo: this cant work, no urls at build time
112+
// ! must use absolute file paths
113+
export const getTemplatePropsUrl = (frontmatterProps: FrontmatterProps): TemplateProps => {
114+
const { title, heroImage, pageId } = frontmatterProps;
115+
116+
// avatarImage
117+
const avatarImageUrl = avatarImage.src;
118+
119+
// heroImage
120+
let heroImageUrl: string;
121+
122+
switch (true) {
123+
// from mdx frontmatter
124+
case Boolean(heroImage.src):
125+
heroImageUrl = `${SITE_URL}${heroImage.src}`; // todo: fix this, path from mdx frontmatter
126+
break;
127+
// hardcoded in 404.mdx frontmatter
128+
case pageId === 'page404':
129+
heroImageUrl = image404.src;
130+
break;
131+
132+
// fallback to random default image
133+
default:
134+
heroImageUrl = `${SITE_URL}${getRandomImageUrl()}`;
135+
break;
136+
}
137+
138+
const templateProps = {
139+
title,
140+
heroImageUrl: heroImageUrl,
141+
avatarImageUrl: avatarImageUrl,
142+
siteUrl: trimHttpProtocol(SITE_URL),
143+
};
144+
145+
return templateProps;
146+
};
147+
148+
/*-------------------------------- url utils (ImageMetadata) ------------------------------*/
149+
150+
export const getOpenGraphImagesMetadata = (): ImageMetadata[] => {
151+
const imageModules = import.meta.glob<{ default: ImageMetadata }>(
152+
// cant be even variable
153+
'/src/assets/images/default/open-graph/*.jpg',
154+
{ eager: true }
155+
);
156+
157+
// convert map to array
158+
const imagesMetadata = Object.keys(imageModules).map((path) => imageModules[path].default);
159+
160+
return imagesMetadata;
161+
};
162+
163+
export const getRandomImageUrl = () => getRandomElementFromArray(getOpenGraphImagesMetadata()).src;

src/pages/api/open-graph/[...route].png.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ export const GET: APIRoute = async ({ props }: APIContext) => {
3030
// resize images in template in CSS only, not in sharp
3131

3232
const templateProps = await getTemplatePropsBase64(props.page);
33-
// const templateProps = getTemplatePropsUrl(props.page);
3433

3534
const fontData = await fs.readFile(`${FONTS_FOLDER}inter-regular.woff`);
3635

0 commit comments

Comments
 (0)