Skip to content

Commit 95e3fb7

Browse files
chore: Cache timezones by calcom version (#15556)
* chore: Cache timezones by calcom version * Fix types
1 parent 3fe26f1 commit 95e3fb7

File tree

5 files changed

+24
-12
lines changed

5 files changed

+24
-12
lines changed

packages/trpc/server/createNextApiHandler.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -57,22 +57,22 @@ export function createNextApiHandler(router: AnyRouter, isPublic = false, namesp
5757
defaultHeaders.headers["cache-control"] = `no-cache`;
5858

5959
if (isPublic && paths) {
60-
const ONE_DAY_IN_SECONDS = 60 * 60 * 24;
6160
const FIVE_MINUTES_IN_SECONDS = 5 * 60;
6261
const ONE_YEAR_IN_SECONDS = 31536000;
62+
const SETTING_FOR_CACHED_BY_VERSION =
63+
process.env.NODE_ENV === "development" ? "no-cache" : `max-age=${ONE_YEAR_IN_SECONDS}`;
6364

6465
const cacheRules = {
6566
session: "no-cache",
6667

67-
i18n: process.env.NODE_ENV === "development" ? "no-cache" : `max-age=${ONE_YEAR_IN_SECONDS}`,
68+
// i18n and cityTimezones are now being accessed using the CalComVersion, which updates on every release,
69+
// letting the clients get the new versions when the version number changes.
70+
i18n: SETTING_FOR_CACHED_BY_VERSION,
71+
cityTimezones: SETTING_FOR_CACHED_BY_VERSION,
6872

6973
// FIXME: Using `max-age=1, stale-while-revalidate=60` fails some booking tests.
7074
"slots.getSchedule": `no-cache`,
7175

72-
// Timezones are hardly updated. No need to burden the servers with requests for this by keeping low max-age.
73-
// Keep it cached for a day and then give it 60 seconds more at most to be updated.
74-
cityTimezones: `max-age=${ONE_DAY_IN_SECONDS}, stale-while-revalidate=60`,
75-
7676
// Feature Flags change but it might be okay to have a 5 minute cache to avoid burdening the servers with requests for this.
7777
// Note that feature flags can be used to quickly kill a feature if it's not working as expected. So, we have to keep fresh time lesser than the deployment time atleast
7878
"features.map": `max-age=${FIVE_MINUTES_IN_SECONDS}, stale-while-revalidate=60`, // "map" - Feature Flag Map

packages/trpc/server/routers/publicViewer/cityTimezones.schema.ts

-1
This file was deleted.

packages/trpc/server/routers/publicViewer/timezones/_router.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import publicProcedure from "../../../procedures/publicProcedure";
22
import { importHandler, router } from "../../../trpc";
3+
import { cityTimezonesSchema } from "./cityTimezones.schema";
34

45
const NAMESPACE = "publicViewer";
56

67
const namespaced = (s: string) => `${NAMESPACE}.${s}`;
78

89
// things that unauthenticated users can query about themselves
910
export const timezonesRouter = router({
10-
cityTimezones: publicProcedure.query(async () => {
11+
cityTimezones: publicProcedure.input(cityTimezonesSchema).query(async () => {
1112
const handler = await importHandler(
1213
namespaced("cityTimezones"),
1314
() => import("@calcom/lib/cityTimezonesHandler")
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
1-
export {};
1+
import { z } from "zod";
2+
3+
export const cityTimezonesSchema = z.object({
4+
CalComVersion: z.string(),
5+
});
6+
7+
export type CityTimezonesSchema = z.infer<typeof cityTimezonesSchema>;

packages/ui/components/form/timezone-select/TimezoneSelect.tsx

+9-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { ITimezoneOption, ITimezone, Props as SelectProps } from "react-tim
33
import BaseSelect from "react-timezone-select";
44

55
import { classNames } from "@calcom/lib";
6+
import { CALCOM_VERSION } from "@calcom/lib/constants";
67
import { filterByCities, addCitiesToDropdown, handleOptionLabel } from "@calcom/lib/timezone";
78
import { trpc } from "@calcom/trpc/react";
89

@@ -18,9 +19,14 @@ export type TimezoneSelectProps = SelectProps & {
1819
timezoneSelectCustomClassname?: string;
1920
};
2021
export function TimezoneSelect(props: TimezoneSelectProps) {
21-
const { data, isPending } = trpc.viewer.timezones.cityTimezones.useQuery(undefined, {
22-
trpc: { context: { skipBatch: true } },
23-
});
22+
const { data, isPending } = trpc.viewer.timezones.cityTimezones.useQuery(
23+
{
24+
CalComVersion: CALCOM_VERSION,
25+
},
26+
{
27+
trpc: { context: { skipBatch: true } },
28+
}
29+
);
2430

2531
return <TimezoneSelectComponent data={data} isPending={isPending} {...props} />;
2632
}

0 commit comments

Comments
 (0)