Skip to content

Commit

Permalink
more robust buildId
Browse files Browse the repository at this point in the history
  • Loading branch information
abvthecity committed Sep 11, 2024
1 parent 7b144b0 commit 2395766
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions packages/ui/docs-bundle/src/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// eslint-disable-next-line import/no-internal-modules
import { FernUser, getAuthEdgeConfig, verifyFernJWTConfig } from "@fern-ui/ui/auth";
import { NextResponse, type MiddlewareConfig, type NextMiddleware } from "next/server";
import { NextRequest, NextResponse, type MiddlewareConfig, type NextMiddleware } from "next/server";
import urlJoin from "url-join";
import { extractBuildId, extractNextDataPathname } from "./utils/extractNextDataPathname";
import { getPageRoute, getPageRouteMatch, getPageRoutePath } from "./utils/pageRoutes";
Expand Down Expand Up @@ -130,7 +130,7 @@ export const middleware: NextMiddleware = async (request) => {
* Mock the /_next/data/... request to the corresponding page route
*/
if (request.nextUrl.pathname.includes("/_next/data/")) {
const buildId = request.nextUrl.buildId ?? extractBuildId(request.nextUrl.pathname) ?? "development";
const buildId = getBuildId(request);
nextUrl.pathname = getPageRoutePath(!isDynamic, buildId, xFernHost, pathname);

const response = NextResponse.rewrite(nextUrl, {
Expand Down Expand Up @@ -164,3 +164,11 @@ export const config: MiddlewareConfig = {
"/((?!api/fern-docs|_next/static|_next/image|favicon.ico).*)",
],
};

function getBuildId(req: NextRequest): string {
return (
req.nextUrl.buildId ??
extractBuildId(req.nextUrl.pathname) ??
(process.env.NODE_ENV === "development" ? "development" : "")
);
}

0 comments on commit 2395766

Please sign in to comment.