Skip to content

Commit

Permalink
fix: disable preview url indexing
Browse files Browse the repository at this point in the history
  • Loading branch information
abvthecity committed Dec 6, 2024
1 parent 836e563 commit 987784a
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 10 deletions.
7 changes: 2 additions & 5 deletions packages/ui/app/src/search/SearchV2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {
useFacetFilters,
useIsMobile,
} from "@fern-ui/fern-docs-search-ui";
import { HEADER_X_ALGOLIA_API_KEY } from "@fern-ui/fern-docs-utils";
import { useEventCallback } from "@fern-ui/react-commons";
import { useAtomValue } from "jotai";
import { useRouter } from "next/router";
Expand Down Expand Up @@ -73,12 +72,10 @@ export function SearchV2(): ReactElement | false {
return {};
}
const searchParams = new URLSearchParams();
searchParams.append("apiKey", data.apiKey);
filters.forEach((filter) => searchParams.append("filters", filter));
const search = String(searchParams);
const res = await fetch(`${facetApiEndpoint}${search ? `?${search}` : ""}`, {
method: "GET",
headers: { [HEADER_X_ALGOLIA_API_KEY]: data.apiKey },
});
const res = await fetch(`${facetApiEndpoint}?${search}`, { method: "GET" });
return res.json();
},
[data, facetApiEndpoint],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { algoliaAppId } from "@/server/env-variables";
import { selectFirst } from "@/server/utils/selectFirst";
import { toArray } from "@/server/utils/toArray";
import { fetchFacetValues } from "@fern-ui/fern-docs-search-server/algolia";
import { HEADER_X_ALGOLIA_API_KEY } from "@fern-ui/fern-docs-utils";
import { algoliasearch } from "algoliasearch";
import { NextApiRequest, NextApiResponse } from "next/types";

Expand All @@ -15,10 +14,10 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
}

const filters = toArray(req.query.filters);
const apiKey = selectFirst(req.headers[HEADER_X_ALGOLIA_API_KEY]);
const apiKey = selectFirst(req.query.apiKey);

if (!apiKey) {
return res.status(400).send(`${HEADER_X_ALGOLIA_API_KEY} is required`);
return res.status(400).send("apiKey is required");
}

return res.json(await fetchFacetValues({ filters, client: algoliasearch(algoliaAppId(), apiKey) }));
Expand Down
11 changes: 11 additions & 0 deletions packages/ui/docs-bundle/src/pages/api/fern-docs/search/v2/key.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getOrgMetadataForDomain } from "@/server/auth/metadata-for-url";
import { algoliaAppId, algoliaSearchApikey } from "@/server/env-variables";
import { selectFirst } from "@/server/utils/selectFirst";
import { getDocsDomainNode } from "@/server/xfernhost/node";
Expand All @@ -18,6 +19,16 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
}

const domain = getDocsDomainNode(req);

const orgMetadata = await getOrgMetadataForDomain(withoutStaging(domain));
if (orgMetadata == null) {
return res.status(404).send("Not found");
}

if (orgMetadata.isPreviewUrl) {
return res.status(400).send("Search is not supported for preview URLs");
}

const userToken = getXUserToken(req);

const apiKey = getSearchApiKey({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { track } from "@/server/analytics/posthog";
import { getOrgMetadataForDomain } from "@/server/auth/metadata-for-url";
import { algoliaAppId, algoliaWriteApiKey, fdrEnvironment, fernToken } from "@/server/env-variables";
import { Gate, withBasicTokenAnonymous } from "@/server/withRbac";
import { getDocsDomainNode } from "@/server/xfernhost/node";
Expand All @@ -14,6 +15,20 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
const domain = getDocsDomainNode(req);

try {
const orgMetadata = await getOrgMetadataForDomain(withoutStaging(domain));
if (orgMetadata == null) {
return res.status(404).send("Not found");
}

// If the domain is a preview URL, we don't want to reindex
if (orgMetadata.isPreviewUrl) {
return res.status(200).json({
added: 0,
updated: 0,
deleted: 0,
});
}

const start = Date.now();
const [authEdgeConfig, featureFlags] = await Promise.all([getAuthEdgeConfig(domain), getFeatureFlags(domain)]);

Expand Down Expand Up @@ -51,7 +66,11 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
deleted: response.deletedObjectIDs.length,
});

return res.status(200).json(response);
return res.status(200).json({
added: response.addedObjectIDs.length,
updated: response.updatedObjectIDs.length,
deleted: response.deletedObjectIDs.length,
});
} catch (error) {
// eslint-disable-next-line no-console
console.error(error);
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/fern-docs-utils/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const COOKIE_REFRESH_TOKEN = "refresh_token" as const; // for api key inj
export const COOKIE_EMAIL = "email" as const; // for api key injection
export const HEADER_X_FERN_HOST = "x-fern-host" as const;
export const HEADER_X_MATCHED_PATH = "x-matched-path" as const;
export const HEADER_X_ALGOLIA_API_KEY = "x-algolia-api-key" as const;

/**
* The role that is used to represent everyone (including unauthenticated users)
*/
Expand Down

0 comments on commit 987784a

Please sign in to comment.