From df8f338648ac191363114c01a69fb8b5eb91e615 Mon Sep 17 00:00:00 2001 From: tsumo Date: Mon, 25 Nov 2024 16:13:58 +0300 Subject: [PATCH] feat(INTERNAL-1361): whats new in separate trpc call --- src/components/WhatsNew/WhatsNew.tsx | 5 ++++ src/utils/trpcClient.ts | 42 +++++++++++++++++----------- trpc/router/whatsnew.ts | 3 +- 3 files changed, 32 insertions(+), 18 deletions(-) diff --git a/src/components/WhatsNew/WhatsNew.tsx b/src/components/WhatsNew/WhatsNew.tsx index 05a6a1700..595130982 100644 --- a/src/components/WhatsNew/WhatsNew.tsx +++ b/src/components/WhatsNew/WhatsNew.tsx @@ -25,6 +25,11 @@ const WhatsNew = () => { }, { staleTime: Infinity, + trpc: { + context: { + skipBatch: true, + }, + }, }, ); diff --git a/src/utils/trpcClient.ts b/src/utils/trpcClient.ts index 869548da4..2a260b841 100644 --- a/src/utils/trpcClient.ts +++ b/src/utils/trpcClient.ts @@ -1,4 +1,4 @@ -import { httpBatchLink } from '@trpc/client'; +import { httpBatchLink, httpLink, splitLink } from '@trpc/client'; import { createTRPCNext } from '@trpc/next'; import type { TrpcRouter } from '../../trpc/router'; @@ -15,6 +15,25 @@ function getBaseUrl() { export const trpc = createTRPCNext({ config: ({ ctx }) => { + const linkOptions = { + url: `${getBaseUrl()}/api/trpc`, + headers: async () => { + if (ctx?.req) { + // https://trpc.io/docs/nextjs/ssr#q-why-do-i-need-to-delete-the-connection-header-when-using-ssr-on-node-18 + // eslint-disable-next-line @typescript-eslint/no-unused-vars + const { connection, ...headers } = ctx.req.headers; + + return { + ...headers, + // Optional: inform server that it's an SSR request + 'x-ssr': '1', + }; + } + + return {}; + }, + }; + return { transformer, @@ -32,23 +51,12 @@ export const trpc = createTRPCNext({ }, links: [ - httpBatchLink({ - url: `${getBaseUrl()}/api/trpc`, - headers: async () => { - if (ctx?.req) { - // https://trpc.io/docs/nextjs/ssr#q-why-do-i-need-to-delete-the-connection-header-when-using-ssr-on-node-18 - // eslint-disable-next-line @typescript-eslint/no-unused-vars - const { connection, ...headers } = ctx.req.headers; - - return { - ...headers, - // Optional: inform server that it's an SSR request - 'x-ssr': '1', - }; - } - - return {}; + splitLink({ + condition: (op) => { + return op.context.skipBatch === true; }, + true: httpLink(linkOptions), + false: httpBatchLink(linkOptions), }), ], }; diff --git a/trpc/router/whatsnew.ts b/trpc/router/whatsnew.ts index bd67d75aa..6a2b1fdeb 100644 --- a/trpc/router/whatsnew.ts +++ b/trpc/router/whatsnew.ts @@ -23,7 +23,8 @@ export const whatsnew = router({ const whatsnewDir = path.join(process.cwd(), 'src/pages/whatsnew'); try { - const versionsWithNotes = fs.readdirSync(whatsnewDir).filter((f) => /^\d+\.\d+\.\d+$/.test(f)); + const directories = await fs.promises.readdir(whatsnewDir); + const versionsWithNotes = directories.filter((f) => /^\d+\.\d+\.\d+$/.test(f)); version = versionsWithNotes.at(-1); } catch (e) { console.log(e);