Skip to content

Commit

Permalink
move to sep module, update test
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Jan 9, 2025
1 parent 0bf605e commit a593767
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 52 deletions.
4 changes: 2 additions & 2 deletions packages/next/src/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import { makeRe } from 'next/dist/compiled/picomatch'
import { existsSync, promises as fs } from 'fs'
import os from 'os'
import { Worker } from '../lib/worker'
import { defaultConfig } from '../server/config-shared'
import {
defaultConfig,
nextConfigNormalizer,
type SerializableNextConfig,
} from '../server/config-shared'
} 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
6 changes: 2 additions & 4 deletions packages/next/src/build/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import type {
NextConfigComplete,
SerializableNextConfig,
} from '../server/config-shared'
import type { NextConfigComplete } from '../server/config-shared'
import type { ExperimentalPPRConfig } from '../server/lib/experimental/ppr'
import type { AppBuildManifest } from './webpack/plugins/app-build-manifest-plugin'
import type { AssetBinding } from './webpack/loaders/get-module-build-info'
Expand All @@ -21,6 +18,7 @@ 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
2 changes: 1 addition & 1 deletion packages/next/src/server/base-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ 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 './config-shared'
import { nextConfigNormalizer } from './serializable-config'
import { shouldServeStreamingMetadata } from './lib/streaming-metadata'

export type FindComponentsResult = {
Expand Down
44 changes: 0 additions & 44 deletions packages/next/src/server/config-shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1216,47 +1216,3 @@ export async function normalizeConfig(phase: string, config: any) {
// Support `new Promise` and `async () =>` as return values of the config export
return await config
}

// Create a type that overrides the experimental htmlLimitedBots field from RegExp to string
export type SerializableNextConfig = Omit<
NextConfigComplete,
'experimental'
> & {
experimental: Omit<NextConfigComplete['experimental'], 'htmlLimitedBots'> & {
htmlLimitedBots: string
}
}

// Before serializing the config to the required files, we need to change the fields
// that are primitives like RegExp that cannot handled by JSON.stringify to string,
// so that they can be serialized properly rather than be treat as {}.
function toSerializableNextConfig(config: NextConfigComplete) {
const normalizedConfig: any = { ...config }
if (config.experimental?.htmlLimitedBots instanceof RegExp) {
normalizedConfig.experimental.htmlLimitedBots =
config.experimental.htmlLimitedBots.source
}

return normalizedConfig as SerializableNextConfig
}

// Recover from the serialized config to the original config.
// For normal cases this config is NextConfigComplete, but for the serialized config such as deployment,
// this is SerializableNextConfig where being stringified and inlined in the code.
function toNormalizedNextConfig(
config: SerializableNextConfig | NextConfigComplete
) {
const normalizedConfig: any = { ...config }
if (typeof config.experimental?.htmlLimitedBots === 'string') {
normalizedConfig.experimental.htmlLimitedBots = new RegExp(
config.experimental.htmlLimitedBots
)
}

return normalizedConfig as NextConfigComplete
}

export const nextConfigNormalizer = {
toSerializableNextConfig,
toNormalizedNextConfig,
}
45 changes: 45 additions & 0 deletions packages/next/src/server/serializable-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import type { NextConfigComplete } from './config-shared'

// Create a type that overrides the experimental htmlLimitedBots field from RegExp to string
export type SerializableNextConfig = Omit<
NextConfigComplete,
'experimental'
> & {
experimental: Omit<NextConfigComplete['experimental'], 'htmlLimitedBots'> & {
htmlLimitedBots: string
}
}

// Before serializing the config to the required files, we need to change the fields
// that are primitives like RegExp that cannot handled by JSON.stringify to string,
// so that they can be serialized properly rather than be treat as {}.
function toSerializableNextConfig(config: NextConfigComplete) {
const normalizedConfig: any = { ...config }
if (config.experimental?.htmlLimitedBots instanceof RegExp) {
normalizedConfig.experimental.htmlLimitedBots =
config.experimental.htmlLimitedBots.source
}

return normalizedConfig as SerializableNextConfig
}

// Recover from the serialized config to the original config.
// For normal cases this config is NextConfigComplete, but for the serialized config such as deployment,
// this is SerializableNextConfig where being stringified and inlined in the code.
function toNormalizedNextConfig(
config: SerializableNextConfig | NextConfigComplete
) {
const normalizedConfig: any = { ...config }
if (typeof config.experimental?.htmlLimitedBots === 'string') {
normalizedConfig.experimental.htmlLimitedBots = new RegExp(
config.experimental.htmlLimitedBots
)
}

return normalizedConfig as NextConfigComplete
}

export const nextConfigNormalizer = {
toSerializableNextConfig,
toNormalizedNextConfig,
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('app-dir - metadata-streaming-customized-rule', () => {
module.exports = {
experimental: {
streamingMetadata: true,
htmlLimitedBots: ['Minibot'],
htmlLimitedBots: /Minibot/i,
}
}
`,
Expand Down

0 comments on commit a593767

Please sign in to comment.