From 09c8f55682ace28fbe925289c1ad6b85e9921e5e Mon Sep 17 00:00:00 2001 From: Hariom Balhara Date: Wed, 29 Jun 2022 16:50:32 +0000 Subject: [PATCH] Make i18n a non-batch request and non-cacheable (#3188) --- apps/web/pages/_app.tsx | 6 +++++- apps/web/pages/api/trpc/[trpc].ts | 12 +++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/apps/web/pages/_app.tsx b/apps/web/pages/_app.tsx index 430fa399a7..0c1355c12c 100644 --- a/apps/web/pages/_app.tsx +++ b/apps/web/pages/_app.tsx @@ -81,7 +81,11 @@ export default withTRPC({ }), splitLink({ // check for context property `skipBatch` - condition: (op) => op.context.skipBatch === true, + condition: (op) => { + // i18n should never be clubbed with other queries, so that it's caching can be managed independently + // We intend to not cache i18n query + return op.context.skipBatch === true || op.path === "viewer.public.i18n"; + }, // when condition is true, use normal request true: httpLink({ url }), // when condition is false, use batching diff --git a/apps/web/pages/api/trpc/[trpc].ts b/apps/web/pages/api/trpc/[trpc].ts index 11e64840bd..da0ab1feda 100644 --- a/apps/web/pages/api/trpc/[trpc].ts +++ b/apps/web/pages/api/trpc/[trpc].ts @@ -37,9 +37,19 @@ export default trpcNext.createNextApiHandler({ // checking we're doing a query request const isQuery = type === "query"; + // i18n response depends on request header + const nonCacheableQueries = ["viewer.public.i18n"]; + const isThereANonCacheableQuery = paths?.some((path) => nonCacheableQueries.includes(path)); + const isThereACacheableQuery = paths?.some((path) => !nonCacheableQueries.includes(path)); + if (isThereANonCacheableQuery && isThereACacheableQuery) { + console.warn( + "Cacheable and Non-cacheable queries are mixed in the same request. Not going to cache the request" + ); + } + // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore ctx.req is available for SSR but not SSG - if (!!ctx?.req && allPublic && allOk && isQuery) { + if (!!ctx?.req && allPublic && allOk && isQuery && !isThereANonCacheableQuery) { // cache request for 1 day + revalidate once every 5 seconds const ONE_DAY_IN_SECONDS = 60 * 60 * 24; return {