diff --git a/packages/next/shared/lib/router/router.ts b/packages/next/shared/lib/router/router.ts index 653ae572d0af221..cc57524783a0ff6 100644 --- a/packages/next/shared/lib/router/router.ts +++ b/packages/next/shared/lib/router/router.ts @@ -1829,7 +1829,9 @@ export default class Router implements BaseRouter { } const preflight = await this._getPreflightData({ - preflightHref: options.as, + preflightHref: hasBasePath(options.as) + ? options.as + : addBasePath(options.as), shouldCache: options.cache, }) diff --git a/test/integration/middleware/with-base-path/pages/_middleware.js b/test/integration/middleware/with-base-path/pages/_middleware.js index a4614ceeb547a7e..2e1bcab993a024b 100644 --- a/test/integration/middleware/with-base-path/pages/_middleware.js +++ b/test/integration/middleware/with-base-path/pages/_middleware.js @@ -2,6 +2,16 @@ import { NextResponse } from 'next/server' export async function middleware(request) { const url = request.nextUrl + + if ( + request.method === 'HEAD' && + url.basePath === '/root' && + url.pathname === '/redirect-me-to-about' + ) { + url.pathname = '/about' + return NextResponse.redirect(url) + } + if (url.pathname === '/redirect-with-basepath' && !url.basePath) { url.basePath = '/root' return NextResponse.redirect(url) diff --git a/test/integration/middleware/with-base-path/pages/index.js b/test/integration/middleware/with-base-path/pages/index.js index 9d255950aa94f63..bc2fa5dd30667ed 100644 --- a/test/integration/middleware/with-base-path/pages/index.js +++ b/test/integration/middleware/with-base-path/pages/index.js @@ -22,6 +22,11 @@ export default function Main({ message }) { Rewrite me to Vercel +
  • + + redirect me to about + +
  • ) diff --git a/test/integration/middleware/with-base-path/test/index.test.js b/test/integration/middleware/with-base-path/test/index.test.js index f76bf60a7421aa6..9e0b9d193b7662d 100644 --- a/test/integration/middleware/with-base-path/test/index.test.js +++ b/test/integration/middleware/with-base-path/test/index.test.js @@ -2,7 +2,14 @@ jest.setTimeout(1000 * 60 * 2) -import { fetchViaHTTP, findPort, killApp, launchApp } from 'next-test-utils' +import { + fetchViaHTTP, + findPort, + killApp, + launchApp, + nextBuild, + nextStart, +} from 'next-test-utils' import { join } from 'path' import cheerio from 'cheerio' import webdriver from 'next-webdriver' @@ -11,12 +18,25 @@ const context = {} context.appDir = join(__dirname, '../') describe('Middleware base tests', () => { - beforeAll(async () => { - context.appPort = await findPort() - context.app = await launchApp(context.appDir, context.appPort) + describe('dev mode', () => { + beforeAll(async () => { + context.appPort = await findPort() + context.app = await launchApp(context.appDir, context.appPort) + }) + afterAll(() => killApp(context.app)) + runTests() + }) + + describe('production mode', () => { + beforeAll(async () => { + await nextBuild(context.appDir) + context.appPort = await findPort() + context.app = await nextStart(context.appDir, context.appPort) + }) + afterAll(() => killApp(context.app)) + runTests() + runPreflightTests() }) - afterAll(() => killApp(context.app)) - runTests() }) function runTests() { @@ -39,3 +59,21 @@ function runTests() { expect($('.title').text()).toBe('About Page') }) } + +function runPreflightTests() { + it('should redirect via preflight middleware request', async () => { + const browser = await webdriver(context.appPort, '/root') + + try { + await browser.waitForCondition( + 'next.router && Object.keys(next.router.sde).length == 4' + ) + const redirect = await browser.eval( + `next.router.sde["http://localhost:${context.appPort}/root/redirect-me-to-about"].redirect` + ) + expect(redirect).toBe('/root/about') + } finally { + await browser.close() + } + }) +}