2021-09-27 14:47:55 +00:00
|
|
|
/**
|
|
|
|
* This file contains tRPC's HTTP response handler
|
|
|
|
*/
|
2022-07-22 17:27:06 +00:00
|
|
|
import * as trpcNext from "@calcom/trpc/server/adapters/next";
|
|
|
|
import { createContext } from "@calcom/trpc/server/createContext";
|
|
|
|
import { appRouter } from "@calcom/trpc/server/routers/_app";
|
2021-09-27 14:47:55 +00:00
|
|
|
|
|
|
|
export default trpcNext.createNextApiHandler({
|
|
|
|
router: appRouter,
|
|
|
|
/**
|
|
|
|
* @link https://trpc.io/docs/context
|
|
|
|
*/
|
|
|
|
createContext,
|
|
|
|
/**
|
|
|
|
* @link https://trpc.io/docs/error-handling
|
|
|
|
*/
|
|
|
|
onError({ error }) {
|
|
|
|
if (error.code === "INTERNAL_SERVER_ERROR") {
|
|
|
|
// send to bug reporting
|
|
|
|
console.error("Something went wrong", error);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Enable query batching
|
|
|
|
*/
|
|
|
|
batching: {
|
|
|
|
enabled: true,
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* @link https://trpc.io/docs/caching#api-response-caching
|
|
|
|
*/
|
2022-06-19 15:02:00 +00:00
|
|
|
responseMeta({ ctx, paths, type, errors }) {
|
|
|
|
// assuming we have all our public routes in `viewer.public`
|
|
|
|
const allPublic = paths && paths.every((path) => path.startsWith("viewer.public."));
|
|
|
|
// checking that no procedures errored
|
|
|
|
const allOk = errors.length === 0;
|
|
|
|
// checking we're doing a query request
|
|
|
|
const isQuery = type === "query";
|
|
|
|
|
2022-06-29 16:50:32 +00:00
|
|
|
// 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"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-06-28 17:37:01 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
|
|
// @ts-ignore ctx.req is available for SSR but not SSG
|
2022-06-29 16:50:32 +00:00
|
|
|
if (!!ctx?.req && allPublic && allOk && isQuery && !isThereANonCacheableQuery) {
|
2022-06-19 15:02:00 +00:00
|
|
|
// cache request for 1 day + revalidate once every 5 seconds
|
|
|
|
const ONE_DAY_IN_SECONDS = 60 * 60 * 24;
|
|
|
|
return {
|
|
|
|
headers: {
|
|
|
|
"cache-control": `s-maxage=5, stale-while-revalidate=${ONE_DAY_IN_SECONDS}`,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
},
|
2021-09-27 14:47:55 +00:00
|
|
|
});
|