Skip to content

Commit

Permalink
fix: revalidation supports querying docs config by id (#150)
Browse files Browse the repository at this point in the history
* revalidation supports docs config id

* fix compile
  • Loading branch information
dsinghvi authored Sep 23, 2023
1 parent 794cb98 commit 9dde2fd
Show file tree
Hide file tree
Showing 15 changed files with 78 additions and 65 deletions.
12 changes: 6 additions & 6 deletions .pnp.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion packages/commons/app-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"depcheck": "depcheck"
},
"dependencies": {
"@fern-fern/registry-browser": "0.19.1-10-gf615da5",
"@fern-fern/registry-browser": "0.20.0-2-g764a1b3",
"@fern-ui/core-utils": "workspace:*",
"lodash-es": "^4.17.21",
"next-mdx-remote": "^4.4.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"@blueprintjs/icons": "^4.4.0",
"@blueprintjs/popover2": "^1.8.0",
"@blueprintjs/select": "^4.4.2",
"@fern-fern/registry-browser": "0.19.1-10-gf615da5",
"@fern-fern/registry-browser": "0.20.0-2-g764a1b3",
"@fern-ui/app-utils": "workspace:*",
"@fern-ui/core-utils": "workspace:*",
"@fern-ui/react-commons": "workspace:*",
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/fe-bundle/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"lint:fern-prod": "yarn compile && yarn env:fern-prod next lint"
},
"dependencies": {
"@fern-fern/registry-browser": "0.19.1-10-gf615da5",
"@fern-fern/registry-browser": "0.20.0-2-g764a1b3",
"@fern-ui/app-utils": "workspace:*",
"@fern-ui/core-utils": "workspace:*",
"@fern-ui/theme": "workspace:*",
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/fe-bundle/src/pages/[host]/sitemap.xml.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export const getServerSideProps: GetServerSideProps = async ({ params = {}, res

const paths = getPathsToRevalidate({
navigationConfig: docs.body.definition.config.navigation,
docsDefinition: docs.body.definition,
apis: docs.body.definition.apis,
});

const urls = paths.map((path) => {
Expand Down
51 changes: 36 additions & 15 deletions packages/ui/fe-bundle/src/pages/api/revalidate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface Request {
const handler: NextApiHandler = async (req, res) => {
try {
const url = req.body?.url;
const docsConfigId = req.body?.docsConfigId;

if (url == null) {
return res.status(400).send("Property 'url' is missing from request.");
Expand All @@ -17,30 +18,50 @@ const handler: NextApiHandler = async (req, res) => {
return res.status(400).send("Property 'url' is not a string.");
}

if (docsConfigId != null && typeof docsConfigId !== "string") {
return res.status(400).send("Property 'docsConfigId' is not a string.");
}

// when we call res.revalidate() nextjs uses
// req.headers.host to make the network request
if (typeof req.headers["x-fern-host"] === "string") {
req.headers.host = req.headers["x-fern-host"];
}

const docs = await REGISTRY_SERVICE.docs.v2.read.getDocsForUrl({
url,
});
if (!docs.ok) {
// eslint-disable-next-line no-console
console.error("Failed to fetch docs", docs.error);
return res.status(500).send("Failed to load docs for: " + url);
}
let pathsToRevalidate: string[] = [];
if (docsConfigId != null) {
const docsConfigResponse = await REGISTRY_SERVICE.docs.v2.read.getDocsConfigById(docsConfigId);
if (!docsConfigResponse.ok) {
// eslint-disable-next-line no-console
console.error("Failed to fetch docs by config id", docsConfigResponse.error);
return res.status(500).send("Failed to load docs for: " + docsConfigId);
}

const { navigation: navigationConfig } = docs.body.definition.config;
// eslint-disable-next-line no-console
console.log("Finding paths to revalidate");

pathsToRevalidate = getPathsToRevalidate({
navigationConfig: docsConfigResponse.body.docsConfig.navigation,
apis: docsConfigResponse.body.apis,
});
} else {
const docs = await REGISTRY_SERVICE.docs.v2.read.getDocsForUrl({
url,
});
if (!docs.ok) {
// eslint-disable-next-line no-console
console.error("Failed to fetch docs", docs.error);
return res.status(500).send("Failed to load docs for: " + url);
}

// eslint-disable-next-line no-console
console.log("Finding paths to revalidate");
// eslint-disable-next-line no-console
console.log("Finding paths to revalidate");

const pathsToRevalidate: string[] = getPathsToRevalidate({
navigationConfig,
docsDefinition: docs.body.definition,
});
pathsToRevalidate = getPathsToRevalidate({
navigationConfig: docs.body.definition.config.navigation,
apis: docs.body.definition.apis,
});
}

// eslint-disable-next-line no-console
console.log(`Found ${pathsToRevalidate.length} paths to revalidate`);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import {
DocsDefinition,
UnversionedTabbedNavigationConfig,
} from "@fern-fern/registry-browser/api/resources/docs/resources/v1/resources/read";
import { FernRegistry } from "@fern-fern/registry-browser";
import { UnversionedTabbedNavigationConfig } from "@fern-fern/registry-browser/api/resources/docs/resources/v1/resources/read";
import { getPathsForUnversionedUntabbedDocs } from "./getPathsForUnversionedUntabbedDocs";

export function getPathsForUnversionedTabbedDocs({
prefix,
navigationConfig,
docsDefinition,
apis,
}: {
prefix: string | undefined;
navigationConfig: UnversionedTabbedNavigationConfig;
docsDefinition: DocsDefinition;
apis: Record<FernRegistry.ApiDefinitionId, FernRegistry.api.v1.read.ApiDefinition>;
}): Set<string> {
const pathsToRevalidate = new Set<string>(["/"]);
navigationConfig.tabs.forEach((tab) => {
Expand All @@ -27,7 +25,7 @@ export function getPathsForUnversionedTabbedDocs({
navigationConfig: {
items: tab.items,
},
docsDefinition,
apis,
}).forEach((val) => {
pathsToRevalidate.add(val);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
import {
DocsDefinition,
UnversionedUntabbedNavigationConfig,
} from "@fern-fern/registry-browser/api/resources/docs/resources/v1/resources/read";
import { FernRegistry } from "@fern-fern/registry-browser";
import { UnversionedUntabbedNavigationConfig } from "@fern-fern/registry-browser/api/resources/docs/resources/v1/resources/read";
import { UrlSlugTree } from "@fern-ui/app-utils";

export function getPathsForUnversionedUntabbedDocs({
prefix,
navigationConfig,
docsDefinition,
apis,
}: {
prefix: string | undefined;
navigationConfig: UnversionedUntabbedNavigationConfig;
docsDefinition: DocsDefinition;
apis: Record<FernRegistry.ApiDefinitionId, FernRegistry.api.v1.read.ApiDefinition>;
}): Set<string> {
const urlSlugTree = new UrlSlugTree({
items: navigationConfig.items,
loadApiDefinition: (id) => docsDefinition.apis[id],
loadApiDefinition: (id) => apis[id],
});
return new Set<string>([
"/",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import {
DocsDefinition,
VersionedNavigationConfig,
} from "@fern-fern/registry-browser/api/resources/docs/resources/v1/resources/read";
import { FernRegistry } from "@fern-fern/registry-browser";
import { VersionedNavigationConfig } from "@fern-fern/registry-browser/api/resources/docs/resources/v1/resources/read";
import { isUnversionedUntabbedNavigationConfig } from "@fern-ui/app-utils";
import { getPathsForUnversionedTabbedDocs } from "./getPathsForTabbedDocs";
import { getPathsForUnversionedUntabbedDocs } from "./getPathsForUnversionedUntabbedDocs";

export function getPathsToRevalidateForVersionedDocs({
navigationConfig,
docsDefinition,
apis,
}: {
navigationConfig: VersionedNavigationConfig;
docsDefinition: DocsDefinition;
apis: Record<FernRegistry.ApiDefinitionId, FernRegistry.api.v1.read.ApiDefinition>;
}): Set<string> {
const pathsToRevalidate: Set<string> = new Set<string>(["/"]);
navigationConfig.versions.forEach((version, idx) => {
Expand All @@ -21,15 +19,15 @@ export function getPathsToRevalidateForVersionedDocs({
getPathsForUnversionedUntabbedDocs({
prefix: undefined,
navigationConfig: version.config,
docsDefinition,
apis,
}).forEach((val) => {
pathsToRevalidate.add(val);
});
}
getPathsForUnversionedUntabbedDocs({
prefix: `/${version.urlSlug}`,
navigationConfig: version.config,
docsDefinition,
apis,
}).forEach((val) => {
pathsToRevalidate.add(val);
});
Expand All @@ -38,15 +36,15 @@ export function getPathsToRevalidateForVersionedDocs({
getPathsForUnversionedTabbedDocs({
prefix: undefined,
navigationConfig: version.config,
docsDefinition,
apis,
}).forEach((val) => {
pathsToRevalidate.add(val);
});
}
getPathsForUnversionedTabbedDocs({
prefix: `/${version.urlSlug}`,
navigationConfig: version.config,
docsDefinition,
apis,
}).forEach((val) => {
pathsToRevalidate.add(val);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,29 @@
import {
DocsDefinition,
NavigationConfig,
} from "@fern-fern/registry-browser/api/resources/docs/resources/v1/resources/read";
import { FernRegistry } from "@fern-fern/registry-browser";
import { NavigationConfig } from "@fern-fern/registry-browser/api/resources/docs/resources/v1/resources/read";
import { isUnversionedUntabbedNavigationConfig, isVersionedNavigationConfig } from "@fern-ui/app-utils";
import { getPathsForUnversionedTabbedDocs } from "./getPathsForTabbedDocs";
import { getPathsForUnversionedUntabbedDocs } from "./getPathsForUnversionedUntabbedDocs";
import { getPathsToRevalidateForVersionedDocs } from "./getPathsForVersionedDocs";

export function getPathsToRevalidate({
navigationConfig,
docsDefinition,
apis,
}: {
navigationConfig: NavigationConfig;
docsDefinition: DocsDefinition;
apis: Record<FernRegistry.ApiDefinitionId, FernRegistry.api.v1.read.ApiDefinition>;
}): string[] {
if (isVersionedNavigationConfig(navigationConfig)) {
return Array.from(
getPathsToRevalidateForVersionedDocs({
navigationConfig,
docsDefinition,
apis,
})
);
} else if (isUnversionedUntabbedNavigationConfig(navigationConfig)) {
return Array.from(
getPathsForUnversionedUntabbedDocs({
navigationConfig,
docsDefinition,
apis,
prefix: undefined,
})
);
Expand All @@ -34,7 +32,7 @@ export function getPathsToRevalidate({
return Array.from(
getPathsForUnversionedTabbedDocs({
navigationConfig,
docsDefinition,
apis,
prefix: undefined,
})
);
Expand Down
14 changes: 7 additions & 7 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1851,24 +1851,24 @@ __metadata:
languageName: node
linkType: hard

"@fern-fern/registry-browser@npm:0.19.1-10-gf615da5":
version: 0.19.1-10-gf615da5
resolution: "@fern-fern/registry-browser@npm:0.19.1-10-gf615da5"
"@fern-fern/registry-browser@npm:0.20.0-2-g764a1b3":
version: 0.20.0-2-g764a1b3
resolution: "@fern-fern/registry-browser@npm:0.20.0-2-g764a1b3"
dependencies:
"@types/url-join": 4.0.1
"@ungap/url-search-params": 0.2.2
axios: 1.4.0
js-base64: 3.7.2
url-join: 4.0.1
checksum: 26d1077db21daa51e5e1a9c56a4dd140f302a90e009aa44d08d547ad0da8fc3f0ff95907f05e972af324695f2f1227246b5340480bd86d57574b1356308620c0
checksum: 544089d6cc1e52c4578a871e40f572213b34d171263e9e9d440f21a0e82f3897872db7adb3e547b25b33385eb8db8472c938c5750c01c259ed48a4fb540c22b2
languageName: node
linkType: hard

"@fern-ui/app-utils@workspace:*, @fern-ui/app-utils@workspace:packages/commons/app-utils":
version: 0.0.0-use.local
resolution: "@fern-ui/app-utils@workspace:packages/commons/app-utils"
dependencies:
"@fern-fern/registry-browser": 0.19.1-10-gf615da5
"@fern-fern/registry-browser": 0.20.0-2-g764a1b3
"@fern-ui/core-utils": "workspace:*"
"@types/jest": ^29.0.3
"@types/lodash-es": 4.17.9
Expand Down Expand Up @@ -1961,7 +1961,7 @@ __metadata:
version: 0.0.0-use.local
resolution: "@fern-ui/fe-bundle@workspace:packages/ui/fe-bundle"
dependencies:
"@fern-fern/registry-browser": 0.19.1-10-gf615da5
"@fern-fern/registry-browser": 0.20.0-2-g764a1b3
"@fern-ui/app-utils": "workspace:*"
"@fern-ui/core-utils": "workspace:*"
"@fern-ui/theme": "workspace:*"
Expand Down Expand Up @@ -2200,7 +2200,7 @@ __metadata:
"@blueprintjs/icons": ^4.4.0
"@blueprintjs/popover2": ^1.8.0
"@blueprintjs/select": ^4.4.2
"@fern-fern/registry-browser": 0.19.1-10-gf615da5
"@fern-fern/registry-browser": 0.20.0-2-g764a1b3
"@fern-ui/app-utils": "workspace:*"
"@fern-ui/core-utils": "workspace:*"
"@fern-ui/react-commons": "workspace:*"
Expand Down

1 comment on commit 9dde2fd

@vercel
Copy link

@vercel vercel bot commented on 9dde2fd Sep 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

fern-dev – ./packages/ui/fe-bundle

fern-dev-buildwithfern.vercel.app
devtest.buildwithfern.com
app-dev.buildwithfern.com
fern-dev-git-main-buildwithfern.vercel.app

Please sign in to comment.