Skip to content

Commit

Permalink
fix: set the canonical url manually for subpath customers that are no…
Browse files Browse the repository at this point in the history
…t setting x-fern-host (#1610)
  • Loading branch information
abvthecity authored Oct 8, 2024
1 parent 44ab44e commit ef19e11
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
14 changes: 11 additions & 3 deletions packages/ui/docs-bundle/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { getPageRoute, getPageRouteMatch, getPageRoutePath } from "@/server/page
import { rewritePosthog } from "@/server/rewritePosthog";
import { getXFernHostEdge } from "@/server/xfernhost/edge";
import type { FernUser } from "@fern-ui/fern-docs-auth";
import { getAuthEdgeConfig } from "@fern-ui/fern-docs-edge-config";
import { COOKIE_FERN_TOKEN } from "@fern-ui/fern-docs-utils";
import { getAuthEdgeConfig, getCanonicalHost } from "@fern-ui/fern-docs-edge-config";
import { COOKIE_FERN_TOKEN, HEADER_X_FERN_HOST } from "@fern-ui/fern-docs-utils";
import { removeTrailingSlash } from "next/dist/shared/lib/router/utils/remove-trailing-slash";
import { NextRequest, NextResponse, type NextMiddleware } from "next/server";
import urlJoin from "url-join";
Expand All @@ -15,10 +15,18 @@ const API_FERN_DOCS_PATTERN = /^(?!\/api\/fern-docs\/).*(\/api\/fern-docs\/)/;
const CHANGELOG_PATTERN = /\.(rss|atom)$/;

export const middleware: NextMiddleware = async (request) => {
const xFernHost = getXFernHostEdge(request);
let xFernHost = getXFernHostEdge(request);
const nextUrl = request.nextUrl.clone();
const headers = new Headers(request.headers);

// for legacy docs customers that use subpath routing and don't set x-fern-host,
// we use edge config to determine the true canonical host
const canonicalHost = await getCanonicalHost(xFernHost);
if (canonicalHost !== xFernHost) {
xFernHost = canonicalHost;
headers.set(HEADER_X_FERN_HOST, xFernHost);
}

/**
* Do not rewrite 404 and 500 pages
*/
Expand Down
7 changes: 7 additions & 0 deletions packages/ui/fern-docs-edge-config/src/getCanonicalHost.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { get } from "@vercel/edge-config";

export async function getCanonicalHost(host: string): Promise<string> {
const config = await get<Record<string, string>>("canonical-host");
const canonicalHost = config?.[host];
return canonicalHost ?? host;
}
1 change: 1 addition & 0 deletions packages/ui/fern-docs-edge-config/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from "./getAuthEdgeConfig";
export * from "./getCanonicalHost";
export * from "./getCustomerAnalytics";
export * from "./getFeatureFlags";
export * from "./getInkeepSettings";
Expand Down

0 comments on commit ef19e11

Please sign in to comment.