Skip to content

Commit 62a71db

Browse files
committed
look at middleware manifest to check if middleware exists
1 parent 2c7a1ca commit 62a71db

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import type {
22
PHASE_PRODUCTION_BUILD as NEXT_PHASE_PRODUCTION_BUILD,
33
ROUTES_MANIFEST as NEXT_ROUTES_MANIFEST,
4+
MIDDLEWARE_MANIFEST as NEXT_MIDDLEWARE_MANIFEST,
45
} from "next/constants.js";
56

67
// export next/constants ourselves so we don't have to dynamically import them
78
// also this gives us a better heads up if the NextJS API changes
89
export const PHASE_PRODUCTION_BUILD: typeof NEXT_PHASE_PRODUCTION_BUILD = "phase-production-build";
910
export const ROUTES_MANIFEST: typeof NEXT_ROUTES_MANIFEST = "routes-manifest.json";
11+
export const MIDDLEWARE_MANIFEST: typeof NEXT_MIDDLEWARE_MANIFEST = "middleware-manifest.json";

packages/@apphosting/adapter-nextjs/src/overrides.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
import { MiddlewareManifest } from "next/dist/build/webpack/plugins/middleware-plugin.js";
12
import { AdapterMetadata } from "./interfaces.js";
2-
import { loadRouteManifest, writeRouteManifest, middlewareExists } from "./utils.js";
3+
import { loadRouteManifest, writeRouteManifest, loadMiddlewareManifest } from "./utils.js";
34

45
export async function addRouteOverrides(
56
appPath: string,
67
distDir: string,
78
adapterMetadata: AdapterMetadata,
89
) {
10+
const middlewareManifest = await loadMiddlewareManifest(appPath, distDir);
911
const routeManifest = await loadRouteManifest(appPath, distDir);
1012
routeManifest.headers.push({
1113
source: "/:path*",
@@ -14,7 +16,7 @@ export async function addRouteOverrides(
1416
key: "x-fah-adapter",
1517
value: `nextjs-${adapterMetadata.adapterVersion}`,
1618
},
17-
...(middlewareExists(appPath, distDir)
19+
...(middlewareExists(middlewareManifest)
1820
? [
1921
{
2022
key: "x-fah-middleware",
@@ -28,3 +30,7 @@ export async function addRouteOverrides(
2830

2931
await writeRouteManifest(appPath, distDir, routeManifest);
3032
}
33+
34+
function middlewareExists(middlewareManifest: MiddlewareManifest) {
35+
return Object.keys(middlewareManifest.middleware).length > 0;
36+
}

packages/@apphosting/adapter-nextjs/src/utils.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import { OutputBundleOptions, RoutesManifest } from "./interfaces.js";
99
import { NextConfigComplete } from "next/dist/server/config-shared.js";
1010
import { OutputBundleConfig } from "@apphosting/common";
1111
import { AdapterMetadata } from "./interfaces.js";
12+
import { MiddlewareManifest } from "next/dist/build/webpack/plugins/middleware-plugin.js";
13+
import { MIDDLEWARE_MANIFEST } from "next/constants.js";
1214

1315
// fs-extra is CJS, readJson can't be imported using shorthand
1416
export const { move, exists, writeFile, readJson, readdir, readFileSync, existsSync, mkdir } =
@@ -43,6 +45,15 @@ export async function loadRouteManifest(
4345
return JSON.parse(json) as RoutesManifest;
4446
}
4547

48+
export async function loadMiddlewareManifest(
49+
standalonePath: string,
50+
distDir: string,
51+
): Promise<MiddlewareManifest> {
52+
const manifestPath = join(standalonePath, distDir, MIDDLEWARE_MANIFEST);
53+
const json = readFileSync(manifestPath, "utf-8");
54+
return JSON.parse(json) as MiddlewareManifest;
55+
}
56+
4657
export async function writeRouteManifest(
4758
standalonePath: string,
4859
distDir: string,
@@ -52,11 +63,6 @@ export async function writeRouteManifest(
5263
await writeFile(manifestPath, JSON.stringify(customManifest));
5364
}
5465

55-
export function middlewareExists(standalonePathDir: string, distdir: string): boolean {
56-
const middlwarePath = join(standalonePathDir, distdir, "server/middleware.js");
57-
return existsSync(middlwarePath);
58-
}
59-
6066
export const isMain = (meta: ImportMeta): boolean => {
6167
if (!meta) return false;
6268
if (!process.argv[1]) return false;

0 commit comments

Comments
 (0)