Skip to content

Commit

Permalink
Pass cacheLifeProfiles to edge route module wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
unstubbable committed Oct 14, 2024
1 parent a477d82 commit 2804668
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 17 deletions.
8 changes: 7 additions & 1 deletion crates/next-core/src/next_app/app_route_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ pub async fn get_app_route_entry(
project_root,
rsc_entry,
page,
next_config,
);
}

Expand All @@ -131,17 +132,22 @@ async fn wrap_edge_route(
project_root: Vc<FileSystemPath>,
entry: Vc<Box<dyn Module>>,
page: AppPage,
next_config: Vc<NextConfig>,
) -> Result<Vc<Box<dyn Module>>> {
const INNER: &str = "INNER_ROUTE_ENTRY";

let next_config = &*next_config.await?;

let source = load_next_js_template(
"edge-app-route.js",
project_root,
fxindexmap! {
"VAR_USERLAND" => INNER.into(),
"VAR_PAGE" => page.to_string().into(),
},
fxindexmap! {},
fxindexmap! {
"nextConfig" => serde_json::to_string(next_config)?.into(),
},
fxindexmap! {},
)
.await?;
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/build/entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ export function getEdgeServerEntry(opts: {
absolutePagePath: opts.absolutePagePath,
page: opts.page,
appDirLoader: Buffer.from(opts.appDirLoader || '').toString('base64'),
nextConfigOutput: opts.config.output,
nextConfig: Buffer.from(JSON.stringify(opts.config)).toString('base64'),
preferredRegion: opts.preferredRegion,
middlewareConfig: Buffer.from(
JSON.stringify(opts.middlewareConfig || {})
Expand Down
7 changes: 6 additions & 1 deletion packages/next/src/build/templates/edge-app-route.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { createServerModuleMap } from '../../server/app-render/action-utils'
import { setReferenceManifestsSingleton } from '../../server/app-render/encryption-utils'
import type { NextConfigComplete } from '../../server/config-shared'
import { EdgeRouteModuleWrapper } from '../../server/web/edge-route-module-wrapper'

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

// injected by the loader afterwards.
declare const nextConfig: NextConfigComplete
// INJECT:nextConfig

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

const rscManifest = self.__RSC_MANIFEST?.['VAR_PAGE']
Expand All @@ -23,4 +28,4 @@ if (rscManifest && rscServerManifest) {

export const ComponentMod = module

export default EdgeRouteModuleWrapper.wrap(module.routeModule)
export default EdgeRouteModuleWrapper.wrap(module.routeModule, { nextConfig })
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { getModuleBuildInfo } from '../get-module-build-info'
import { stringifyRequest } from '../../stringify-request'
import type { NextConfig } from '../../../../server/config-shared'
import type { webpack } from 'next/dist/compiled/webpack/webpack'
import { WEBPACK_RESOURCE_QUERIES } from '../../../../lib/constants'
import type { MiddlewareConfig } from '../../../analysis/get-page-static-info'
Expand All @@ -12,7 +11,7 @@ export type EdgeAppRouteLoaderQuery = {
page: string
appDirLoader: string
preferredRegion: string | string[] | undefined
nextConfigOutput: NextConfig['output']
nextConfig: string
middlewareConfig: string
}

Expand All @@ -24,6 +23,7 @@ const EdgeAppRouteLoader: webpack.LoaderDefinitionFunction<EdgeAppRouteLoaderQue
preferredRegion,
appDirLoader: appDirLoaderBase64 = '',
middlewareConfig: middlewareConfigBase64 = '',
nextConfig: nextConfigBase64,
} = this.getOptions()

const appDirLoader = Buffer.from(appDirLoaderBase64, 'base64').toString()
Expand Down Expand Up @@ -54,10 +54,21 @@ const EdgeAppRouteLoader: webpack.LoaderDefinitionFunction<EdgeAppRouteLoaderQue
stringifiedPagePath.length - 1
)}?${WEBPACK_RESOURCE_QUERIES.edgeSSREntry}`

return await loadEntrypoint('edge-app-route', {
VAR_USERLAND: modulePath,
VAR_PAGE: page,
})
const stringifiedConfig = Buffer.from(
nextConfigBase64 || '',
'base64'
).toString()

return await loadEntrypoint(
'edge-app-route',
{
VAR_USERLAND: modulePath,
VAR_PAGE: page,
},
{
nextConfig: stringifiedConfig,
}
)
}

export default EdgeAppRouteLoader
19 changes: 11 additions & 8 deletions packages/next/src/server/web/edge-route-module-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ import { searchParamsToUrlQuery } from '../../shared/lib/router/utils/querystrin
import type { RequestLifecycleOpts } from '../base-server'
import { CloseController, trackStreamConsumed } from './web-on-close'
import { getEdgePreviewProps } from './get-edge-preview-props'
import type { NextConfigComplete } from '../config-shared'

type WrapOptions = Partial<Pick<AdapterOptions, 'page'>>
export interface WrapOptions {
nextConfig: NextConfigComplete
}

/**
* EdgeRouteModuleWrapper is a wrapper around a route module.
Expand All @@ -33,7 +36,10 @@ export class EdgeRouteModuleWrapper {
*
* @param routeModule the route module to wrap
*/
private constructor(private readonly routeModule: AppRouteRouteModule) {
private constructor(
private readonly routeModule: AppRouteRouteModule,
private readonly nextConfig: NextConfigComplete
) {
// TODO: (wyattjoh) possibly allow the module to define it's own matcher
this.matcher = new RouteMatcher(routeModule.definition)
}
Expand All @@ -47,18 +53,14 @@ export class EdgeRouteModuleWrapper {
* override the ones passed from the runtime
* @returns a function that can be used as a handler for the edge runtime
*/
public static wrap(
routeModule: AppRouteRouteModule,
options: WrapOptions = {}
) {
public static wrap(routeModule: AppRouteRouteModule, options: WrapOptions) {
// Create the module wrapper.
const wrapper = new EdgeRouteModuleWrapper(routeModule)
const wrapper = new EdgeRouteModuleWrapper(routeModule, options.nextConfig)

// Return the wrapping function.
return (opts: AdapterOptions) => {
return adapter({
...opts,
...options,
IncrementalCache,
// Bind the handler method to the wrapper so it still has context.
handler: wrapper.handler.bind(wrapper),
Expand Down Expand Up @@ -118,6 +120,7 @@ export class EdgeRouteModuleWrapper {
dynamicIO: !!process.env.__NEXT_DYNAMIC_IO,
},
buildId: '', // TODO: Populate this properly.
cacheLifeProfiles: this.nextConfig.experimental.cacheLife,
},
}

Expand Down

0 comments on commit 2804668

Please sign in to comment.