Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions packages/next/src/server/lib/streaming-metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,23 @@ import {
} from '../../shared/lib/router/utils/is-bot'
import type { BaseNextRequest } from '../base-http'

// Cache compiled RegExp per pattern string to avoid re-creating it on every
// request. The pattern comes from `nextConfig.htmlLimitedBots` which is a
// static config value, so the cache will hold at most 1-2 entries.
let cachedPattern: string | undefined
let cachedRegex: RegExp | undefined

export function shouldServeStreamingMetadata(
userAgent: string,
htmlLimitedBots: string | undefined
): boolean {
const blockingMetadataUARegex = new RegExp(
htmlLimitedBots || HTML_LIMITED_BOT_UA_RE_STRING,
'i'
)
const pattern = htmlLimitedBots || HTML_LIMITED_BOT_UA_RE_STRING
if (pattern !== cachedPattern) {
cachedPattern = pattern
cachedRegex = new RegExp(pattern, 'i')
}
// Only block metadata for HTML-limited bots
if (userAgent && blockingMetadataUARegex.test(userAgent)) {
if (userAgent && cachedRegex!.test(userAgent)) {
return false
}
return true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ export const INTERCEPTION_ROUTE_MARKERS = [
export type InterceptionMarker = (typeof INTERCEPTION_ROUTE_MARKERS)[number]

export function isInterceptionRouteAppPath(path: string): boolean {
// TODO-APP: add more serious validation
// Fast path: interception markers always start with '(' — skip the
// split + find for the vast majority of paths that have no parentheses.
if (!path.includes('(')) return false

return (
path
.split('/')
Expand Down
Loading