Skip to content

Open source CapCut alternative.

TypeScript

feat(site): blog pages serve fully static after a publish

David Le21ef37d11cc3
diff --git a/site/next.config.ts b/site/next.config.ts
index 64f265b3..b4f1a37e 100644
--- a/site/next.config.ts
+++ b/site/next.config.ts
@@ -14,6 +14,11 @@ const nextConfig: NextConfig = {
// the store holds this route's own project, and usePlayback disposes decoders
// and the audio mixer from an effect cleanup.
cacheComponents: true,
+ // One App Shell per route is prefetched and shared by every link to it,
+ // and a route visited at an address the build did not list (a blog post
+ // published after the deploy) is rendered in the background on that first
+ // visit and served fully static from then on.
+ partialPrefetching: true,
// Cut (the video editor) uploads large media. Two independent limits apply:
// its media route reads req.formData() (a route handler), so it isn't covered
// by serverActions.bodySizeLimit; and src/proxy.ts runs on /api/cut/* on every
diff --git a/site/src/lib/blog/revalidate.ts b/site/src/lib/blog/revalidate.ts
index adb9e1a2..427e3286 100644
--- a/site/src/lib/blog/revalidate.ts
+++ b/site/src/lib/blog/revalidate.ts
@@ -2,11 +2,31 @@
// page drop their copies at once. Expired outright: a route handler cannot use
// updateTag, and the stale-while-revalidate profile would show the first
// visitor after a publish the page from before it.
+//
+// Then the pages are fetched once after the response goes out. That visit is
+// what makes Next render the index and the post again with everything cached,
+// so the next reader gets a finished static page, never the shell with its
+// skeleton, including for a post published after the deploy.
import { revalidateTag } from "next/cache";
+import { after } from "next/server";
+import { CUT_HOSTED_ORIGIN } from "@/cut/lib/hosts";
+import { blogPath } from "@/lib/blog/keys";
import { BLOG_TAG, blogPostTag } from "@/lib/blog/read";
export function revalidateBlog(slugs: string[]): void {
+ const unique = new Set(slugs);
revalidateTag(BLOG_TAG, { expire: 0 });
- for (const slug of new Set(slugs)) revalidateTag(blogPostTag(slug), { expire: 0 });
+ for (const slug of unique) revalidateTag(blogPostTag(slug), { expire: 0 });
+ after(async () => {
+ const paths = ["/blog", ...Array.from(unique, blogPath)];
+ await Promise.all(
+ paths.map((path) =>
+ fetch(`${CUT_HOSTED_ORIGIN}${path}`, { cache: "no-store", headers: { "x-blog-warm": "1" } }).then(
+ (response) => response.body?.cancel(),
+ () => undefined,
+ ),
+ ),
+ );
+ });
}