Skip to content

perf: og image caching v1#23180

Merged
hbjORbj merged 3 commits intomainfrom
perf/og-image-caching-v1
Aug 19, 2025
Merged

perf: og image caching v1#23180
hbjORbj merged 3 commits intomainfrom
perf/og-image-caching-v1

Conversation

@hbjORbj
Copy link
Contributor

@hbjORbj hbjORbj commented Aug 19, 2025

What does this PR do?

  • Add browser caching header to OG image generation endpoint (/api/social/og/image)

Mandatory Tasks (DO NOT REMOVE)

  • I have self-reviewed the code (A decent size PR without self-review might be rejected).
  • N/A - I have updated the developer docs in /docs if this PR makes changes that would require a documentation change. If N/A, write N/A here and check the checkbox.
  • I confirm automated tests are in place that prove my fix is effective or that my feature works.

How should this be tested?

Tested already:

Screenshot 2025-08-19 at 7 40 49 PM

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Aug 19, 2025

Walkthrough

The change standardizes 200 responses in apps/web/app/api/social/og/image/route.tsx: meeting, app, and generic image routes now return Content-Type: image/png and Cache-Control: public, max-age=3600, stale-while-revalidate=86400. Error (400) handling and response bodies were not modified. No exported/public signatures changed.

Tip

🔌 Remote MCP (Model Context Protocol) integration is now available!

Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between d66e4f3 and dde6147.

