From a99031bdd2202b80a185b496850b2678a916aa97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Efra=C3=ADn=20Roch=C3=ADn?= Date: Tue, 18 Apr 2023 23:46:17 -0700 Subject: [PATCH] fix: [CAL-1490] Google Cal/Outlook not actively syncing/self hosted (#8305) --- apps/web/pages/_document.tsx | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/apps/web/pages/_document.tsx b/apps/web/pages/_document.tsx index aae5340cb4..d3c2b696d2 100644 --- a/apps/web/pages/_document.tsx +++ b/apps/web/pages/_document.tsx @@ -1,3 +1,4 @@ +import type { NextPageContext } from "next"; import type { DocumentContext, DocumentProps } from "next/document"; import Document, { Head, Html, Main, NextScript } from "next/document"; import { z } from "zod"; @@ -7,15 +8,22 @@ import { getDirFromLang } from "@calcom/lib/i18n"; import { csp } from "@lib/csp"; type Props = Record & DocumentProps; - +function setHeader(ctx: NextPageContext, name: string, value: string) { + try { + ctx.res?.setHeader(name, value); + } catch (e) { + // Getting "Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client" when revalidate calendar chache + console.log(`Error setting header ${name}=${value} for ${ctx.asPath || "unknown asPath"}`, e); + } +} class MyDocument extends Document { static async getInitialProps(ctx: DocumentContext) { const { nonce } = csp(ctx.req || null, ctx.res || null); if (!process.env.CSP_POLICY) { - ctx.res?.setHeader("x-csp", "not-opted-in"); + setHeader(ctx, "x-csp", "not-opted-in"); } else if (!ctx.res?.getHeader("x-csp")) { // If x-csp not set by gSSP, then it's initialPropsOnly - ctx.res?.setHeader("x-csp", "initialPropsOnly"); + setHeader(ctx, "x-csp", "initialPropsOnly"); } const asPath = ctx.asPath || ""; // Use a dummy URL as default so that URL parsing works for relative URLs as well. We care about searchParams and pathname only