Skip to content

Commit

Permalink
Handle basePath app-dir minimal case (vercel#53189)
Browse files Browse the repository at this point in the history
  • Loading branch information
ijjk authored and Strift committed Jul 27, 2023
1 parent 8dc7bc1 commit b7209b0
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions packages/next/src/server/base-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -720,11 +720,11 @@ export default abstract class Server<ServerOptions extends Options = Options> {
addRequestMeta(req, '_nextHadBasePath', true)
}

const useMatchedPathHeader =
this.minimalMode && typeof req.headers['x-matched-path'] === 'string'

// TODO: merge handling with x-invoke-path
if (
this.minimalMode &&
typeof req.headers['x-matched-path'] === 'string'
) {
if (useMatchedPathHeader) {
try {
if (this.hasAppDir) {
// ensure /index path is normalized for prerender
Expand All @@ -738,7 +738,8 @@ export default abstract class Server<ServerOptions extends Options = Options> {
// x-matched-path is the source of truth, it tells what page
// should be rendered because we don't process rewrites in minimalMode
let matchedPath = normalizeRscPath(
new URL(req.headers['x-matched-path'], 'http://localhost').pathname,
new URL(req.headers['x-matched-path'] as string, 'http://localhost')
.pathname,
this.hasAppDir
)

Expand Down Expand Up @@ -1023,14 +1024,14 @@ export default abstract class Server<ServerOptions extends Options = Options> {
// when x-invoke-path is specified we can short short circuit resolving
// we only honor this header if we are inside of a render worker to
// prevent external users coercing the routing path
const matchedPath = req.headers['x-invoke-path'] as string

if (
!(req.headers['x-matched-path'] && this.minimalMode) &&
const invokePath = req.headers['x-invoke-path'] as string
const useInvokePath =
!useMatchedPathHeader &&
process.env.NEXT_RUNTIME !== 'edge' &&
process.env.__NEXT_PRIVATE_RENDER_WORKER &&
matchedPath
) {
invokePath

if (useInvokePath) {
if (req.headers['x-invoke-status']) {
const invokeQuery = req.headers['x-invoke-query']

Expand All @@ -1054,7 +1055,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
return this.renderError(err, req, res, '/_error', parsedUrl.query)
}

const parsedMatchedPath = new URL(matchedPath || '/', 'http://n')
const parsedMatchedPath = new URL(invokePath || '/', 'http://n')
const invokePathnameInfo = getNextPathnameInfo(
parsedMatchedPath.pathname,
{
Expand Down Expand Up @@ -1139,6 +1140,14 @@ export default abstract class Server<ServerOptions extends Options = Options> {
return
}

// ensure we strip the basePath when not using an invoke header
if (!(useMatchedPathHeader || useInvokePath) && pathnameInfo.basePath) {
parsedUrl.pathname = removePathPrefix(
parsedUrl.pathname,
pathnameInfo.basePath
)
}

res.statusCode = 200
return await this.run(req, res, parsedUrl)
} catch (err: any) {
Expand Down

0 comments on commit b7209b0

Please sign in to comment.