2021-09-27 14:47:55 +00:00
|
|
|
/**
|
|
|
|
* This file contains tRPC's HTTP response handler
|
|
|
|
*/
|
|
|
|
import { createContext } from "@server/createContext";
|
|
|
|
import { appRouter } from "@server/routers/_app";
|
2021-10-14 19:22:01 +00:00
|
|
|
import * as trpcNext from "@trpc/server/adapters/next";
|
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";
|
|
|
|
|
|
|
|
if (allPublic && allOk && isQuery) {
|
|
|
|
// 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
|
|
|
});
|