Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: enable redirects in config #936

Merged
merged 1 commit into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/ui/docs-bundle/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@
"postcss-import": "^16.0.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"sharp": "^0.33.3"
"sharp": "^0.33.3",
"url-join": "4.0.1"
},
"devDependencies": {
"@fern-platform/configs": "workspace:*",
Expand All @@ -69,6 +70,7 @@
"@types/node-fetch": "2.6.9",
"@types/react": "^18.0.20",
"@types/react-dom": "^18.2.18",
"@types/url-join": "4.0.1",
"autoprefixer": "^10.4.16",
"depcheck": "^1.4.3",
"env-cmd": "toddbluhm/env-cmd",
Expand Down
7 changes: 5 additions & 2 deletions packages/ui/docs-bundle/src/utils/getDocsPageProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import { jwtVerify } from "jose";
import type { Redirect } from "next";
import type { IncomingMessage, ServerResponse } from "node:http";
import urljoin from "url-join";
import { getFeatureFlags } from "../pages/api/fern-docs/feature-flags";
import { getAuthorizationUrl, getJwtTokenSecret } from "./auth";
import { getRedirectForPath } from "./hackRedirects";
Expand Down Expand Up @@ -160,13 +161,15 @@ async function convertDocsToDocsPageProps({
const docsConfig = docsDefinition.config;
const pages = docs.definition.pages;

const redirect = getRedirectForPath(xFernHost, `/${slug.join("/")}`);
const currentPath = urljoin("/", slug.join("/"));

const redirect = getRedirectForPath(currentPath, docs.baseUrl, docsConfig.redirects);

if (redirect != null) {
return {
type: "redirect",
redirect: {
destination: redirect.to,
destination: redirect.destination,
permanent: false,
},
};
Expand Down
46 changes: 32 additions & 14 deletions packages/ui/docs-bundle/src/utils/hackRedirects.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,49 @@
interface Redirect {
from: string;
to: string;
}
import { DocsV1Read, DocsV2Read } from "@fern-api/fdr-sdk";
import urljoin from "url-join";

const HUME_REDIRECTS: Redirect[] = [
const HUME_REDIRECTS: DocsV1Read.RedirectConfig[] = [
{
from: "/streaming-api-tutorial",
to: "/docs/expression-measurement-api/websocket",
source: "/streaming-api-tutorial",
destination: "/docs/expression-measurement-api/websocket",
},
{
from: "/docs/support",
to: "/support",
source: "/docs/support",
destination: "/support",
},
{
from: "/streaming-api-error-codes",
to: "/docs/resources/errors",
source: "/streaming-api-error-codes",
destination: "/docs/resources/errors",
},
];

function getRedirects(domain: string): Redirect[] {
function getRedirects(domain: string): DocsV1Read.RedirectConfig[] {
if (domain.includes("hume")) {
return HUME_REDIRECTS;
}

return [];
}

export function getRedirectForPath(domain: string, path: string): Redirect | undefined {
return getRedirects(domain).find((redirect) => redirect.from === path);
export function getRedirectForPath(
path: string,
baseUrl: DocsV2Read.BaseUrl,
redirects: DocsV1Read.RedirectConfig[] = [],
): DocsV1Read.RedirectConfig | undefined {
return [...getRedirects(baseUrl.domain), ...withBasepath(redirects, baseUrl.basePath)].find(
(redirect) => redirect.source === path,
);
}

function withBasepath(
redirects: DocsV1Read.RedirectConfig[],
basePath: string | undefined,
): DocsV1Read.RedirectConfig[] {
if (basePath == null) {
return redirects;
}
return redirects.map((redirect) => ({
...redirect,
source: redirect.source.startsWith(basePath) ? redirect.source : urljoin(basePath, redirect.source),
destination: redirect.destination,
}));
}
Loading
Loading