Skip to content

armgit / bygga

Public Imported from GitHub
0

IKEA assembly, step by step: animated, manual-style 3D guides. Scan any paper manual with Together AI.

TypeScript

Share cards cached for real; drafted products get their own drawing

Youssef9f846277ee19
diff --git a/next.config.ts b/next.config.ts
index d241d5a..6dbfb65 100644
--- a/next.config.ts
+++ b/next.config.ts
@@ -50,11 +50,19 @@ const nextConfig: NextConfig = {
{
// share cards only change on deploy: let the CDN keep them, so a viral link renders each card once
source: "/:path(opengraph-image|twitter-image)",
- headers: [{ key: "Cache-Control", value: "public, max-age=3600, s-maxage=31536000, stale-while-revalidate=86400" }],
+ headers: [
+ { key: "Cache-Control", value: "public, max-age=3600, s-maxage=31536000, stale-while-revalidate=86400" },
+ // Next sets its own Cache-Control on dynamic images; Vercel's CDN reads this one first
+ { key: "Vercel-CDN-Cache-Control", value: "max-age=31536000, stale-while-revalidate=86400" },
+ ],
},
{
source: "/build/:slug/:img(opengraph-image|twitter-image)",
- headers: [{ key: "Cache-Control", value: "public, max-age=3600, s-maxage=31536000, stale-while-revalidate=86400" }],
+ headers: [
+ { key: "Cache-Control", value: "public, max-age=3600, s-maxage=31536000, stale-while-revalidate=86400" },
+ // Next sets its own Cache-Control on dynamic images; Vercel's CDN reads this one first
+ { key: "Vercel-CDN-Cache-Control", value: "max-age=31536000, stale-while-revalidate=86400" },
+ ],
},
{
// the service worker itself must never be stale
diff --git a/src/app/build/[slug]/opengraph-image.tsx b/src/app/build/[slug]/opengraph-image.tsx
index 0ecc979..2806f4c 100644
--- a/src/app/build/[slug]/opengraph-image.tsx
+++ b/src/app/build/[slug]/opengraph-image.tsx
@@ -1,32 +1,10 @@
-import { catalogSpec } from "@/lib/catalog";
-import { buildPlan } from "@/lib/plan";
-import { KIND_LABELS } from "@/lib/products";
-import { kindOf } from "@/lib/search";
-import { OG_SIZE, drawing, guideCard } from "@/lib/og/card";
-
-/** A drawing to fall back on for each archetype. */
-const BY_ARCH: Record<string, string> = { shelf: "bookcase", drawers: "chest", table: "dining", bed: "bed", cabinet: "cabinet", seat: "chair", sofa: "sofa", custom: "cabinet" };
+import { OG_SIZE } from "@/lib/og/card";
+import { guideImage } from "@/lib/og/guide";
export const alt = "An IKEA assembly guide on Bygga: the product drawn in 3D, with every step read out loud";
export const size = OG_SIZE;
export const contentType = "image/png";
export default async function Image({ params }: { params: Promise<{ slug: string }> }) {
- const slug = decodeURIComponent((await params).slug);
- const spec = catalogSpec(slug);
- if (!spec) {
- // a product we draft on the fly: its name, and the drawing of the kind its slug names, if any
- const name = slug.split("-")[0].toUpperCase().slice(0, 18);
- const kind = kindOf(slug.replace(/-/g, " "));
- return guideCard({ name, variant: "Assembly, step by step", steps: null, art: await drawing(kind ? `kind-${kind}` : "kind-bookcase") });
- }
- const kind = kindOf(`${spec.category} ${spec.variant}`) ?? KIND_LABELS[0].kind;
- const art = (await drawing(slug)) ?? (await drawing(`kind-${kind}`)) ?? (await drawing(`kind-${BY_ARCH[spec.archetype]}`));
- let steps: number | null = null;
- try {
- steps = buildPlan(spec).steps.length;
- } catch {
- /* the card still works without a count */
- }
- return guideCard({ name: spec.name, variant: spec.variant, steps, art });
+ return guideImage(decodeURIComponent((await params).slug));
}
diff --git a/src/app/build/[slug]/twitter-image.tsx b/src/app/build/[slug]/twitter-image.tsx
index 0ecc979..2806f4c 100644
--- a/src/app/build/[slug]/twitter-image.tsx
+++ b/src/app/build/[slug]/twitter-image.tsx
@@ -1,32 +1,10 @@
-import { catalogSpec } from "@/lib/catalog";
-import { buildPlan } from "@/lib/plan";
-import { KIND_LABELS } from "@/lib/products";
-import { kindOf } from "@/lib/search";
-import { OG_SIZE, drawing, guideCard } from "@/lib/og/card";
-
-/** A drawing to fall back on for each archetype. */
-const BY_ARCH: Record<string, string> = { shelf: "bookcase", drawers: "chest", table: "dining", bed: "bed", cabinet: "cabinet", seat: "chair", sofa: "sofa", custom: "cabinet" };
+import { OG_SIZE } from "@/lib/og/card";
+import { guideImage } from "@/lib/og/guide";
export const alt = "An IKEA assembly guide on Bygga: the product drawn in 3D, with every step read out loud";
export const size = OG_SIZE;
export const contentType = "image/png";
export default async function Image({ params }: { params: Promise<{ slug: string }> }) {
- const slug = decodeURIComponent((await params).slug);
- const spec = catalogSpec(slug);
- if (!spec) {
- // a product we draft on the fly: its name, and the drawing of the kind its slug names, if any
- const name = slug.split("-")[0].toUpperCase().slice(0, 18);
- const kind = kindOf(slug.replace(/-/g, " "));
- return guideCard({ name, variant: "Assembly, step by step", steps: null, art: await drawing(kind ? `kind-${kind}` : "kind-bookcase") });
- }
- const kind = kindOf(`${spec.category} ${spec.variant}`) ?? KIND_LABELS[0].kind;
- const art = (await drawing(slug)) ?? (await drawing(`kind-${kind}`)) ?? (await drawing(`kind-${BY_ARCH[spec.archetype]}`));
- let steps: number | null = null;
- try {
- steps = buildPlan(spec).steps.length;
- } catch {
- /* the card still works without a count */
- }
- return guideCard({ name: spec.name, variant: spec.variant, steps, art });
+ return guideImage(decodeURIComponent((await params).slug));
}
diff --git a/src/lib/og/card.tsx b/src/lib/og/card.tsx
index 17b2475..96c8640 100644
--- a/src/lib/og/card.tsx
+++ b/src/lib/og/card.tsx
@@ -23,16 +23,13 @@ const INK_2 = "#55534e";
const ACCENT = "#ffd21f";
const PAD = 72;
+// read once when the module loads, not per request: that keeps the site card static, so it is drawn at build time
const here = join(process.cwd(), "src/lib/og");
-let fonts: Promise<{ name: string; data: Buffer; weight: 500 | 700 | 800; style: "normal" }[]> | null = null;
-function loadFonts() {
- fonts ??= Promise.all([
- readFile(join(here, "display-800.woff")).then((data) => ({ name: "Display", data, weight: 800 as const, style: "normal" as const })),
- readFile(join(here, "sans-500.woff")).then((data) => ({ name: "Sans", data, weight: 500 as const, style: "normal" as const })),
- readFile(join(here, "sans-700.woff")).then((data) => ({ name: "Sans", data, weight: 700 as const, style: "normal" as const })),
- ]);
- return fonts;
-}
+const FONTS = await Promise.all([
+ readFile(join(here, "display-800.woff")).then((data) => ({ name: "Display", data, weight: 800 as const, style: "normal" as const })),
+ readFile(join(here, "sans-500.woff")).then((data) => ({ name: "Sans", data, weight: 500 as const, style: "normal" as const })),
+ readFile(join(here, "sans-700.woff")).then((data) => ({ name: "Sans", data, weight: 700 as const, style: "normal" as const })),
+]);
/** A drawing from public/og as a data URI, or null. */
export async function drawing(name: string) {
@@ -85,11 +82,13 @@ function Marked({ children, size }: { children: string; size: number }) {
}
function render(node: React.ReactElement) {
- return loadFonts().then((f) => new ImageResponse(node, { ...OG_SIZE, fonts: f }));
+ return new ImageResponse(node, { ...OG_SIZE, fonts: FONTS });
}
-export async function siteCard() {
- const [front, back] = await Promise.all([drawing("hero-bygga"), drawing("hero-paper")]);
+const HERO = await Promise.all([drawing("hero-bygga"), drawing("hero-paper")]);
+
+export function siteCard() {
+ const [front, back] = HERO;
const H = 84;
return render(
<div style={{ width: "100%", height: "100%", display: "flex", position: "relative", background: PAGE, fontFamily: "Sans", color: INK, overflow: "hidden" }}>
@@ -140,7 +139,7 @@ function nameSize(name: string) {
return Math.max(56, Math.min(lines.length > 1 ? 80 : 118, Math.floor(NAME_W / em)));
}
-export async function guideCard(o: { name: string; variant: string; steps: number | null; art: string | null }) {
+export function guideCard(o: { name: string; variant: string; steps: number | null; art: string | null }) {
const size = nameSize(o.name);
const variant = spellOut(o.variant);
return render(
diff --git a/src/lib/og/guide.tsx b/src/lib/og/guide.tsx
new file mode 100644
index 0000000..d748965
--- /dev/null
+++ b/src/lib/og/guide.tsx
@@ -0,0 +1,31 @@
+import { catalogSpec } from "@/lib/catalog";
+import { ikeaType } from "@/lib/ikea";
+import { buildPlan } from "@/lib/plan";
+import { KIND_LABELS } from "@/lib/products";
+import { kindOf } from "@/lib/search";
+import { drawing, guideCard } from "./card";
+
+/** A drawing to fall back on for each archetype. */
+const BY_ARCH: Record<string, string> = { shelf: "bookcase", drawers: "chest", table: "dining", bed: "bed", cabinet: "cabinet", seat: "chair", sofa: "sofa", custom: "cabinet" };
+
+/** The share card for /build/{slug}, shared by its opengraph and twitter images. */
+export async function guideImage(slug: string) {
+ const spec = catalogSpec(slug);
+ if (!spec) {
+ // a product we draft on the fly: its name, and the drawing of the kind IKEA sells it as, or the slug names
+ const name = slug.split("-")[0].toUpperCase().slice(0, 18);
+ const type = await ikeaType(name).catch(() => null);
+ const kind = kindOf(type ?? "") ?? kindOf(slug.replace(/-/g, " "));
+ const art = (kind && (await drawing(`kind-${kind}`))) || (await drawing("hero-bygga"));
+ return guideCard({ name, variant: type ?? "Assembly, step by step", steps: null, art });
+ }
+ const kind = kindOf(`${spec.category} ${spec.variant}`) ?? KIND_LABELS[0].kind;
+ const art = (await drawing(slug)) ?? (await drawing(`kind-${kind}`)) ?? (await drawing(`kind-${BY_ARCH[spec.archetype]}`));
+ let steps: number | null = null;
+ try {
+ steps = buildPlan(spec).steps.length;
+ } catch {
+ /* the card still works without a count */
+ }
+ return guideCard({ name: spec.name, variant: spec.variant, steps, art });
+}