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

Switch over to using og:image generating service #12

Merged
merged 1 commit into from
Mar 11, 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
2,380 changes: 775 additions & 1,605 deletions package-lock.json

Large diffs are not rendered by default.

13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"start": "astro dev",
"build": "astro build",
"newpost": "node ./scripts/newpost.js",
"postinstall": "patch-package",
"preview": "wrangler pages dev ./dist",
"astro": "astro",
"prettier": "prettier --write .",
Expand All @@ -22,21 +23,21 @@
"typecheck:watch": "tsc --noEmit --watch"
},
"dependencies": {
"@astrojs/cloudflare": "^9.0.2",
"@astrojs/mdx": "^2.0.5",
"@astrojs/rss": "^4.0.2",
"@astrojs/sitemap": "^3.0.5",
"@astrojs/cloudflare": "^9.1.0",
"@astrojs/mdx": "^2.1.1",
"@astrojs/rss": "^4.0.5",
"@astrojs/sitemap": "^3.1.1",
"@astrojs/tailwind": "^5.1.0",
"@daviddarnes/share-button": "^1.1.2",
"@evilmartians/harmony": "^1.2.0",
"@types/sanitize-html": "^2.9.5",
"astro": "^4.2.0",
"astro": "^4.4.15",
"astro-robots-txt": "^1.0.0",
"clsx": "^2.1.0",
"drizzle-orm": "^0.29.4",
"marked": "^11.1.1",
"mdast-util-to-string": "^4.0.0",
"puppeteer": "^21.7.0",
"patch-package": "^8.0.0",
"reading-time": "^1.5.0",
"sanitize-html": "^2.11.0",
"tailwind-merge": "^2.2.0",
Expand Down
29 changes: 29 additions & 0 deletions patches/astro+4.4.15.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
diff --git a/node_modules/astro/dist/runtime/server/render/util.js b/node_modules/astro/dist/runtime/server/render/util.js
index 44fe49e..a55817b 100644
--- a/node_modules/astro/dist/runtime/server/render/util.js
+++ b/node_modules/astro/dist/runtime/server/render/util.js
@@ -70,6 +70,9 @@ Make sure to use the static attribute syntax (\`${key}={value}\`) instead of the
if (key === "className") {
return markHTMLString(` class="${toAttributeString(value, shouldEscape)}"`);
}
+ if (typeof value === 'string' && value.includes('&') && urlCanParse(value)) {
+ return markHTMLString(` ${key}="${toAttributeString(value, false)}"`);
+ }
if (value === true && (key.startsWith("data-") || htmlBooleanAttributes.test(key))) {
return markHTMLString(` ${key}`);
} else {
@@ -131,6 +134,14 @@ function promiseWithResolvers() {
reject
};
}
+function urlCanParse(url) {
+ try {
+ new URL(url, 'http://example.com');
+ return true;
+ } catch {
+ return false;
+ }
+}
export {
addAttribute,
defineScriptVars,
32 changes: 15 additions & 17 deletions src/components/BaseHead.astro
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,16 @@ export interface Props {

const canonicalURL = new URL(Astro.url.pathname, Astro.site);

const { title, description, image = "/open-graph/og-image.png" } = Astro.props;
let { title, description, image } = Astro.props;

if (!image) {
const searchParams = new URLSearchParams();
searchParams.set("title", title);
if (description) {
searchParams.set("description", description);
}
image = "https://og-images.kevinkipp.com/" + "?" + searchParams.toString();
}
---

<head>
Expand All @@ -20,8 +29,6 @@ const { title, description, image = "/open-graph/og-image.png" } = Astro.props;
<meta name="generator" content={Astro.generator} />
<meta name="author" content="Kevin Kipp" />

<!-- Font preloads -->

<!-- Canonical URL -->
<link rel="canonical" href={canonicalURL} />

Expand All @@ -38,26 +45,17 @@ const { title, description, image = "/open-graph/og-image.png" } = Astro.props;
<meta property="og:site_name" content="Kevin Kipp" />
<meta property="og:title" content={title} />
{description && <meta property="og:description" content={description} />}
{
image && (
<>
<meta property="og:image" content={new URL(image, Astro.url)} />
<meta property="og:image:width" content="1200" />
<meta property="og:image:height" content="630" />
</>
)
}

<meta property="og:image" content={image} />
<meta property="og:image:width" content="1200" />
<meta property="og:image:height" content="630" />

<!-- Twitter -->
<meta property="twitter:card" content="summary_large_image" />
<meta property="twitter:url" content={Astro.url} />
<meta property="twitter:title" content={title} />
{description && <meta property="twitter:description" content={description} />}
{
image && (
<meta property="twitter:image" content={new URL(image, Astro.url)} />
)
}
<meta property="twitter:image" content={image} />

<link
rel="alternate"
Expand Down
6 changes: 1 addition & 5 deletions src/layouts/BlogPost.astro
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@ const { title, description, published, updatedDate, heroImage, heroAlt } = data;
---

<Html>
<BaseHead
title={title}
description={description}
image={`/open-graph${Astro.url.pathname}og-image.png`}
/>
<BaseHead title={title} description={description} />
<Body>
<Header />
<Main>
Expand Down
6 changes: 0 additions & 6 deletions src/pages/blog/[...slug].astro
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import { type CollectionEntry } from "astro:content";
import { getPublishedBlogPosts } from "../../content/blog/_getPublishedBlogPosts";
import BlogPost from "../../layouts/BlogPost.astro";
import { generateOgImage } from "../../utils/ogImages/generateOgImage";

interface Props {
post: CollectionEntry<"blog">;
Expand All @@ -18,11 +17,6 @@ export async function getStaticPaths() {

const { post } = Astro.props;
const { Content, remarkPluginFrontmatter } = await post.render();
await generateOgImage({
title: post.data.title,
description: post.data.description,
pathname: Astro.url.pathname,
});
---

<BlogPost minutesRead={remarkPluginFrontmatter.minutesRead} post={post}>
Expand Down
7 changes: 0 additions & 7 deletions src/pages/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import { SITE_DESCRIPTION, SITE_TITLE } from "../consts";
import { getPublishedBlogPosts } from "../content/blog/_getPublishedBlogPosts";
import Html from "../layouts/Html.astro";
import ColumnWrapper from "../components/ColumnWrapper.astro";
import { generateOgImage } from "../utils/ogImages/generateOgImage";

const { src } = await getImage({
src: profile,
Expand All @@ -26,12 +25,6 @@ const posts = await getPublishedBlogPosts().then(
// only show the first 5 posts
(posts) => posts.filter((_, index) => index < 10),
);

await generateOgImage({
title: "👋🏻 Heyo! I'm Kevin.",
description: "UI Engineer based in Austin, TX.",
pathname: "/",
});
---

<Html>
Expand Down
59 changes: 0 additions & 59 deletions src/utils/ogImages/generateOgImage.ts

This file was deleted.

104 changes: 0 additions & 104 deletions src/utils/ogImages/template.ts

This file was deleted.