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

chore: don't pass headers to cc #843

Merged
merged 1 commit into from
Jan 16, 2024
Merged
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
12 changes: 10 additions & 2 deletions apps/nextjs/src/app/_components/posts.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"use client";

import { use } from "react";

import type { RouterOutputs } from "@acme/api";
import { cn } from "@acme/ui";
import { Button } from "@acme/ui/button";
Expand Down Expand Up @@ -79,8 +81,14 @@ export function CreatePostForm() {
);
}

export function PostList() {
const [posts] = api.post.all.useSuspenseQuery();
export function PostList(props: {
posts: Promise<RouterOutputs["post"]["all"]>;
}) {
// TODO: Make `useSuspenseQuery` work without having to pass a promise from RSC
const initialData = use(props.posts);
const { data: posts } = api.post.all.useQuery(undefined, {
initialData,
});

if (posts.length === 0) {
return (
Expand Down
8 changes: 1 addition & 7 deletions apps/nextjs/src/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import type { Metadata, Viewport } from "next";
import { cache } from "react";
import { headers } from "next/headers";
import { GeistMono } from "geist/font/mono";
import { GeistSans } from "geist/font/sans";

Expand Down Expand Up @@ -41,8 +39,6 @@ export const viewport: Viewport = {
],
};

const getHeaders = cache(async () => headers());

export default function RootLayout(props: { children: React.ReactNode }) {
return (
<html lang="en" suppressHydrationWarning>
Expand All @@ -54,9 +50,7 @@ export default function RootLayout(props: { children: React.ReactNode }) {
)}
>
<ThemeProvider attribute="class" defaultTheme="system" enableSystem>
<TRPCReactProvider headersPromise={getHeaders()}>
{props.children}
</TRPCReactProvider>
<TRPCReactProvider>{props.children}</TRPCReactProvider>
<div className="absolute bottom-4 right-4">
<ThemeToggle />
</div>
Expand Down
9 changes: 3 additions & 6 deletions apps/nextjs/src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,8 @@ import {
export const runtime = "edge";

export default async function HomePage() {
// You don't need to fetch these here, just showing different usages
// If you don't want the Suspense loading state, you could pass these
// posts as props as use as initialData in the query.
const posts = await api.post.all();
console.log("RSC Posts:", posts);
// You can await this here if you don't want to show Suspense fallback below
const posts = api.post.all();

return (
<main className="container h-screen py-16">
Expand All @@ -36,7 +33,7 @@ export default async function HomePage() {
</div>
}
>
<PostList />
<PostList posts={posts} />
</Suspense>
</div>
</div>
Expand Down
7 changes: 2 additions & 5 deletions apps/nextjs/src/trpc/react.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ import type { AppRouter } from "@acme/api";

export const api = createTRPCReact<AppRouter>();

export function TRPCReactProvider(props: {
children: React.ReactNode;
headersPromise: Promise<Headers>;
}) {
export function TRPCReactProvider(props: { children: React.ReactNode }) {
const [queryClient] = useState(() => new QueryClient());

const [trpcClient] = useState(() =>
Expand All @@ -28,7 +25,7 @@ export function TRPCReactProvider(props: {
unstable_httpBatchStreamLink({
url: getBaseUrl() + "/api/trpc",
async headers() {
const headers = new Headers(await props.headersPromise);
const headers = new Headers();
headers.set("x-trpc-source", "nextjs-react");
return headers;
},
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/router/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { CreatePostSchema } from "@acme/validators";
import { createTRPCRouter, protectedProcedure, publicProcedure } from "../trpc";

export const postRouter = createTRPCRouter({
all: publicProcedure.query(({ ctx }) => {
all: protectedProcedure.query(({ ctx }) => {
// return ctx.db.select().from(schema.post).orderBy(desc(schema.post.id));
return ctx.db.query.post.findMany({
orderBy: desc(schema.post.id),
Expand Down