📒 Files selected for processing (1)
  • apps/web/app/api/social/og/image/route.tsx (3 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • apps/web/app/api/social/og/image/route.tsx
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Install dependencies / Yarn install & cache
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch perf/og-image-caching-v1

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@graphite-app graphite-app bot requested a review from a team August 19, 2025 09:56
@keithwillcode keithwillcode added core area: core, team members only foundation labels Aug 19, 2025
@dosubot dosubot bot added the performance area: performance, page load, slow, slow endpoints, loading screen, unresponsive label Aug 19, 2025
@graphite-app
Copy link

graphite-app bot commented Aug 19, 2025

Graphite Automations

"Add consumer team as reviewer" took an action on this PR • (08/19/25)

1 reviewer was added to this PR based on Keith Williams's automation.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (9)
apps/web/app/api/social/og/image/route.tsx (9)

90-93: Add s-maxage for shared/CDN caching (keep max-age for browsers).

To ensure Vercel/edge CDN uses the intended TTL (not just browsers), include s-maxage alongside max-age. Optional: also set CDN-Cache-Control for Vercel to give CDN-specific directives.

 return new Response(img.body, {
   status: 200,
   headers: {
     "Content-Type": "image/png",
-    "Cache-Control": "public, max-age=3600, stale-while-revalidate=86400",
+    "Cache-Control": "public, max-age=3600, s-maxage=3600, stale-while-revalidate=86400",
+    // "CDN-Cache-Control": "public, s-maxage=3600, stale-while-revalidate=86400", // optional for Vercel
   },
 });

105-108: Avoid long-lived caching for 400 errors; prefer no-store or a very short TTL.

Caching 400 responses for an hour can lock in client mistakes (or schema iteration) and make debugging harder. For validation errors, consider not caching or using a small TTL (e.g., 30–60s).

 headers: {
   "Content-Type": "application/json",
-  "Cache-Control": "public, max-age=3600, stale-while-revalidate=86400",
+  "Cache-Control": "no-store",
 },

If you intentionally want caching, consider: public, max-age=60, s-maxage=60, must-revalidate.


135-138: Same for app 400 responses: don’t cache for long (align policy across 4xx).

Mirror the meeting change: use no-store or a short TTL to avoid sticky client-side mistakes and improve iteration.

 headers: {
   "Content-Type": "application/json",
-  "Cache-Control": "public, max-age=3600, stale-while-revalidate=86400",
+  "Cache-Control": "no-store",
 },

125-125: Apply the same Cache-Control to app images (200) for consistency and perf.

Success responses for "app" images should get the same caching as "meeting" to realize CDN perf gains.

-return new Response(img.body, { status: 200, headers: { "Content-Type": "image/png" } });
+return new Response(img.body, {
+  status: 200,
+  headers: {
+    "Content-Type": "image/png",
+    "Cache-Control": "public, max-age=3600, s-maxage=3600, stale-while-revalidate=86400",
+  },
+});

156-156: Also add Cache-Control to generic images (200).

Keep caching behavior uniform across all image types.

-return new Response(img.body, { status: 200, headers: { "Content-Type": "image/png" } });
+return new Response(img.body, {
+  status: 200,
+  headers: {
+    "Content-Type": "image/png",
+    "Cache-Control": "public, max-age=3600, s-maxage=3600, stale-while-revalidate=86400",
+  },
+});

165-167: Align generic 400 with chosen 4xx policy (recommend no-store).

Other 4xx responses are being adjusted; generic should follow the same rule.

-headers: { "Content-Type": "application/json" },
+headers: {
+  "Content-Type": "application/json",
+  "Cache-Control": "no-store",
+},

174-176: Set explicit Cache-Control for 404 (avoid caching errors).

404s here are dynamic (query-driven). Recommend no-store to prevent caching transient misses.

-return new Response("What you're looking for is not here..", { status: 404 });
+return new Response("What you're looking for is not here..", {
+  status: 404,
+  headers: {
+    "Content-Type": "text/plain; charset=utf-8",
+    "Cache-Control": "no-store",
+  },
+});

177-179: Do not cache 500 responses.

500s should never be cached; add no-store and a Content-Type.

-return new Response("Internal server error", { status: 500 });
+return new Response("Internal server error", {
+  status: 500,
+  headers: {
+    "Content-Type": "text/plain; charset=utf-8",
+    "Cache-Control": "no-store",
+  },
+});

37-43: Optional perf: cache fonts across invocations to reduce per-request latency.

Fetching fonts on every request adds avoidable overhead. In edge runtimes, module-level state often persists for the instance lifetime. Hoist the fetch into a module-level promise and reuse.

Example (outside the current ranges, for illustration):

// top-level
const fontsPromise = (async () => {
  const [cal, interRegular, interMedium] = await Promise.allSettled([
    fetch(new URL("/fonts/cal.ttf", WEBAPP_URL)).then((r) => r.arrayBuffer()),
    fetch(new URL("/fonts/Inter-Regular.ttf", WEBAPP_URL)).then((r) => r.arrayBuffer()),
    fetch(new URL("/fonts/Inter-Medium.ttf", WEBAPP_URL)).then((r) => r.arrayBuffer()),
  ]);

  const fonts: SatoriOptions["fonts"] = [];
  if (interRegular.status === "fulfilled") fonts.push({ name: "inter", data: interRegular.value, weight: 400 });
  if (interMedium.status === "fulfilled") fonts.push({ name: "inter", data: interMedium.value, weight: 500 });
  if (cal.status === "fulfilled") {
    fonts.push({ name: "cal", data: cal.value, weight: 400 });
    fonts.push({ name: "cal", data: cal.value, weight: 600 });
  }
  return fonts;
})();

// in handler
const fonts = await fontsPromise;

Also applies to: 44-57

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 9e9f815 and d66e4f3.

📒 Files selected for processing (1)
  • apps/web/app/api/social/og/image/route.tsx (3 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
**/*.tsx

📄 CodeRabbit Inference Engine (.cursor/rules/review.mdc)

Always use t() for text localization in frontend code; direct text embedding should trigger a warning

Files:

  • apps/web/app/api/social/og/image/route.tsx
**/*.{ts,tsx}

📄 CodeRabbit Inference Engine (.cursor/rules/review.mdc)

Flag excessive Day.js use in performance-critical code; prefer native Date or Day.js .utc() in hot paths like loops

Files:

  • apps/web/app/api/social/og/image/route.tsx

@vercel
Copy link

vercel bot commented Aug 19, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

2 Skipped Deployments
Project Deployment Preview Comments Updated (UTC)
cal Ignored Ignored Aug 19, 2025 10:08am
cal-eu Ignored Ignored Aug 19, 2025 10:08am

@github-actions
Copy link
Contributor

github-actions bot commented Aug 19, 2025

E2E results are ready!

@hbjORbj hbjORbj merged commit 02e486d into main Aug 19, 2025
84 of 87 checks passed
@hbjORbj hbjORbj deleted the perf/og-image-caching-v1 branch August 19, 2025 11:54
@coderabbitai coderabbitai bot mentioned this pull request Aug 20, 2025
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core area: core, team members only foundation performance area: performance, page load, slow, slow endpoints, loading screen, unresponsive ready-for-e2e

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants