Make i18n a non-batch request and non-cacheable (#3188)

hotfix/recurring-event-config-missing^2
Hariom Balhara 2022-06-29 16:50:32 +00:00 committed by GitHub
parent 3f3ffa2ba8
commit 09c8f55682
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 2 deletions

View File

@ -81,7 +81,11 @@ export default withTRPC<AppRouter>({
}),
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

View File

@ -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 {