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

@cloudflare/pages-plugin-vercel-ogで実装しなおしてみる #107

Merged
merged 5 commits into from
Sep 16, 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
221 changes: 0 additions & 221 deletions .astro/astro/content.d.ts

This file was deleted.

1 change: 0 additions & 1 deletion .astro/integrations/_inox-tools_astro-when/types.d.ts

This file was deleted.

5 changes: 0 additions & 5 deletions .astro/settings.json

This file was deleted.

3 changes: 0 additions & 3 deletions .astro/types.d.ts

This file was deleted.

2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ pnpm-debug.log*
test-results/
playwright-report/
playwright/.cache/

.astro/
2 changes: 1 addition & 1 deletion astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default defineConfig({
site: "https://yukky-sandbox.dev/",
vite: {
optimizeDeps: {
exclude: ["fsevents"],
exclude: ["fsevents", "@cloudflare/pages-plugin-vercel-og"],
},
},
output: "hybrid",
Expand Down
Binary file modified bun.lockb
Binary file not shown.
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,21 @@
"@astrojs/react": "^3.6.2",
"@astrojs/rss": "^4.0.7",
"@astrojs/tailwind": "^5.1.0",
"@cloudflare/pages-plugin-vercel-og": "^0.1.2",
"@formkit/tempo": "^0.1.2",
"@types/react": "^18.3.5",
"@types/react-dom": "^18.3.0",
"astro": "4.15.6",
"microcms-js-sdk": "^3.0.0",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"satori": "^0.11.0",
"sharp": "^0.33.5",
"tailwindcss": "^3.3.3",
"typescript-eslint": "^7.2.0"
},
"devDependencies": {
"@playwright/test": "^1.36.2",
"@tailwindcss/typography": "^0.5.9",
"@types/bun": "^1.0.8",
"@types/sharp": "^0.32.0",
"eslint": "^8.46.0",
"eslint-config-prettier": "^9.1.0",
"eslint-config-standard-with-typescript": "^43.0.1",
Expand Down
77 changes: 47 additions & 30 deletions src/pages/og/_OgImage.tsx → src/functions/post/_middleware.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import satori from "satori";
import sharp from "sharp";
import React from "react";
import vercelOGPagesPlugin from "@cloudflare/pages-plugin-vercel-og";

interface OgImageProps {
text: string;
ogTitle: string;
}

const OgImage = ({ text }: OgImageProps): JSX.Element => {
const OgImage = ({ ogTitle }: OgImageProps): JSX.Element => {
return (
<div
style={{
Expand Down Expand Up @@ -36,7 +36,7 @@ const OgImage = ({ text }: OgImageProps): JSX.Element => {
paddingLeft: "48px",
}}
>
<h1>{text}</h1>
<h1>{ogTitle}</h1>
<div
style={{
display: "flex",
Expand Down Expand Up @@ -82,31 +82,6 @@ const OgImage = ({ text }: OgImageProps): JSX.Element => {
</div>
);
};
export async function getOgImage(text: string): Promise<Buffer> {
const notoSansJpUrl = `https://fonts.googleapis.com/css2?family=Noto+Sans+JP:wght@600`;
const reggeaeOneUlr =
"https://fonts.googleapis.com/css2?family=Reggae+One&display=swap&text=ゆっきーの砂場";
const notoSansJpFontData = await getFontData(notoSansJpUrl);
const reggeaeOneFontData = await getFontData(reggeaeOneUlr);
const svg = await satori(<OgImage text={text} />, {
width: 800,
height: 400,
fonts: [
{
name: "Noto Sans JP",
data: notoSansJpFontData,
style: "normal",
},
{
name: "Reggae One",
data: reggeaeOneFontData,
style: "normal",
},
],
});

return await sharp(Buffer.from(svg)).png().toBuffer();
}

const getFontData = async (url: string): Promise<ArrayBuffer> => {
const css = await (
Expand All @@ -128,3 +103,45 @@ const getFontData = async (url: string): Promise<ArrayBuffer> => {

return await fetch(resource[1]).then(async (res) => await res.arrayBuffer());
};

export const onRequest = async () => {
const notoSansJpUrl = `https://fonts.googleapis.com/css2?family=Noto+Sans+JP:wght@600`;
const reggeaeOneUlr =
"https://fonts.googleapis.com/css2?family=Reggae+One&display=swap&text=ゆっきーの砂場";
const notoSansJpFontData = await getFontData(notoSansJpUrl);
const reggeaeOneFontData = await getFontData(reggeaeOneUlr);
return vercelOGPagesPlugin<OgImageProps>({
imagePathSuffix: "/og-image.png",
component: ({ ogTitle }) => {
return <OgImage ogTitle={ogTitle} />;
},
extractors: {
on: {
'meta[property="og:title"]': (props) => ({
element(element) {
props.ogTitle = element.getAttribute("content");
},
}),
},
},
options: {
width: 800,
height: 400,
fonts: [
{
name: "Noto Sans JP",
data: notoSansJpFontData,
style: "normal",
},
{
name: "Reggae One",
data: reggeaeOneFontData,
style: "normal",
},
],
},
autoInject: {
openGraph: true,
},
});
};
Loading
Loading