Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: lazy load cookies #1654

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/spotty-emus-rush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"create-t3-app": patch
---

fix: lazy load cookie to prevent layout from being forced dynamic
4 changes: 3 additions & 1 deletion cli/template/extras/src/app/layout/with-trpc-tw.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export const metadata = {
icons: [{ rel: "icon", url: "/favicon.ico" }],
};

const getCookieString = () => Promise.resolve(cookies().toString());

export default function RootLayout({
children,
}: {
Expand All @@ -24,7 +26,7 @@ export default function RootLayout({
return (
<html lang="en">
<body className={`font-sans ${inter.variable}`}>
<TRPCReactProvider cookies={cookies().toString()}>
<TRPCReactProvider cookiePromise={getCookieString()}>
{children}
</TRPCReactProvider>
</body>
Expand Down
4 changes: 3 additions & 1 deletion cli/template/extras/src/app/layout/with-trpc.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export const metadata = {
icons: [{ rel: "icon", url: "/favicon.ico" }],
};

const getCookieString = () => Promise.resolve(cookies().toString());

export default function RootLayout({
children,
}: {
Expand All @@ -23,7 +25,7 @@ export default function RootLayout({
return (
<html lang="en">
<body className={inter.className}>
<TRPCReactProvider cookies={cookies().toString()}>
<TRPCReactProvider cookiePromise={getCookieString()}>
{children}
</TRPCReactProvider>
</body>
Expand Down
7 changes: 4 additions & 3 deletions cli/template/extras/src/trpc/react.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { loggerLink, unstable_httpBatchStreamLink } from "@trpc/client";
import { createTRPCReact } from "@trpc/react-query";
import { useState } from "react";
import { use, useState } from "react";

import { type AppRouter } from "~/server/api/root";
import { getUrl, transformer } from "./shared";
Expand All @@ -12,8 +12,9 @@ export const api = createTRPCReact<AppRouter>();

export function TRPCReactProvider(props: {
children: React.ReactNode;
cookies: string;
cookiePromise: Promise<string>;
}) {
const cookie = use(props.cookiePromise);
const [queryClient] = useState(() => new QueryClient());

const [trpcClient] = useState(() =>
Expand All @@ -29,7 +30,7 @@ export function TRPCReactProvider(props: {
url: getUrl(),
headers() {
return {
cookie: props.cookies,
cookie,
"x-trpc-source": "react",
};
},
Expand Down
Loading