From 23957662dfa26056ef734aed8416dccc7ad6d9ee Mon Sep 17 00:00:00 2001 From: Andrew Jiang Date: Wed, 11 Sep 2024 18:20:38 -0400 Subject: [PATCH] more robust buildId --- packages/ui/docs-bundle/src/middleware.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/ui/docs-bundle/src/middleware.ts b/packages/ui/docs-bundle/src/middleware.ts index 1a02a9eb50..8c53865c65 100644 --- a/packages/ui/docs-bundle/src/middleware.ts +++ b/packages/ui/docs-bundle/src/middleware.ts @@ -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"; @@ -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, { @@ -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" : "") + ); +}