Skip to content

Commit 73ac563

Browse files
committed
Don't inspect request headers in cron jobs
1 parent eed2d7f commit 73ac563

File tree

4 files changed

+21
-4
lines changed

4 files changed

+21
-4
lines changed

.env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ ADMINS=
106106
MONTHLY_CRON_ENABLED=
107107

108108
# Functional tests
109-
E2E_TEST_ENV=local
109+
E2E_TEST_ENV="not running e2e tests"
110110
E2E_TEST_SECRET=test-secret
111111
E2E_TEST_ACCOUNT_BASE_EMAIL=test-account
112112
E2E_TEST_ACCOUNT_BASE_PASSWORD=test-password

src/app/functions/server/getExperiments.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ describe("getExperiments", () => {
9999
});
100100

101101
it("calls Cirrus V2 when feature flag is enabled with preview param", async () => {
102+
process.env.NEXT_RUNTIME = "test";
102103
process.env.NIMBUS_SIDECAR_URL = "https://cirrus.example";
103104
getEnabledFeatureFlagsMock.mockReturnValue([
104105
"CirrusV2",
@@ -188,7 +189,8 @@ describe("getExperiments", () => {
188189
);
189190
});
190191

191-
it("fallsback to defaultExperimentData when not experiment data is returned by Cirrus", async () => {
192+
it("fallsback to defaultExperimentData when no experiment data is returned by Cirrus", async () => {
193+
process.env.NEXT_RUNTIME = "test";
192194
process.env.NIMBUS_SIDECAR_URL = "https://cirrus.example";
193195
getEnabledFeatureFlagsMock.mockReturnValue([
194196
"CirrusV2",

src/app/functions/server/getExperiments.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,13 @@ export async function getExperiments(params: {
5959
serverUrl.pathname += "v1/features";
6060
}
6161

62-
const nextHeaders = await loadNextHeaders();
62+
const nextHeaders =
63+
// We also check for experiments in cron jobs, where there are
64+
// no HTTP requests. Skip the headers there; we don't need to
65+
// force-enable experiments in cronjobs.
66+
typeof process.env.NEXT_RUNTIME === "string"
67+
? await loadNextHeaders()
68+
: null;
6369
let previewMode = false;
6470
if (nextHeaders) {
6571
const headersList = await nextHeaders.headers();

src/db/tables/featureFlags.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,16 @@ export async function getEnabledFeatureFlags(
9292
options: { isSignedOut?: false; email: string } | { isSignedOut: true },
9393
): Promise<FeatureFlagName[]> {
9494
// Force feature flags for E2E tests via URL query params
95-
if (process.env.E2E_TEST_ENV === "local") {
95+
if (
96+
// Check that we're running under Next.js; in cronjobs
97+
// there's no request to inspect. We're not force-enabling
98+
// feature flags in that environment anyway.
99+
typeof process.env.NEXT_RUNTIME === "string" &&
100+
// When running end-to-end tests locally or in a PR,
101+
// we can force-enable feature flags that haven't been enabled
102+
// yet on stage or on prod:
103+
process.env.E2E_TEST_ENV === "local"
104+
) {
96105
const { headers } = await import("next/headers");
97106
const forcedFeatureFlags = (await headers()).get("x-forced-feature-flags");
98107

0 commit comments

Comments
 (0)