Skip to content

Commit

Permalink
Support basePath with edge runtime for Custom App Routes (#52910)
Browse files Browse the repository at this point in the history
Fixes a bug where the edge runtime didn't support `basePath` with Custom App Routes.

- Fixes #49661
  • Loading branch information
wyattjoh committed Jul 19, 2023
1 parent 20b115e commit 3e821ef
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 48 deletions.
14 changes: 11 additions & 3 deletions packages/next/src/server/web/edge-route-module-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import type { RouteModule } from '../future/route-modules/route-module'
import type { NextRequest } from './spec-extension/request'

import './globals'

import { adapter, type AdapterOptions } from './adapter'
import { IncrementalCache } from '../lib/incremental-cache'

import { removeTrailingSlash } from '../../shared/lib/router/utils/remove-trailing-slash'
import { RouteMatcher } from '../future/route-matchers/route-matcher'
import { removeTrailingSlash } from '../../shared/lib/router/utils/remove-trailing-slash'
import { removePathPrefix } from '../../shared/lib/router/utils/remove-path-prefix'

type WrapOptions = Partial<Pick<AdapterOptions, 'page'>>

Expand Down Expand Up @@ -64,7 +65,14 @@ export class EdgeRouteModuleWrapper {
private async handler(request: NextRequest): Promise<Response> {
// Get the pathname for the matcher. Pathnames should not have trailing
// slashes for matching.
const pathname = removeTrailingSlash(new URL(request.url).pathname)
let pathname = removeTrailingSlash(new URL(request.url).pathname)

// Get the base path and strip it from the pathname if it exists.
const { basePath } = request.nextUrl
if (basePath) {
// If the path prefix doesn't exist, then this will do nothing.
pathname = removePathPrefix(pathname, basePath)
}

// Get the match for this request.
const match = this.matcher.match(pathname)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
process.env.BASE_PATH = '/docs'
require('./app-custom-routes.test')

0 comments on commit 3e821ef

Please sign in to comment.