Skip to content

Commit

Permalink
Revert "fix: revalidate all paths before and after docs registration" (
Browse files Browse the repository at this point in the history
  • Loading branch information
abvthecity authored Apr 9, 2024
1 parent 20d0862 commit fb1869b
Show file tree
Hide file tree
Showing 20 changed files with 121 additions and 307 deletions.
54 changes: 13 additions & 41 deletions fern/apis/revalidation/definition/__package__.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,66 +4,38 @@ service:
auth: false
base-path: /api/fern-docs
endpoints:
bulkRevalidate:
path: /revalidate-bulk
docs: Revalidates a list of URLs
display-name: Revalidate a list of URLs
method: POST
request:
name: BulkRevalidateRequest
body:
properties:
host:
type: string
docs: |
The host you want to revalidate the docs for.
slugs:
type: list<string>
docs: |
The paths to revalidate, sans initial slash. i.e. `path/to/page`
response: BulkRevalidateResponse
revalidateAllV2:
path: /revalidate-all/v2
docs: Revalidates a docs website
display-name: Revalidate a docs instance
method: POST
request:
name: CreateRevalidateAllV2Request
headers:
x-fern-host:
headers:
x-fern-host:
type: string
docs: |
The host you want to revalidate the docs for.
query-parameters:
basePath:
type: string
docs: |
The base path of the docs you want to revalidate.
body:
properties:
host:
type: string
docs: |
The host you want to revalidate the docs for.
response: BulkRevalidateResponse
listSlugs:
path: /list-slugs
display-name: Get all slugs for docs site
method: GET
request:
name: GetSitemapRequest
headers:
x-fern-host: optional<string>
response: list<string>
response: RevalidateAllV2Response

types:
BulkRevalidateResponse:
RevalidateAllV2Response:
properties:
successfulRevalidations: list<SuccessfulRevalidation>
failedRevalidations: list<FailedRevalidation>

SuccessfulRevalidation:
properties:
success: literal<true>
url: string
successfulRevalidations: list<SuccessfulRevalidations>
failedRevalidations: list<unknown>

FailedRevalidation:
SuccessfulRevalidations:
properties:
success: literal<false>
success: string
url: string
message: optional<string>
39 changes: 0 additions & 39 deletions packages/commons/fdr-utils/src/__test__/buildUrl.test.ts

This file was deleted.

24 changes: 6 additions & 18 deletions packages/commons/fdr-utils/src/buildUrl.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,8 @@
// returns: `mydocs.docs.buildwithfern.com/path/to/page`
export function buildUrl({ host, pathname }: { host: string; pathname?: string }): string {
// Remove protocol if it exists
host = host.replace(/(^\w+:|^)\/\//, "");

// Remove trailing slash at the end of the host
host = host.endsWith("/") ? host.slice(0, -1) : host;

// Remove '.staging' if it exists
host = host.replace(".docs.staging.", ".docs.");

if (pathname == null || pathname.length === 0) {
return host;
export function buildUrl({ host, pathname }: { host: string; pathname: string }): string {
let hostWithoutTrailingSlash = host.endsWith("/") ? host.slice(0, -1) : host;
hostWithoutTrailingSlash = hostWithoutTrailingSlash.replace(".docs.staging.", ".docs.");
if (pathname.length === 0) {
return hostWithoutTrailingSlash;
}

// Remove leading slash at the start of the pathname
pathname = pathname.startsWith("/") ? pathname.slice(1) : pathname;

return `${host}/${pathname}`;
return `${hostWithoutTrailingSlash}/${pathname}`;
}
11 changes: 1 addition & 10 deletions packages/commons/fdr-utils/src/getAllUrlsFromDocsConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,6 @@ export function getAllUrlsFromDocsConfig(
basePath: string | undefined,
nav: DocsV1Read.NavigationConfig,
apis: Record<string, APIV1Read.ApiDefinition>,
): string[] {
const slugs = getAllSlugsFromDocsConfig(basePath, nav, apis);
return slugs.map((slug) => buildUrl({ host, pathname: slug }));
}

export function getAllSlugsFromDocsConfig(
basePath: string | undefined,
nav: DocsV1Read.NavigationConfig,
apis: Record<string, APIV1Read.ApiDefinition>,
): string[] {
const basePathSlug = basePath != null ? basePath.split("/").filter((t) => t.length > 0) : [];
const root = resolveSidebarNodesRoot(nav, apis, basePathSlug);
Expand All @@ -26,5 +17,5 @@ export function getAllSlugsFromDocsConfig(
visitedSlugs.push(node.slug.join("/"));
});

return Array.from(new Set(visitedSlugs));
return Array.from(new Set(visitedSlugs.map((slug) => buildUrl({ host, pathname: slug }))));
}
1 change: 0 additions & 1 deletion packages/ui/docs-bundle/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
"@aws-sdk/s3-request-presigner": "^3.540.0",
"@fern-api/fdr-sdk": "workspace:*",
"@fern-api/venus-api-sdk": "^0.1.0",
"@fern-fern/revalidation-sdk": "0.0.6",
"@fern-ui/core-utils": "workspace:*",
"@fern-ui/fdr-utils": "workspace:*",
"@fern-ui/ui": "workspace:*",
Expand Down
4 changes: 3 additions & 1 deletion packages/ui/docs-bundle/src/pages/404.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ export default NotFoundPage;

export const getStaticProps: GetStaticProps<NotFoundPage.Props> = async ({ params = {} }) => {
const host = params.host as string | undefined;
const slugArray = params.slug as string[] | undefined;
const pathname = slugArray != null ? slugArray.join("/") : "";
const docs = await REGISTRY_SERVICE.docs.v2.read.getDocsForUrl({
url: process.env.NEXT_PUBLIC_DOCS_DOMAIN ?? buildUrl({ host: host ?? "" }),
url: process.env.NEXT_PUBLIC_DOCS_DOMAIN ?? buildUrl({ host: host ?? "", pathname }),
});

if (!docs.ok) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { buildUrl, getAllUrlsFromDocsConfig } from "@fern-ui/fdr-utils";
import { NextApiHandler, NextApiRequest, NextApiResponse } from "next";
import { loadWithUrl } from "../../../utils/loadWithUrl";
import { toValidPathname } from "../../../utils/toValidPathname";

function getHostFromUrl(url: string | undefined): string | undefined {
if (url == null) {
Expand Down Expand Up @@ -54,8 +55,14 @@ const handler: NextApiHandler = async (
if (typeof xFernHost !== "string") {
return res.status(404).json({ successfulRevalidations: [], failedRevalidations: [] });
}
const hostWithoutTrailingSlash = xFernHost.endsWith("/") ? xFernHost.slice(0, -1) : xFernHost;

const docs = await loadWithUrl(buildUrl({ host: xFernHost }));
const docs = await loadWithUrl(
buildUrl({
host: hostWithoutTrailingSlash,
pathname: toValidPathname(req.query.basePath),
}),
);

if (docs == null) {
// return notFoundResponse();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { FernRevalidation } from "@fern-fern/revalidation-sdk";
import { isPlainObject } from "@fern-ui/core-utils";
import { buildUrl, getAllUrlsFromDocsConfig } from "@fern-ui/fdr-utils";
import { NextApiHandler, NextApiRequest, NextApiResponse } from "next";
import { loadWithUrl } from "../../../../utils/loadWithUrl";
import { RevalidatePathResult, isFailureResult, isSuccessResult } from "../../../../utils/revalidate-types";
import { toValidPathname } from "../../../../utils/toValidPathname";

function getHostFromUrl(url: string | undefined): string | undefined {
if (url == null) {
Expand All @@ -17,9 +16,35 @@ export const config = {
maxDuration: 300,
};

type RevalidatePathResult = RevalidatePathSuccessResult | RevalidatePathErrorResult;

interface RevalidatePathSuccessResult {
success: true;
url: string;
}

function isSuccessResult(result: RevalidatePathResult): result is RevalidatePathSuccessResult {
return result.success;
}

interface RevalidatePathErrorResult {
success: false;
url: string;
message: string;
}

function isFailureResult(result: RevalidatePathResult): result is RevalidatePathErrorResult {
return !result.success;
}

type RevalidatedPaths = {
successfulRevalidations: RevalidatePathSuccessResult[];
failedRevalidations: RevalidatePathErrorResult[];
};

const handler: NextApiHandler = async (
req: NextApiRequest,
res: NextApiResponse<FernRevalidation.BulkRevalidateResponse>,
res: NextApiResponse<RevalidatedPaths>,
): Promise<unknown> => {
if (req.method !== "POST") {
return res.status(405).json({ successfulRevalidations: [], failedRevalidations: [] });
Expand All @@ -31,8 +56,14 @@ const handler: NextApiHandler = async (
if (typeof xFernHost !== "string") {
return res.status(404).json({ successfulRevalidations: [], failedRevalidations: [] });
}
const hostWithoutTrailingSlash = xFernHost.endsWith("/") ? xFernHost.slice(0, -1) : xFernHost;

const docs = await loadWithUrl(buildUrl({ host: xFernHost }));
const docs = await loadWithUrl(
buildUrl({
host: hostWithoutTrailingSlash,
pathname: toValidPathname(req.query.basePath),
}),
);

if (docs == null) {
// return notFoundResponse();
Expand Down
87 changes: 0 additions & 87 deletions packages/ui/docs-bundle/src/pages/api/fern-docs/revalidate-bulk.ts

This file was deleted.

Loading

0 comments on commit fb1869b

Please sign in to comment.