From c3290860d573ad59ddba883020b191c055cf17f8 Mon Sep 17 00:00:00 2001 From: Zack Tanner Date: Fri, 9 Feb 2024 07:45:30 -0800 Subject: [PATCH] cache interception regexp --- packages/next/src/server/base-server.ts | 13 +++++++------ packages/next/src/server/dev/next-dev-server.ts | 7 +++---- packages/next/src/server/next-server.ts | 9 +++++---- packages/next/src/server/web-server.ts | 4 ++-- 4 files changed, 17 insertions(+), 16 deletions(-) diff --git a/packages/next/src/server/base-server.ts b/packages/next/src/server/base-server.ts index 87361bc64686c..a3d173669ce58 100644 --- a/packages/next/src/server/base-server.ts +++ b/packages/next/src/server/base-server.ts @@ -323,7 +323,7 @@ export default abstract class Server { protected readonly serverOptions: Readonly protected readonly appPathRoutes?: Record protected readonly clientReferenceManifest?: ClientReferenceManifest - protected interceptionRouteRewrites: ManifestRewriteRoute[] + protected interceptionRoutePatterns: RegExp[] protected nextFontManifest?: NextFontManifest private readonly responseCache: ResponseCacheBase @@ -332,7 +332,7 @@ export default abstract class Server { protected abstract getPagesManifest(): PagesManifest | undefined protected abstract getAppPathsManifest(): PagesManifest | undefined protected abstract getBuildId(): string - protected abstract getInterceptionRouteRewrites(): ManifestRewriteRoute[] + protected abstract getinterceptionRoutePatterns(): RegExp[] protected readonly enabledDirectories: NextEnabledDirectories protected abstract getEnabledDirectories(dev: boolean): NextEnabledDirectories @@ -566,7 +566,7 @@ export default abstract class Server { this.pagesManifest = this.getPagesManifest() this.appPathsManifest = this.getAppPathsManifest() this.appPathRoutes = this.getAppPathRoutes() - this.interceptionRouteRewrites = this.getInterceptionRouteRewrites() + this.interceptionRoutePatterns = this.getinterceptionRoutePatterns() // Configure the routes. this.matchers = this.getRouteMatchers() @@ -1749,8 +1749,8 @@ export default abstract class Server { protected pathCouldBeIntercepted(resolvedPathname: string): boolean { return ( isInterceptionRouteAppPath(resolvedPathname) || - this.interceptionRouteRewrites?.some((rewrite) => { - return new RegExp(rewrite.regex).test(resolvedPathname) + this.interceptionRoutePatterns.some((regexp) => { + return regexp.test(resolvedPathname) }) ) } @@ -1763,8 +1763,9 @@ export default abstract class Server { ): void { const baseVaryHeader = `${RSC_HEADER}, ${NEXT_ROUTER_STATE_TREE}, ${NEXT_ROUTER_PREFETCH_HEADER}` const isRSCRequest = - Boolean(req.headers[RSC_HEADER.toLowerCase()]) || + req.headers[RSC_HEADER.toLowerCase()] === '1' || getRequestMeta(req, 'isRSCRequest') + let addedNextUrlToVary = false if (isAppPath && this.pathCouldBeIntercepted(resolvedPathname)) { diff --git a/packages/next/src/server/dev/next-dev-server.ts b/packages/next/src/server/dev/next-dev-server.ts index 8259837d0ad05..7620b7251aa87 100644 --- a/packages/next/src/server/dev/next-dev-server.ts +++ b/packages/next/src/server/dev/next-dev-server.ts @@ -20,7 +20,6 @@ import type { UnwrapPromise } from '../../lib/coalesced-function' import type { NodeNextResponse, NodeNextRequest } from '../base-http/node' import type { RouteEnsurer } from '../future/route-matcher-managers/dev-route-matcher-manager' import type { PagesManifest } from '../../build/webpack/plugins/pages-manifest-plugin' -import type { ManifestRewriteRoute } from '../../build' import fs from 'fs' import { Worker } from 'next/dist/compiled/jest-worker' @@ -291,7 +290,7 @@ export default class DevServer extends Server { this.ready = undefined // In dev, this needs to be called after prepare because the build entries won't be known in the constructor - this.interceptionRouteRewrites = this.getInterceptionRouteRewrites() + this.interceptionRoutePatterns = this.getinterceptionRoutePatterns() // This is required by the tracing subsystem. setGlobal('appDir', this.appDir) @@ -549,11 +548,11 @@ export default class DevServer extends Server { ) } - protected getInterceptionRouteRewrites(): ManifestRewriteRoute[] { + protected getinterceptionRoutePatterns(): RegExp[] { const rewrites = generateInterceptionRoutesRewrites( Object.keys(this.appPathRoutes ?? {}), this.nextConfig.basePath - ).map((route) => buildCustomRoute('rewrite', route)) + ).map((route) => new RegExp(buildCustomRoute('rewrite', route).regex)) return rewrites ?? [] } diff --git a/packages/next/src/server/next-server.ts b/packages/next/src/server/next-server.ts index 2b62c6b07697a..956c2604c7fa7 100644 --- a/packages/next/src/server/next-server.ts +++ b/packages/next/src/server/next-server.ts @@ -11,7 +11,7 @@ import { import type { MiddlewareManifest } from '../build/webpack/plugins/middleware-plugin' import type RenderResult from './render-result' import type { FetchEventResult } from './web/types' -import type { ManifestRewriteRoute, PrerenderManifest } from '../build' +import type { PrerenderManifest } from '../build' import type { BaseNextRequest, BaseNextResponse } from './base-http' import type { PagesManifest } from '../build/webpack/plugins/pages-manifest-plugin' import type { NextParsedUrlQuery, NextUrlWithParsedQuery } from './request-meta' @@ -372,11 +372,12 @@ export default class NextNodeServer extends BaseServer { ) as PagesManifest } - protected getInterceptionRouteRewrites(): ManifestRewriteRoute[] { + protected getinterceptionRoutePatterns(): RegExp[] { const routesManifest = this.getRoutesManifest() return ( - routesManifest?.rewrites.beforeFiles.filter(isInterceptionRouteRewrite) ?? - [] + routesManifest?.rewrites.beforeFiles + .filter(isInterceptionRouteRewrite) + .map((rewrite) => new RegExp(rewrite.regex)) ?? [] ) } diff --git a/packages/next/src/server/web-server.ts b/packages/next/src/server/web-server.ts index 3539ebda3d0ef..cf067358896f9 100644 --- a/packages/next/src/server/web-server.ts +++ b/packages/next/src/server/web-server.ts @@ -3,7 +3,7 @@ import type RenderResult from './render-result' import type { NextParsedUrlQuery, NextUrlWithParsedQuery } from './request-meta' import type { Params } from '../shared/lib/router/utils/route-matcher' import type { LoadComponentsReturnType } from './load-components' -import type { ManifestRewriteRoute, PrerenderManifest } from '../build' +import type { PrerenderManifest } from '../build' import type { LoadedRenderOpts, MiddlewareRoutingItem, @@ -394,7 +394,7 @@ export default class NextWebServer extends BaseServer { return null } - protected getInterceptionRouteRewrites(): ManifestRewriteRoute[] { + protected getinterceptionRoutePatterns(): RegExp[] { // TODO: This needs to be implemented. return [] }