Skip to content

Commit 175939b

Browse files
committed
less config inlining
1 parent 47f8e55 commit 175939b

File tree

6 files changed

+37
-32
lines changed

6 files changed

+37
-32
lines changed

crates/next-core/src/next_app/app_route_entry.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@ pub async fn get_app_route_entry(
113113
project_root,
114114
rsc_entry,
115115
page,
116-
next_config,
117116
);
118117
}
119118

@@ -132,17 +131,14 @@ async fn wrap_edge_route(
132131
project_root: FileSystemPath,
133132
entry: ResolvedVc<Box<dyn Module>>,
134133
page: AppPage,
135-
next_config: Vc<NextConfig>,
136134
) -> Result<Vc<Box<dyn Module>>> {
137135
let inner = rcstr!("INNER_ROUTE_ENTRY");
138136

139-
let next_config = &*next_config.await?;
140-
141137
let source = load_next_js_template(
142138
"edge-app-route.js",
143139
project_root.clone(),
144140
&[("VAR_USERLAND", &*inner), ("VAR_PAGE", &page.to_string())],
145-
&[("nextConfig", &*serde_json::to_string(next_config)?)],
141+
&[],
146142
&[],
147143
)
148144
.await?;

packages/next/src/build/entries.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,6 @@ export function getEdgeServerEntry(opts: {
621621
absolutePagePath: opts.absolutePagePath,
622622
page: opts.page,
623623
appDirLoader: Buffer.from(opts.appDirLoader || '').toString('base64'),
624-
nextConfig: Buffer.from(JSON.stringify(opts.config)).toString('base64'),
625624
preferredRegion: opts.preferredRegion,
626625
middlewareConfig: Buffer.from(
627626
JSON.stringify(opts.middlewareConfig || {})

packages/next/src/build/templates/edge-app-route.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
import { setManifestsSingleton } from '../../server/app-render/manifests-singleton'
2-
import type { NextConfigComplete } from '../../server/config-shared'
32
import type { EdgeHandler } from '../../server/web/adapter'
43
import { EdgeRouteModuleWrapper } from '../../server/web/edge-route-module-wrapper'
54

65
// Import the userland code.
76
import * as module from 'VAR_USERLAND'
87

98
// injected by the loader afterwards.
10-
declare const nextConfig: NextConfigComplete
11-
// INJECT:nextConfig
129

1310
const maybeJSONParse = (str?: string) => (str ? JSON.parse(str) : undefined)
1411

@@ -26,7 +23,6 @@ if (rscManifest && rscServerManifest) {
2623
export const ComponentMod = module
2724

2825
const handler: EdgeHandler = EdgeRouteModuleWrapper.wrap(module.routeModule, {
29-
nextConfig,
3026
page: 'VAR_PAGE',
3127
})
3228
export default handler

packages/next/src/build/webpack/loaders/next-edge-app-route-loader/index.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ export type EdgeAppRouteLoaderQuery = {
1111
page: string
1212
appDirLoader: string
1313
preferredRegion: string | string[] | undefined
14-
nextConfig: string
1514
middlewareConfig: string
1615
cacheHandlers: string
1716
}
@@ -24,7 +23,6 @@ const EdgeAppRouteLoader: webpack.LoaderDefinitionFunction<EdgeAppRouteLoaderQue
2423
preferredRegion,
2524
appDirLoader: appDirLoaderBase64 = '',
2625
middlewareConfig: middlewareConfigBase64 = '',
27-
nextConfig: nextConfigBase64,
2826
cacheHandlers: cacheHandlersStringified,
2927
} = this.getOptions()
3028

@@ -64,20 +62,13 @@ const EdgeAppRouteLoader: webpack.LoaderDefinitionFunction<EdgeAppRouteLoaderQue
6462
stringifiedPagePath.length - 1
6563
)}?${WEBPACK_RESOURCE_QUERIES.edgeSSREntry}`
6664

67-
const stringifiedConfig = Buffer.from(
68-
nextConfigBase64 || '',
69-
'base64'
70-
).toString()
71-
7265
return await loadEntrypoint(
7366
'edge-app-route',
7467
{
7568
VAR_USERLAND: modulePath,
7669
VAR_PAGE: page,
7770
},
78-
{
79-
nextConfig: stringifiedConfig,
80-
}
71+
{}
8172
)
8273
}
8374

packages/next/src/server/route-modules/route-module.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ import type { ReactLoadableManifest } from '../load-components'
4242
import type { NextFontManifest } from '../../build/webpack/plugins/next-font-manifest-plugin'
4343
import { normalizeDataPath } from '../../shared/lib/page-path/normalize-data-path'
4444
import { pathHasPrefix } from '../../shared/lib/router/utils/path-has-prefix'
45-
import { addRequestMeta, getRequestMeta } from '../request-meta'
45+
import {
46+
addRequestMeta,
47+
getRequestMeta,
48+
type NextIncomingMessage,
49+
} from '../request-meta'
4650
import { normalizePagePath } from '../../shared/lib/page-path/normalize-page-path'
4751
import { isStaticMetadataRoute } from '../../lib/metadata/is-metadata-route'
4852
import { IncrementalCache } from '../lib/incremental-cache'
@@ -501,6 +505,26 @@ export abstract class RouteModule<
501505
)
502506
}
503507

508+
/// A more lightweight version of `prepare()` for only retrieving the config on edge
509+
public getNextConfigEdge(req: NextIncomingMessage): NextConfigComplete {
510+
if (process.env.NEXT_RUNTIME !== 'edge') {
511+
throw new Error(
512+
'Invariant: getNextConfigEdge most only be called in edge runtime'
513+
)
514+
}
515+
516+
let serverFilesManifest =
517+
(self.__SERVER_FILES_MANIFEST as any as RequiredServerFilesManifest) || {}
518+
const relativeProjectDir =
519+
getRequestMeta(req, 'relativeProjectDir') || this.relativeProjectDir
520+
const routerServerContext =
521+
routerServerGlobal[RouterServerContextSymbol]?.[relativeProjectDir]
522+
const nextConfig =
523+
routerServerContext?.nextConfig || serverFilesManifest.config
524+
525+
return nextConfig
526+
}
527+
504528
public async prepare(
505529
req: IncomingMessage | BaseNextRequest,
506530
res: ServerResponse | null,

packages/next/src/server/web/edge-route-module-wrapper.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
import type { NextRequest } from './spec-extension/request'
21
import type {
32
AppRouteRouteHandlerContext,
43
AppRouteRouteModule,
54
} from '../route-modules/app-route/module'
65

76
import './globals'
87

9-
import { adapter, type EdgeHandler } from './adapter'
8+
import { adapter, type NextRequestHint, type EdgeHandler } from './adapter'
109
import { IncrementalCache } from '../lib/incremental-cache'
1110
import { RouteMatcher } from '../route-matchers/route-matcher'
1211
import type { NextFetchEvent } from './spec-extension/fetch-event'
@@ -15,10 +14,9 @@ import { getServerUtils } from '../server-utils'
1514
import { searchParamsToUrlQuery } from '../../shared/lib/router/utils/querystring'
1615
import { CloseController, trackStreamConsumed } from './web-on-close'
1716
import { getEdgePreviewProps } from './get-edge-preview-props'
18-
import type { NextConfigComplete } from '../config-shared'
17+
import { WebNextRequest } from '../../server/base-http/web'
1918

2019
export interface WrapOptions {
21-
nextConfig: NextConfigComplete
2220
page: string
2321
}
2422

@@ -36,10 +34,7 @@ export class EdgeRouteModuleWrapper {
3634
*
3735
* @param routeModule the route module to wrap
3836
*/
39-
private constructor(
40-
private readonly routeModule: AppRouteRouteModule,
41-
private readonly nextConfig: NextConfigComplete
42-
) {
37+
private constructor(private readonly routeModule: AppRouteRouteModule) {
4338
// TODO: (wyattjoh) possibly allow the module to define it's own matcher
4439
this.matcher = new RouteMatcher(routeModule.definition)
4540
}
@@ -58,7 +53,7 @@ export class EdgeRouteModuleWrapper {
5853
options: WrapOptions
5954
): EdgeHandler {
6055
// Create the module wrapper.
61-
const wrapper = new EdgeRouteModuleWrapper(routeModule, options.nextConfig)
56+
const wrapper = new EdgeRouteModuleWrapper(routeModule)
6257

6358
// Return the wrapping function.
6459
return (opts) => {
@@ -73,7 +68,7 @@ export class EdgeRouteModuleWrapper {
7368
}
7469

7570
private async handler(
76-
request: NextRequest,
71+
request: NextRequestHint,
7772
evt: NextFetchEvent
7873
): Promise<Response> {
7974
const utils = getServerUtils({
@@ -86,6 +81,10 @@ export class EdgeRouteModuleWrapper {
8681
caseSensitive: false,
8782
})
8883

84+
const nextConfig = this.routeModule.getNextConfigEdge(
85+
new WebNextRequest(request)
86+
)
87+
8988
const { params } = utils.normalizeDynamicRouteParams(
9089
searchParamsToUrlQuery(request.nextUrl.searchParams),
9190
false
@@ -116,7 +115,7 @@ export class EdgeRouteModuleWrapper {
116115
experimental: {
117116
authInterrupts: !!process.env.__NEXT_EXPERIMENTAL_AUTH_INTERRUPTS,
118117
},
119-
cacheLifeProfiles: this.nextConfig.cacheLife,
118+
cacheLifeProfiles: nextConfig.cacheLife,
120119
},
121120
sharedContext: {
122121
buildId: '', // TODO: Populate this properly.

0 commit comments

Comments
 (0)