Partial Prerendering (PPR) & Caching

Swift Rust combines pre-computed static HTML shells with dynamic server streams using Partial Prerendering (PPR) and fine-grained cache directives.

Partial Prerendering (PPR)

PPR serves a static HTML shell instantly from the edge while streaming dynamic holes in parallel over the same HTTP connection.

app/product/[id]/page.tsx
import { Suspense } from "react";
import { StaticHeader } from "./header";
import { DynamicInventory } from "./inventory";

export const experimental_ppr = true;

export default function ProductPage({ params }: { params: { id: string } }) {
  return (
    <div>
      {/* Pre-rendered instantly into the static shell */}
      <StaticHeader />

      {/* Streamed dynamically through the Rust server socket */}
      <Suspense fallback={<div>Loading stock...</div>}>
        <DynamicInventory productId={params.id} />
      </Suspense>
    </div>
  );
}

The "use cache" Directive

Annotate async functions or components with "use cache" to memoize expensive database operations or external API calls:

app/data.ts
export async function getTopStories() {
  "use cache";
  return await fetchNewsFromUpstream();
}

On-Demand Revalidation

Invalidate cached items programmatically using revalidatePath or revalidateTag:

app/api/revalidate/route.ts
import { revalidateTag, revalidatePath } from "swift-rust";

export async function POST(req: Request) {
  const { tag, path } = await req.json();

  if (tag) revalidateTag(tag);
  if (path) revalidatePath(path);

  return Response.json({ revalidated: true, now: Date.now() });
}