Skip to content

Commit

Permalink
feat(INTERNAL-1361): whats new in separate trpc call
Browse files Browse the repository at this point in the history
  • Loading branch information
tsumo committed Nov 25, 2024
1 parent e0d806b commit df8f338
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 18 deletions.
5 changes: 5 additions & 0 deletions src/components/WhatsNew/WhatsNew.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ const WhatsNew = () => {
},
{
staleTime: Infinity,
trpc: {
context: {
skipBatch: true,
},
},
},
);

Expand Down
42 changes: 25 additions & 17 deletions src/utils/trpcClient.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -15,6 +15,25 @@ function getBaseUrl() {

export const trpc = createTRPCNext<TrpcRouter>({
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,

Expand All @@ -32,23 +51,12 @@ export const trpc = createTRPCNext<TrpcRouter>({
},

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),
}),
],
};
Expand Down
3 changes: 2 additions & 1 deletion trpc/router/whatsnew.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit df8f338

Please sign in to comment.