-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Automatically generate a new Open Graph image on every build
This took some fiddling to get right, and still depends on an arbitrary wait to allow the page to render completely, so will want some proper monitoring when running in the GitHub Actions CD environment.
- Loading branch information
1 parent
7c1796e
commit ed6a18a
Showing
6 changed files
with
111 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { spawn } from "child_process"; | ||
|
||
import { pEventIterator } from "p-event"; | ||
import { chromium } from "playwright"; | ||
|
||
import type { AstroIntegration } from "astro"; | ||
|
||
const delay = (milliseconds: number) => | ||
new Promise((resolve) => { | ||
setTimeout(resolve, milliseconds); | ||
}); | ||
|
||
const memewallOgImage = (): AstroIntegration => ({ | ||
name: "memewall-og-image", | ||
hooks: { | ||
"astro:build:start": async () => { | ||
const astroServerProcess = spawn("pnpm", ["dev"]); | ||
let astroServerUrl: string; | ||
|
||
const asyncIterator = pEventIterator(astroServerProcess.stdout, "data"); | ||
for await (const chunk of asyncIterator) { | ||
// wait for the Astro dev server to be ready and to report its URL | ||
if ( | ||
(astroServerUrl = chunk | ||
.toString() | ||
.match(/http:\/\/[\w\d]+:\d+\//)?.[0]) | ||
) { | ||
break; | ||
} | ||
} | ||
|
||
console.log(` --> Spawned Astro server listening at ${astroServerUrl}`); | ||
|
||
const browser = await chromium.launch({ | ||
args: [ | ||
"--no-sandbox", | ||
"--disable-setuid-sandbox", | ||
"--disable-web-security", | ||
], | ||
}); | ||
|
||
const page = await browser.newPage(); | ||
await page.setViewportSize({ width: 1200, height: 630 }); | ||
await page.goto(astroServerUrl, { waitUntil: "networkidle" }); | ||
|
||
// `waitUntil: "networkidle"` seems to be inadequate | ||
console.log(` --> Waiting for the page to render before capturing...`); | ||
await delay(1000); | ||
|
||
await page.screenshot({ path: `./assets/open-graph.jpeg` }); | ||
|
||
await browser.close(); | ||
astroServerProcess.kill(); | ||
}, | ||
}, | ||
}); | ||
|
||
export { memewallOgImage }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters