using batching to mitigate startup request cost

This commit is contained in:
2024-11-08 16:53:56 -07:00
parent c47d7405c2
commit cc8d004aa4
5 changed files with 14 additions and 33 deletions

View File

@@ -2,7 +2,7 @@ import { createTrpcContext } from "@/services/trpc/context";
import { trpcAppRouter } from "@/services/trpc/router/app"; import { trpcAppRouter } from "@/services/trpc/router/app";
import { fetchRequestHandler } from "@trpc/server/adapters/fetch"; import { fetchRequestHandler } from "@trpc/server/adapters/fetch";
const handler = (request: Request) => { const handler = async (request: Request) => {
return fetchRequestHandler({ return fetchRequestHandler({
endpoint: "/api/trpc", endpoint: "/api/trpc",
req: request, req: request,

View File

@@ -52,6 +52,13 @@ async function DataHydration({
router: trpcAppRouter, router: trpcAppRouter,
ctx: createTrpcContext(), ctx: createTrpcContext(),
transformer: superjson, transformer: superjson,
queryClientConfig: {
defaultOptions: {
queries: {
staleTime: Infinity,
},
},
},
}); });
const allSettings = await fileStorageService.settings.getAllCoursesSettings(); const allSettings = await fileStorageService.settings.getAllCoursesSettings();
await Promise.all( await Promise.all(
@@ -63,7 +70,7 @@ async function DataHydration({
await Promise.all( await Promise.all(
moduleNames.map( moduleNames.map(
async (moduleName) => async (moduleName) =>
await trpcHelper.assignment.getAllAssignments.prefetch({ await trpcHelper.assignment.getAllAssignments.fetch({
courseName, courseName,
moduleName, moduleName,
}) })
@@ -75,7 +82,7 @@ async function DataHydration({
await Promise.all( await Promise.all(
allSettings.map( allSettings.map(
async (settings) => async (settings) =>
await trpcHelper.lectures.getLectures.prefetch({ await trpcHelper.lectures.getLectures.fetch({
courseName: settings.name, courseName: settings.name,
}) })
) )

View File

@@ -8,7 +8,7 @@ export function makeQueryClient() {
// above 0 to avoid refetching immediately on the client // above 0 to avoid refetching immediately on the client
// staleTime: 60_000, // staleTime: 60_000,
staleTime: 1000 * 60 * 60, // 1 hour staleTime: 1000 * 60 * 60, // 1 hour
// refetchInterval: 5000, // debug only // refetchInterval: 7000, // debug only
refetchOnWindowFocus: false, refetchOnWindowFocus: false,
retry: 0, retry: 0,
refetchOnMount: false, refetchOnMount: false,

View File

@@ -4,24 +4,19 @@ import superjson from "superjson";
import { httpBatchLink, httpLink } from "@trpc/client"; import { httpBatchLink, httpLink } from "@trpc/client";
import { trpc } from "./utils"; import { trpc } from "./utils";
import { getQueryClient } from "@/app/providersQueryClientUtils"; import { getQueryClient } from "@/app/providersQueryClientUtils";
import { isServer } from "@tanstack/react-query";
export default function TrpcProvider({ export default function TrpcProvider({
children, children,
}: { }: {
children: React.ReactNode; children: React.ReactNode;
}) { }) {
// NOTE: Your production URL environment variable may be different const url = isServer ? "http://localhost:3000/api/trpc/" : "/api/trpc"
const url = "http://localhost:3000/api/trpc/"
//"/api/trpc";
// process.env.NEXT_PUBLIC_APP_DOMAIN &&
// !process.env.NEXT_PUBLIC_APP_DOMAIN.includes("localhost")
// ? `https://www.${process.env.NEXT_PUBLIC_APP_DOMAIN}/api/trpc/`
// : "http://localhost:3000/api/trpc/";
const [trpcClient] = useState(() => const [trpcClient] = useState(() =>
trpc.createClient({ trpc.createClient({
links: [ links: [
httpLink({ httpBatchLink({
url, url,
transformer: superjson, transformer: superjson,
}), }),

View File

@@ -122,24 +122,3 @@ export function createSuccessToast(message: string) {
} }
); );
} }
// export function createQueryClient() {
// return new QueryClient({
// defaultOptions: {
// queries: {
// refetchOnWindowFocus: false,
// retry: 0,
// },
// mutations: {
// onError: addErrorAsToast,
// retry: 0,
// },
// },
// queryCache: new QueryCache({
// onError: addErrorAsToast,
// }),
// mutationCache: new MutationCache({
// onError: addErrorAsToast,
// }),
// });
// }