Skip to content

Commit

Permalink
simplify the serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Jan 10, 2025
1 parent a593767 commit 048d9c9
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 100 deletions.
17 changes: 6 additions & 11 deletions packages/next/src/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ import { existsSync, promises as fs } from 'fs'
import os from 'os'
import { Worker } from '../lib/worker'
import { defaultConfig } from '../server/config-shared'
import {
nextConfigNormalizer,
type SerializableNextConfig,
} from '../server/serializable-config'
import devalue from 'next/dist/compiled/devalue'
import findUp from 'next/dist/compiled/find-up'
import { nanoid } from 'next/dist/compiled/nanoid/index.cjs'
Expand Down Expand Up @@ -219,7 +215,7 @@ import {
getParsedNodeOptionsWithoutInspect,
} from '../server/lib/utils'
import { InvariantError } from '../shared/lib/invariant-error'
import { HTML_LIMITED_BOT_UA_RE } from '../shared/lib/router/utils/is-bot'
import { HTML_LIMITED_BOT_UA_RE_STRING } from '../shared/lib/router/utils/is-bot'

type Fallback = null | boolean | string

Expand Down Expand Up @@ -533,7 +529,7 @@ async function writeFunctionsConfigManifest(

interface RequiredServerFilesManifest {
version: number
config: SerializableNextConfig
config: NextConfigComplete
appDir: string
relativeAppDir: string
files: string[]
Expand Down Expand Up @@ -1323,9 +1319,8 @@ export default async function build(
htmlLimitedBots: string
} = {
version: 0,
htmlLimitedBots: (
config.experimental.htmlLimitedBots || HTML_LIMITED_BOT_UA_RE
).source,
htmlLimitedBots:
config.experimental.htmlLimitedBots || HTML_LIMITED_BOT_UA_RE_STRING,
}
await writeManifest(responseConfigManifestPath, responseConfigManifest)

Expand Down Expand Up @@ -2480,7 +2475,7 @@ export default async function build(

const serverFilesManifest: RequiredServerFilesManifest = {
version: 1,
config: nextConfigNormalizer.toSerializableNextConfig({
config: {
...config,
configFile: undefined,
...(ciEnvironment.hasNextSupport
Expand All @@ -2499,7 +2494,7 @@ export default async function build(
// @ts-expect-error internal field TODO: fix this, should use a separate mechanism to pass the info.
isExperimentalCompile: isCompileMode,
},
}),
},
appDir: dir,
relativeAppDir: path.relative(outputFileTracingRoot, dir),
files: [
Expand Down
3 changes: 1 addition & 2 deletions packages/next/src/build/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import type { WebpackLayerName } from '../lib/constants'
import type { AppPageModule } from '../server/route-modules/app-page/module'
import type { RouteModule } from '../server/route-modules/route-module'
import type { NextComponentType } from '../shared/lib/utils'
import type { SerializableNextConfig } from '../server/serializable-config'

import '../server/require-hook'
import '../server/node-polyfill-crypto'
Expand Down Expand Up @@ -1467,7 +1466,7 @@ export async function copyTracedFiles(
pageKeys: readonly string[],
appPageKeys: readonly string[] | undefined,
tracingRoot: string,
serverConfig: SerializableNextConfig,
serverConfig: NextConfigComplete,
middlewareManifest: MiddlewareManifest,
hasInstrumentationHook: boolean,
staticPages: Set<string>
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/server/app-render/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ export interface RenderOptsPartial {
inlineCss: boolean
authInterrupts: boolean
streamingMetadata: boolean
htmlLimitedBots: RegExp | undefined
htmlLimitedBots: string | undefined
}
postponed?: string

Expand Down
8 changes: 3 additions & 5 deletions packages/next/src/server/base-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ import type { RouteModule } from './route-modules/route-module'
import { FallbackMode, parseFallbackField } from '../lib/fallback'
import { toResponseCacheEntry } from './response-cache/utils'
import { scheduleOnNextTick } from '../lib/scheduler'
import { nextConfigNormalizer } from './serializable-config'
import { shouldServeStreamingMetadata } from './lib/streaming-metadata'

export type FindComponentsResult = {
Expand Down Expand Up @@ -485,9 +484,7 @@ export default abstract class Server<

// TODO: should conf be normalized to prevent missing
// values from causing issues as this can be user provided
this.nextConfig = nextConfigNormalizer.toNormalizedNextConfig(
conf as NextConfigComplete
)
this.nextConfig = conf as NextConfigComplete
this.hostname = hostname
if (this.hostname) {
// we format the hostname so that it can be fetched
Expand Down Expand Up @@ -599,7 +596,8 @@ export default abstract class Server<
inlineCss: this.nextConfig.experimental.inlineCss ?? false,
authInterrupts: !!this.nextConfig.experimental.authInterrupts,
streamingMetadata: !!this.nextConfig.experimental.streamingMetadata,
htmlLimitedBots: this.nextConfig.experimental.htmlLimitedBots,
htmlLimitedBots:
this.nextConfig.experimental.htmlLimitedBots.toString(),
},
onInstrumentationRequestError:
this.instrumentationOnRequestError.bind(this),
Expand Down
8 changes: 6 additions & 2 deletions packages/next/src/server/config-shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,18 @@ import type { ExpireTime } from './lib/revalidate'
import type { SupportedTestRunners } from '../cli/next-test'
import type { ExperimentalPPRConfig } from './lib/experimental/ppr'
import { INFINITE_CACHE } from '../lib/constants'
import { HTML_LIMITED_BOT_UA_RE } from '../shared/lib/router/utils/is-bot'

export type NextConfigComplete = Required<NextConfig> & {
images: Required<ImageConfigComplete>
typescript: Required<TypeScriptConfig>
configOrigin?: string
configFile?: string
configFileName: string
// override NextConfigComplete.experimental.htmlLimitedBots to string
// because it's not defined in NextConfigComplete.experimental
experimental: Omit<ExperimentalConfig, 'htmlLimitedBots'> & {
htmlLimitedBots: string
}
}

export type I18NDomains = readonly DomainLocale[]
Expand Down Expand Up @@ -1204,7 +1208,7 @@ export const defaultConfig: NextConfig = {
inlineCss: false,
newDevOverlay: false,
streamingMetadata: false,
htmlLimitedBots: HTML_LIMITED_BOT_UA_RE,
htmlLimitedBots: undefined,
},
bundlePagesRouterDependencies: false,
}
Expand Down
9 changes: 4 additions & 5 deletions packages/next/src/server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1222,11 +1222,10 @@ export default async function loadConfig(
}
}

// For deployment, deserialize the regex config
if (typeof userConfig.experimental?.htmlLimitedBots === 'string') {
userConfig.experimental.htmlLimitedBots = new RegExp(
userConfig.experimental.htmlLimitedBots
)
// serialize the regex config into string
if (userConfig.experimental?.htmlLimitedBots instanceof RegExp) {
userConfig.experimental.htmlLimitedBots =
userConfig.experimental.htmlLimitedBots.source
}

onLoadUserConfig?.(userConfig)
Expand Down
9 changes: 6 additions & 3 deletions packages/next/src/server/lib/streaming-metadata.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HTML_LIMITED_BOT_UA_RE } from '../../shared/lib/router/utils/is-bot'
import { HTML_LIMITED_BOT_UA_RE_STRING } from '../../shared/lib/router/utils/is-bot'

export function shouldServeStreamingMetadata(
userAgent: string,
Expand All @@ -7,13 +7,16 @@ export function shouldServeStreamingMetadata(
htmlLimitedBots,
}: {
streamingMetadata: boolean
htmlLimitedBots: RegExp | undefined
htmlLimitedBots: string | undefined
}
): boolean {
if (!streamingMetadata) {
return false
}

const blockingMetadataUARegex = htmlLimitedBots || HTML_LIMITED_BOT_UA_RE
const blockingMetadataUARegex = new RegExp(
htmlLimitedBots || HTML_LIMITED_BOT_UA_RE_STRING,
'i'
)
return !blockingMetadataUARegex.test(userAgent)
}
45 changes: 0 additions & 45 deletions packages/next/src/server/serializable-config.ts

This file was deleted.

29 changes: 3 additions & 26 deletions packages/next/src/shared/lib/router/utils/is-bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,11 @@ const HEADLESS_BROWSER_BOT_UA_RE =

// This regex contains the bots that we need to do a blocking render for and can't safely stream the response
// due to how they parse the DOM. For example, they might explicitly check for metadata in the `head` tag, so we can't stream metadata tags after the `head` was sent.
export const HTML_LIMITED_BOT_UA_ARRAY = [
'Mediapartners-Google',
'Slurp',
'DuckDuckBot',
'baiduspider',
'yandex',
'sogou',
'bitlybot',
'tumblr',
'vkShare',
'quora link preview',
'redditbot',
'ia_archiver',
'Bingbot',
'BingPreview',
'applebot',
'facebookexternalhit',
'facebookcatalog',
'Twitterbot',
'LinkedInBot',
'Slackbot',
'Discordbot',
'WhatsApp',
'SkypeUriPreview',
]
export const HTML_LIMITED_BOT_UA_RE_STRING =
'Mediapartners-Google|Slurp|DuckDuckBot|baiduspider|yandex|sogou|bitlybot|tumblr|vkShare|quora link preview|redditbot|ia_archiver|Bingbot|BingPreview|applebot|facebookexternalhit|facebookcatalog|Twitterbot|LinkedInBot|Slackbot|Discordbot|WhatsApp|SkypeUriPreview'

export const HTML_LIMITED_BOT_UA_RE = new RegExp(
HTML_LIMITED_BOT_UA_ARRAY.join('|'),
HTML_LIMITED_BOT_UA_RE_STRING,
'i'
)

Expand Down

0 comments on commit 048d9c9

Please sign in to comment.