Skip to content

Commit

Permalink
cache interception regexp
Browse files Browse the repository at this point in the history
  • Loading branch information
ztanner committed Feb 9, 2024
1 parent d5a5952 commit c329086
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 16 deletions.
13 changes: 7 additions & 6 deletions packages/next/src/server/base-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
protected readonly serverOptions: Readonly<ServerOptions>
protected readonly appPathRoutes?: Record<string, string[]>
protected readonly clientReferenceManifest?: ClientReferenceManifest
protected interceptionRouteRewrites: ManifestRewriteRoute[]
protected interceptionRoutePatterns: RegExp[]
protected nextFontManifest?: NextFontManifest
private readonly responseCache: ResponseCacheBase

Expand All @@ -332,7 +332,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
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
Expand Down Expand Up @@ -566,7 +566,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
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()
Expand Down Expand Up @@ -1749,8 +1749,8 @@ export default abstract class Server<ServerOptions extends Options = Options> {
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)
})
)
}
Expand All @@ -1763,8 +1763,9 @@ export default abstract class Server<ServerOptions extends Options = Options> {
): 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)) {
Expand Down
7 changes: 3 additions & 4 deletions packages/next/src/server/dev/next-dev-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 ?? []
}
Expand Down
9 changes: 5 additions & 4 deletions packages/next/src/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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)) ?? []
)
}

Expand Down
4 changes: 2 additions & 2 deletions packages/next/src/server/web-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -394,7 +394,7 @@ export default class NextWebServer extends BaseServer<WebServerOptions> {
return null
}

protected getInterceptionRouteRewrites(): ManifestRewriteRoute[] {
protected getinterceptionRoutePatterns(): RegExp[] {
// TODO: This needs to be implemented.
return []
}
Expand Down

0 comments on commit c329086

Please sign in to comment.