Skip to content

Commit

Permalink
feat: add standalone mode support
Browse files Browse the repository at this point in the history
  • Loading branch information
wyattjoh committed May 14, 2024
1 parent 55edc3f commit 4ab5046
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
23 changes: 22 additions & 1 deletion packages/next/src/server/base-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ import {
} from './request-adapter/x-invoke-path-request-adapter'
import { BaseRequestAdapter } from './request-adapter/base-request-adapter'
import { BubbledError, isBubbledError } from './lib/trace/bubble-error'
import { StandaloneRequestAdapter } from './request-adapter/standalone-request-adapter'

export type FindComponentsResult = {
components: LoadComponentsReturnType
Expand Down Expand Up @@ -550,7 +551,27 @@ export default abstract class Server<
this.responseCache = this.getResponseCache({ dev })

// Setup the request adapter.
if (this.minimalMode && process.env.NEXT_RUNTIME !== 'edge') {
if (
this.minimalMode &&
process.env.NEXT_RUNTIME !== 'edge' &&
this.nextConfig.output === 'standalone'
) {
this.requestAdapter = new StandaloneRequestAdapter(
new XMatchedPathRequestAdapter(
this.buildId,
this.enabledDirectories,
this.i18nProvider,
this.matchers,
this.nextConfig,
this.getRoutesManifest.bind(this)
),
new XInvokePathRequestAdapter(
this.enabledDirectories,
this.i18nProvider,
this.nextConfig
)
)
} else if (this.minimalMode && process.env.NEXT_RUNTIME !== 'edge') {
this.requestAdapter = new XMatchedPathRequestAdapter(
this.buildId,
this.enabledDirectories,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import type { BaseNextRequest } from '../base-http'
import type { NextUrlWithParsedQuery } from '../request-meta'
import type { RequestAdapter } from './request-adapter'
import type { XInvokePathRequestAdapter } from './x-invoke-path-request-adapter'
import type { XMatchedPathRequestAdapter } from './x-matched-path-request-adapter'

export class StandaloneRequestAdapter<ServerRequest extends BaseNextRequest>
implements RequestAdapter<ServerRequest>
{
constructor(
private readonly xMatchedPathRequestAdapter: XMatchedPathRequestAdapter<ServerRequest>,
private readonly xInvokePathRequestAdapter: XInvokePathRequestAdapter<ServerRequest>
) {}

public async adapt(
req: ServerRequest,
parsedURL: NextUrlWithParsedQuery
): Promise<void> {
// Today, standalone mode is used to test the x-matched-path support as
// well.

// FIXME: remove this fallback when the tests are updated.
if (
req.headers['x-matched-path'] &&
typeof req.headers['x-matched-path'] === 'string'
) {
return this.xMatchedPathRequestAdapter.adapt(req, parsedURL)
}

return this.xInvokePathRequestAdapter.adapt(req, parsedURL)
}
}

0 comments on commit 4ab5046

Please sign in to comment.