Skip to content

Commit

Permalink
make queuing more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
abvthecity committed Dec 6, 2024
1 parent 987784a commit 0669734
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down Expand Up @@ -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);
Expand Down
15 changes: 12 additions & 3 deletions packages/ui/docs-bundle/src/server/queueAlgoliaReindex.ts
Original file line number Diff line number Diff line change
@@ -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<string | undefined> => {
export const queueAlgoliaReindex = async (
host: string,
domain: string,
basepath_: string = "",
): Promise<string | undefined> => {
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);
}
Expand All @@ -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;
Expand Down

0 comments on commit 0669734

Please sign in to comment.