@@ -146,9 +146,12 @@ export async function validateNextConfigOverride(
146146 * Modifies the app's route manifest (routes-manifest.json) to add Firebase App Hosting
147147 * specific overrides (i.e headers).
148148 *
149- * This function adds the following headers to all routes:
149+ * It adds the following headers to all routes:
150150 * - x-fah-adapter: The Firebase App Hosting adapter version used to build the app.
151+ *
152+ * It also adds the following headers to all routes for which middleware is enabled:
151153 * - x-fah-middleware: When middleware is enabled.
154+ *
152155 * @param appPath The path to the app directory.
153156 * @param distDir The path to the dist directory.
154157 * @param adapterMetadata The adapter metadata.
@@ -158,38 +161,48 @@ export async function addRouteOverrides(
158161 distDir : string ,
159162 adapterMetadata : AdapterMetadata ,
160163) {
161- const middlewareManifest = loadMiddlewareManifest ( appPath , distDir ) ;
162164 const routeManifest = loadRouteManifest ( appPath , distDir ) ;
165+
166+ // Add the adapter version to all routes
163167 routeManifest . headers . push ( {
164168 source : "/:path*" ,
165169 headers : [
166170 {
167171 key : "x-fah-adapter" ,
168172 value : `nextjs-${ adapterMetadata . adapterVersion } ` ,
169173 } ,
170- ...( middlewareExists ( middlewareManifest )
171- ? [
172- {
173- key : "x-fah-middleware" ,
174- value : "true" ,
175- } ,
176- ]
177- : [ ] ) ,
178174 ] ,
179175 /*
180- NextJs converts the source string to a regex using path-to-regexp (https://github.com/pillarjs/path-to-regexp) at
181- build time: https://github.com/vercel/next.js/blob/canary/packages/next/src/build/index.ts#L1273.
182- This regex is then used to match the route against the request path.
176+ NextJs converts the source string to a regex using path-to-regexp (https://github.com/pillarjs/path-to-regexp) at
177+ build time: https://github.com/vercel/next.js/blob/canary/packages/next/src/build/index.ts#L1273.
178+ This regex is then used to match the route against the request path.
183179
184- This regex was generated by building a sample NextJs app with the source string `/:path*` and then inspecting the
185- routes-manifest.json file.
186- */
180+ This regex was generated by building a sample NextJs app with the source string `/:path*` and then inspecting the
181+ routes-manifest.json file.
182+ */
187183 regex : "^(?:/((?:[^/]+?)(?:/(?:[^/]+?))*))?(?:/)?$" ,
188184 } ) ;
189185
190- await writeRouteManifest ( appPath , distDir , routeManifest ) ;
191- }
186+ // Add the middleware header to all routes for which middleware is enabled
187+ const middlewareManifest = loadMiddlewareManifest ( appPath , distDir ) ;
188+ const rootMiddleware = middlewareManifest . middleware [ "/" ] ;
189+ if ( ! rootMiddleware ?. matchers ) {
190+ console . log ( "No middleware found for root path, skipping route overrides" ) ;
191+ return ;
192+ }
192193
193- function middlewareExists ( middlewareManifest : MiddlewareManifest ) {
194- return Object . keys ( middlewareManifest . middleware ) . length > 0 ;
194+ rootMiddleware . matchers . forEach ( ( matcher ) => {
195+ routeManifest . headers . push ( {
196+ source : matcher . regexp ,
197+ headers : [
198+ {
199+ key : "x-fah-middleware" ,
200+ value : "true" ,
201+ } ,
202+ ] ,
203+ regex : matcher . regexp ,
204+ } ) ;
205+ } ) ;
206+
207+ await writeRouteManifest ( appPath , distDir , routeManifest ) ;
195208}
0 commit comments