Skip to content

Commit

Permalink
add test suspense code
Browse files Browse the repository at this point in the history
  • Loading branch information
mengxi-ream committed Feb 20, 2024
1 parent 90827a9 commit b747bba
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
23 changes: 23 additions & 0 deletions src/app/_components/test-suspense.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"use client";

import { api } from "@/trpc/react";
import { Suspense } from "react";

export default function TestSuspense() {
return (
<div>
<h1>Test Suspense</h1>
<Suspense fallback={<div>Loading...</div>}>
<SayHi />
</Suspense>
</div>
);
}

function SayHi() {
const [greet, getGreet] = api.post.hello.useSuspenseQuery({
text: "Suspense user",
});

return <div>{greet.greeting}</div>;
}
2 changes: 2 additions & 0 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Link from "next/link";
import { CreatePost } from "@/app/_components/create-post";
import { getServerAuthSession } from "@/server/auth";
import { api } from "@/trpc/server";
import TestSuspense from "./_components/test-suspense";

export default async function Home() {
noStore();
Expand Down Expand Up @@ -59,6 +60,7 @@ export default async function Home() {
</div>

<CrudShowcase />
<TestSuspense />
</div>
</main>
);
Expand Down
7 changes: 5 additions & 2 deletions src/server/api/routers/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ import {
} from "@/server/api/trpc";

export const postRouter = createTRPCRouter({
hello: publicProcedure
hello: protectedProcedure
.input(z.object({ text: z.string() }))
.query(({ input }) => {
.query(async ({ input }) => {
// wait for 1 second
await new Promise((resolve) => setTimeout(resolve, 1000));

return {
greeting: `Hello ${input.text}`,
};
Expand Down

0 comments on commit b747bba

Please sign in to comment.