diff --git a/packages/ui/docs-bundle/src/pages/api/fern-docs/revalidate-all/v3.ts b/packages/ui/docs-bundle/src/pages/api/fern-docs/revalidate-all/v3.ts index aebdc590fd..63c73b2931 100644 --- a/packages/ui/docs-bundle/src/pages/api/fern-docs/revalidate-all/v3.ts +++ b/packages/ui/docs-bundle/src/pages/api/fern-docs/revalidate-all/v3.ts @@ -1,10 +1,12 @@ import { DocsKVCache } from "@/server/DocsCache"; import { DocsLoader } from "@/server/DocsLoader"; +import { getOrgMetadataForDomain } from "@/server/auth/metadata-for-url"; import { queueAlgoliaReindex } from "@/server/queueAlgoliaReindex"; import { Revalidator } from "@/server/revalidator"; import { getDocsDomainNode, getHostNode } from "@/server/xfernhost/node"; import { NodeCollector } from "@fern-api/fdr-sdk/navigation"; import type { FernDocs } from "@fern-fern/fern-docs-sdk"; +import { withoutStaging } from "@fern-ui/fern-docs-utils"; import { NextApiHandler, NextApiRequest, NextApiResponse } from "next"; export const config = { @@ -53,7 +55,10 @@ const handler: NextApiHandler = async ( // queue algolia reindexing try { - await queueAlgoliaReindex(domain, root.slug); + const orgMetadata = await getOrgMetadataForDomain(withoutStaging(domain)); + if (orgMetadata?.isPreviewUrl === false) { + await queueAlgoliaReindex(host, withoutStaging(domain), root.slug); + } } catch (err) { // eslint-disable-next-line no-console console.error(err); diff --git a/packages/ui/docs-bundle/src/pages/api/fern-docs/revalidate-all/v4.ts b/packages/ui/docs-bundle/src/pages/api/fern-docs/revalidate-all/v4.ts index e83ef7a95e..95a8efc58d 100644 --- a/packages/ui/docs-bundle/src/pages/api/fern-docs/revalidate-all/v4.ts +++ b/packages/ui/docs-bundle/src/pages/api/fern-docs/revalidate-all/v4.ts @@ -1,10 +1,12 @@ import { DocsKVCache } from "@/server/DocsCache"; import { DocsLoader } from "@/server/DocsLoader"; +import { getOrgMetadataForDomain } from "@/server/auth/metadata-for-url"; import { queueAlgoliaReindex } from "@/server/queueAlgoliaReindex"; import { Revalidator } from "@/server/revalidator"; import { getDocsDomainNode, getHostNode } from "@/server/xfernhost/node"; import { NodeCollector } from "@fern-api/fdr-sdk/navigation"; import type { FernDocs } from "@fern-fern/fern-docs-sdk"; +import { withoutStaging } from "@fern-ui/fern-docs-utils"; import { NextApiHandler, NextApiRequest, NextApiResponse } from "next"; export const config = { @@ -60,7 +62,10 @@ const handler: NextApiHandler = async ( if (offset === 0) { // queue algolia reindexing try { - await queueAlgoliaReindex(domain, root.slug); + const orgMetadata = await getOrgMetadataForDomain(withoutStaging(domain)); + if (orgMetadata?.isPreviewUrl === false) { + await queueAlgoliaReindex(host, withoutStaging(domain), root.slug); + } } catch (err) { // eslint-disable-next-line no-console console.error(err); diff --git a/packages/ui/docs-bundle/src/server/queueAlgoliaReindex.ts b/packages/ui/docs-bundle/src/server/queueAlgoliaReindex.ts index ab58c546d7..3a349f569c 100644 --- a/packages/ui/docs-bundle/src/server/queueAlgoliaReindex.ts +++ b/packages/ui/docs-bundle/src/server/queueAlgoliaReindex.ts @@ -1,15 +1,23 @@ import { Client } from "@upstash/qstash"; import { qstashToken } from "@/server/env-variables"; -import { addLeadingSlash } from "@fern-ui/fern-docs-utils"; +import { HEADER_X_FERN_HOST, addLeadingSlash } from "@fern-ui/fern-docs-utils"; import { removeTrailingSlash } from "next/dist/shared/lib/router/utils/remove-trailing-slash"; import { conformTrailingSlash } from "./trailingSlash"; -export const queueAlgoliaReindex = async (domain: string, basepath_: string = ""): Promise => { +export const queueAlgoliaReindex = async ( + host: string, + domain: string, + basepath_: string = "", +): Promise => { if (process.env.VERCEL && process.env.VERCEL_ENV !== "development") { const client = new Client({ token: qstashToken() }); const headers = new Headers(); + + // add x-fern-host header to identify the docs domain (for compatibility with vercel preview urls) + headers.set(HEADER_X_FERN_HOST, domain); + if (process.env.VERCEL_AUTOMATION_BYPASS_SECRET) { headers.set("x-vercel-protection-bypass", process.env.VERCEL_AUTOMATION_BYPASS_SECRET); } @@ -18,9 +26,10 @@ export const queueAlgoliaReindex = async (domain: string, basepath_: string = "" const endpoint = addLeadingSlash(conformTrailingSlash("/api/fern-docs/search/v2/reindex")); const res = await client.publishJSON({ - url: `https://${domain}${basepath}${endpoint}`, + url: `https://${host}${basepath}${endpoint}`, method: "POST", headers, + retries: 1, }); return res.messageId;