Skip to content

Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.

⚡️ Stream document shell before session gates resolve (#2858)

Luke Vella5552b1b4a304
diff --git a/CLAUDE.md b/CLAUDE.md
index b4c84d395..741ff45e0 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -162,6 +162,12 @@ Always use gitmoji prefixes in commit messages. Follow the gitmoji convention (h
- Keep component props minimal — pass only the bare minimum information needed
- Add `"use client"` directive to the top of any `.tsx` file that requires client-side JavaScript
+### Streaming & Server Data Access
+These rules prepare the app for Next.js `cacheComponents` (static shell + streamed dynamic content). Follow them in all new and touched server code:
+- Never await runtime data (session, `cookies()`, `headers()`, tRPC prefetches) at the top of a layout or page. Extract the awaits into a private async component rendered under a `<Suspense>` boundary (see the gate pattern in `app/[locale]/(space)/layout.tsx`) so the shell streams immediately.
+- Pass `params`/`searchParams` promises down into Suspense-wrapped children and await them there, instead of awaiting at the top of the page.
+- Don't call argless `dayjs()`, `new Date()`, `Date.now()`, or `Math.random()` during server render outside request-bound components — synchronous IO fails prerendering once `cacheComponents` is enabled. Compute "now" on the client or behind a Suspense boundary after `connection()`.
+
### File Organization
- Route handlers follow Next.js App Router conventions
- Always use kebab-case for file names
diff --git a/apps/web/src/app/[locale]/(optional-space)/layout.tsx b/apps/web/src/app/[locale]/(optional-space)/layout.tsx
index a25f1260f..ea9acc837 100644
--- a/apps/web/src/app/[locale]/(optional-space)/layout.tsx
+++ b/apps/web/src/app/[locale]/(optional-space)/layout.tsx
@@ -1,4 +1,6 @@
import { dehydrate, HydrationBoundary } from "@tanstack/react-query";
+import { Suspense } from "react";
+import { RouterLoadingIndicator } from "@/components/router-loading-indicator";
import { SessionRefresher } from "@/components/session-refresher";
import { PayWall } from "@/features/billing/components/pay-wall";
import { isQuickCreateEnabled } from "@/features/quick-create/constants";
@@ -12,11 +14,9 @@ import {
createPublicSSRHelper,
} from "@/trpc/server/create-ssr-helper";
-export default async function Layout({
- children,
-}: {
- children: React.ReactNode;
-}) {
+// The session and tRPC prefetch awaits sit below the Suspense boundary in
+// the default export so the document shell can flush before they resolve.
+async function OptionalSpaceGate({ children }: { children: React.ReactNode }) {
const helpers = isQuickCreateEnabled
? await createPublicSSRHelper()
: await createPrivateSSRHelper();
@@ -51,3 +51,11 @@ export default async function Layout({
</HydrationBoundary>
);
}
+
+export default function Layout({ children }: { children: React.ReactNode }) {
+ return (
+ <Suspense fallback={<RouterLoadingIndicator />}>
+ <OptionalSpaceGate>{children}</OptionalSpaceGate>
+ </Suspense>
+ );
+}
diff --git a/apps/web/src/app/[locale]/(space)/layout.tsx b/apps/web/src/app/[locale]/(space)/layout.tsx
index fd8d85982..3374920a9 100644
--- a/apps/web/src/app/[locale]/(space)/layout.tsx
+++ b/apps/web/src/app/[locale]/(space)/layout.tsx
@@ -1,3 +1,5 @@
+import { Suspense } from "react";
+import { RouterLoadingIndicator } from "@/components/router-loading-indicator";
import { SessionRefresher } from "@/components/session-refresher";
import { PayWall } from "@/features/billing/components/pay-wall";
import { SpaceProvider } from "@/features/space/client";
@@ -8,11 +10,10 @@ import { getLocale } from "@/i18n/server/get-locale";
import { getSession } from "@/lib/auth";
import { DateTimeProvider } from "@/lib/datetime/client";
-export default async function Layout({
- children,
-}: {
- children: React.ReactNode;
-}) {
+// The session gate awaits below this boundary so the document shell can
+// flush before the session store responds; without it, every hard load of
+// a space route streams nothing until getActiveSpace resolves.
+async function SpaceGate({ children }: { children: React.ReactNode }) {
const [locale, session, space] = await Promise.all([
getLocale(),
getSession(),
@@ -41,3 +42,11 @@ export default async function Layout({
</>
);
}
+
+export default function Layout({ children }: { children: React.ReactNode }) {
+ return (
+ <Suspense fallback={<RouterLoadingIndicator />}>
+ <SpaceGate>{children}</SpaceGate>
+ </Suspense>
+ );
+}
diff --git a/apps/web/src/app/[locale]/control-panel/layout.tsx b/apps/web/src/app/[locale]/control-panel/layout.tsx
index e92a70bc6..7a97c98f5 100644
--- a/apps/web/src/app/[locale]/control-panel/layout.tsx
+++ b/apps/web/src/app/[locale]/control-panel/layout.tsx
@@ -2,6 +2,8 @@ import { Icon } from "@rallly/ui/icon";
import { SidebarInset, SidebarTrigger } from "@rallly/ui/sidebar";
import { GaugeIcon } from "lucide-react";
import type { Metadata } from "next";
+import { Suspense } from "react";
+import { RouterLoadingIndicator } from "@/components/router-loading-indicator";
import { LicenseLimitWarning } from "@/features/licensing/components/license-limit-warning";
import { CommandMenu } from "@/features/navigation/components/command-menu";
import { UserProvider } from "@/features/user/client";
@@ -13,11 +15,9 @@ import { createAdminSSRHelper } from "@/trpc/server/create-ssr-helper";
import { ControlPanelSidebarProvider } from "./control-panel-sidebar-provider";
import { ControlPanelSidebar } from "./sidebar";
-export default async function AdminLayout({
- children,
-}: {
- children: React.ReactNode;
-}) {
+// The admin gate awaits below the Suspense boundary in the default export
+// so the document shell can flush before the session store responds.
+async function AdminGate({ children }: { children: React.ReactNode }) {
const [locale, { user }] = await Promise.all([
getLocale(),
createAdminSSRHelper(),
@@ -59,6 +59,18 @@ export default async function AdminLayout({
);
}
+export default function AdminLayout({
+ children,
+}: {
+ children: React.ReactNode;
+}) {
+ return (
+ <Suspense fallback={<RouterLoadingIndicator />}>
+ <AdminGate>{children}</AdminGate>
+ </Suspense>
+ );
+}
+
export async function generateMetadata({
params,
}: {