Some checks failed
Test examples / Test Examples (20) (push) Has been cancelled
Test examples / Test Examples (22) (push) Has been cancelled
Lock Threads / action (push) Has been cancelled
Trigger Release / start (push) Has been cancelled
Stale issue handler / stale (push) Has been cancelled
Update Font Data / create-pull-request (push) Has been cancelled
build-and-deploy / deploy-target (push) Has been cancelled
build-and-deploy / build (push) Has been cancelled
build-and-deploy / stable - aarch64-unknown-linux-musl - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-unknown-linux-musl - node@16 (push) Has been cancelled
build-and-deploy / stable - aarch64-unknown-linux-gnu - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-unknown-linux-gnu - node@16 (push) Has been cancelled
build-and-deploy / stable - aarch64-pc-windows-msvc - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-pc-windows-msvc - node@16 (push) Has been cancelled
build-and-deploy / stable - aarch64-apple-darwin - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-apple-darwin - node@16 (push) Has been cancelled
build-and-deploy / build-wasm (nodejs) (push) Has been cancelled
build-and-deploy / build-wasm (web) (push) Has been cancelled
build-and-deploy / Deploy preview tarball (push) Has been cancelled
build-and-deploy / Potentially publish release (push) Has been cancelled
build-and-deploy / publish-turbopack-npm-packages (push) Has been cancelled
build-and-deploy / Deploy examples (push) Has been cancelled
build-and-deploy / thank you, build (push) Has been cancelled
build-and-deploy / Upload Turbopack Bytesize metrics to Datadog (push) Has been cancelled
Rspack Next.js development integration tests / Rspack integration tests (push) Has been cancelled
Rspack Next.js production integration tests / Rspack integration tests (push) Has been cancelled
Turbopack Next.js development integration tests / Next.js integration tests (push) Has been cancelled
Turbopack Next.js production integration tests / Next.js integration tests (push) Has been cancelled
Update Rspack test manifest / Update and upload Rspack development test manifest (push) Has been cancelled
Update Rspack test manifest / Update and upload Rspack production test manifest (push) Has been cancelled
Upload bundler test manifests to areweturboyet.com / Upload test results (push) Has been cancelled
Update React / create-pull-request (push) Has been cancelled
test-e2e-project-reset-cron / reset-test-project (push) Has been cancelled
Notify about the top 15 issues/PRs/feature requests (most reacted) in the last 90 days / run (push) Has been cancelled
107 lines
2.6 KiB
JavaScript
107 lines
2.6 KiB
JavaScript
import { NextResponse, URLPattern } from 'next/server'
|
|
|
|
export async function middleware(request) {
|
|
const url = request.nextUrl
|
|
|
|
// this is needed for tests to get the BUILD_ID
|
|
if (url.pathname.startsWith('/_next/static/__BUILD_ID')) {
|
|
return NextResponse.next()
|
|
}
|
|
|
|
if (request.headers.get('x-prerender-revalidate')) {
|
|
return NextResponse.next({
|
|
headers: { 'x-middleware': 'hi' },
|
|
})
|
|
}
|
|
|
|
if (url.pathname === '/about/') {
|
|
return NextResponse.rewrite(new URL('/about/a', request.url))
|
|
}
|
|
|
|
if (url.pathname === '/ssr-page/') {
|
|
url.pathname = '/ssr-page-2'
|
|
return NextResponse.rewrite(url)
|
|
}
|
|
|
|
if (url.pathname === '/') {
|
|
url.pathname = '/ssg/first'
|
|
return NextResponse.rewrite(url)
|
|
}
|
|
|
|
if (url.pathname === '/to-ssg/') {
|
|
url.pathname = '/ssg/hello'
|
|
url.searchParams.set('from', 'middleware')
|
|
return NextResponse.rewrite(url)
|
|
}
|
|
|
|
if (url.pathname === '/sha/') {
|
|
url.pathname = '/shallow'
|
|
return NextResponse.rewrite(url)
|
|
}
|
|
|
|
if (url.pathname === '/rewrite-to-dynamic/') {
|
|
url.pathname = '/blog/from-middleware'
|
|
url.searchParams.set('some', 'middleware')
|
|
return NextResponse.rewrite(url)
|
|
}
|
|
|
|
if (url.pathname === '/rewrite-to-config-rewrite/') {
|
|
url.pathname = '/rewrite-3'
|
|
url.searchParams.set('some', 'middleware')
|
|
return NextResponse.rewrite(url)
|
|
}
|
|
|
|
if (url.pathname === '/redirect-to-somewhere/') {
|
|
url.pathname = '/somewhere'
|
|
return NextResponse.redirect(url, {
|
|
headers: {
|
|
'x-redirect-header': 'hi',
|
|
},
|
|
})
|
|
}
|
|
|
|
const original = new URL(request.url)
|
|
return NextResponse.next({
|
|
headers: {
|
|
'req-url-path': `${original.pathname}${original.search}`,
|
|
'req-url-basepath': request.nextUrl.basePath,
|
|
'req-url-pathname': request.nextUrl.pathname,
|
|
'req-url-query': request.nextUrl.searchParams.get('foo'),
|
|
'req-url-locale': request.nextUrl.locale,
|
|
'req-url-params':
|
|
url.pathname !== '/static' ? JSON.stringify(params(request.url)) : '{}',
|
|
},
|
|
})
|
|
}
|
|
|
|
const PATTERNS = [
|
|
[
|
|
new URLPattern({ pathname: '/:locale/:id' }),
|
|
({ pathname }) => ({
|
|
pathname: '/:locale/:id',
|
|
params: pathname.groups,
|
|
}),
|
|
],
|
|
[
|
|
new URLPattern({ pathname: '/:id' }),
|
|
({ pathname }) => ({
|
|
pathname: '/:id',
|
|
params: pathname.groups,
|
|
}),
|
|
],
|
|
]
|
|
|
|
const params = (url) => {
|
|
const input = url.split('?')[0]
|
|
let result = {}
|
|
|
|
for (const [pattern, handler] of PATTERNS) {
|
|
const patternResult = pattern.exec(input)
|
|
if (patternResult !== null && 'pathname' in patternResult) {
|
|
result = handler(patternResult)
|
|
break
|
|
}
|
|
}
|
|
return result
|
|
}
|