Files
vercel__next.js/test/e2e/middleware-base-path/app/middleware.js
JJ Kasper 607ff2b322 Update to process redirects/rewrites for _next/data with middleware (#37574)
* Update to process redirects/rewrites for _next/data

* correct matched-path resolving with middleware

* Add next-data header

* migrate middleware tests

* lint-fix

* update error case

* update test case

* Handle additional resolving cases and add more tests

* update test from merge

* fix test

* rm .only

* apply changes from review

* ensure _next/data resolving does not apply without middleware
2022-06-10 12:35:12 -05:00

25 lines
575 B
JavaScript

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)
}
if (url.pathname === '/redirect-with-basepath') {
url.pathname = '/about'
return NextResponse.rewrite(url)
}
}