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
202 lines
5.2 KiB
JavaScript
202 lines
5.2 KiB
JavaScript
import { NextResponse } from 'next/server'
|
|
|
|
const PUBLIC_FILE = /\.(.*)$/
|
|
|
|
/**
|
|
* @param {import('next/server').NextRequest} request
|
|
*/
|
|
export async function middleware(request) {
|
|
const url = request.nextUrl
|
|
|
|
if (url.pathname.includes('article')) {
|
|
return NextResponse.next()
|
|
}
|
|
|
|
// this is needed for tests to get the BUILD_ID
|
|
if (url.pathname.startsWith('/_next/static/__BUILD_ID')) {
|
|
return NextResponse.next()
|
|
}
|
|
|
|
if (url.pathname.includes('/to/some/404/path')) {
|
|
return NextResponse.next({
|
|
'x-matched-path': '/404',
|
|
})
|
|
}
|
|
|
|
if (
|
|
url.pathname.includes(
|
|
'/middleware-external-rewrite-body-headers-return-body'
|
|
)
|
|
) {
|
|
const tmpHeaders = new Headers(request.headers)
|
|
|
|
tmpHeaders.set('x-hello-from-middleware1', 'hello')
|
|
|
|
return NextResponse.rewrite(
|
|
'https://next-data-api-endpoint.vercel.app/api/echo-body',
|
|
{
|
|
request: {
|
|
headers: tmpHeaders,
|
|
},
|
|
}
|
|
)
|
|
}
|
|
|
|
if (
|
|
url.pathname.includes(
|
|
'/middleware-external-rewrite-body-headers-return-headers'
|
|
)
|
|
) {
|
|
const tmpHeaders = new Headers(request.headers)
|
|
|
|
tmpHeaders.set('x-hello-from-middleware1', 'hello')
|
|
|
|
return NextResponse.rewrite(
|
|
'https://next-data-api-endpoint.vercel.app/api/echo-headers',
|
|
{
|
|
request: {
|
|
headers: tmpHeaders,
|
|
},
|
|
}
|
|
)
|
|
}
|
|
|
|
if (url.pathname.includes('/middleware-external-rewrite-body')) {
|
|
return NextResponse.rewrite(
|
|
'https://next-data-api-endpoint.vercel.app/api/echo-body'
|
|
)
|
|
}
|
|
|
|
if (url.pathname.includes('/rewrite-to-static')) {
|
|
request.nextUrl.pathname = '/static-ssg/post-1'
|
|
return NextResponse.rewrite(request.nextUrl)
|
|
}
|
|
|
|
if (url.pathname.includes('/fallback-true-blog/rewritten')) {
|
|
request.nextUrl.pathname = '/about'
|
|
return NextResponse.rewrite(request.nextUrl)
|
|
}
|
|
|
|
if (url.pathname.startsWith('/about') && url.searchParams.has('override')) {
|
|
const isExternal = url.searchParams.get('override') === 'external'
|
|
return NextResponse.rewrite(
|
|
isExternal
|
|
? 'https://example.vercel.sh'
|
|
: new URL('/ab-test/a', request.url)
|
|
)
|
|
}
|
|
|
|
if (url.pathname === '/rewrite-to-beforefiles-rewrite') {
|
|
url.pathname = '/beforefiles-rewrite'
|
|
return NextResponse.rewrite(url)
|
|
}
|
|
|
|
if (url.pathname === '/rewrite-to-afterfiles-rewrite') {
|
|
url.pathname = '/afterfiles-rewrite'
|
|
return NextResponse.rewrite(url)
|
|
}
|
|
|
|
if (url.pathname.startsWith('/to-blog')) {
|
|
const slug = url.pathname.split('/').pop()
|
|
url.pathname = `/fallback-true-blog/${slug}`
|
|
return NextResponse.rewrite(url)
|
|
}
|
|
|
|
if (url.pathname === '/rewrite-to-ab-test') {
|
|
let bucket = request.cookies.get('bucket')
|
|
if (!bucket) {
|
|
bucket = Math.random() >= 0.5 ? 'a' : 'b'
|
|
url.pathname = `/ab-test/${bucket}`
|
|
const response = NextResponse.rewrite(url)
|
|
response.cookies.set('bucket', bucket, { maxAge: 10 })
|
|
return response
|
|
}
|
|
|
|
url.pathname = `/${bucket}`
|
|
return NextResponse.rewrite(url)
|
|
}
|
|
|
|
if (url.pathname === '/rewrite-me-to-about') {
|
|
url.pathname = '/about'
|
|
return NextResponse.rewrite(url, {
|
|
headers: { 'x-rewrite-target': String(url) },
|
|
})
|
|
}
|
|
|
|
if (url.pathname === '/rewrite-me-with-a-colon') {
|
|
url.pathname = '/with:colon'
|
|
return NextResponse.rewrite(url)
|
|
}
|
|
|
|
if (url.pathname === '/colon:here') {
|
|
url.pathname = '/no-colon-here'
|
|
return NextResponse.rewrite(url)
|
|
}
|
|
|
|
if (url.pathname === '/rewrite-me-to-vercel') {
|
|
return NextResponse.rewrite('https://example.vercel.sh')
|
|
}
|
|
|
|
if (url.pathname === '/clear-query-params') {
|
|
const allowedKeys = ['allowed']
|
|
for (const key of [...url.searchParams.keys()]) {
|
|
if (!allowedKeys.includes(key)) {
|
|
url.searchParams.delete(key)
|
|
}
|
|
}
|
|
return NextResponse.rewrite(url)
|
|
}
|
|
|
|
if (url.pathname === '/dynamic-no-cache/1') {
|
|
const rewriteUrl =
|
|
request.headers.get('purpose') === 'prefetch'
|
|
? '/dynamic-no-cache/1'
|
|
: '/dynamic-no-cache/2'
|
|
|
|
url.pathname = rewriteUrl
|
|
|
|
return NextResponse.rewrite(url, {
|
|
headers: { 'x-middleware-cache': 'no-cache' },
|
|
})
|
|
}
|
|
|
|
if (
|
|
url.pathname === '/rewrite-me-without-hard-navigation' ||
|
|
url.searchParams.get('path') === 'rewrite-me-without-hard-navigation'
|
|
) {
|
|
url.searchParams.set('middleware', 'foo')
|
|
url.pathname = request.cookies.has('about-bypass')
|
|
? '/about-bypass'
|
|
: '/about'
|
|
|
|
return NextResponse.rewrite(url, {
|
|
headers: { 'x-middleware-cache': 'no-cache' },
|
|
})
|
|
}
|
|
|
|
if (url.pathname.endsWith('/dynamic-replace')) {
|
|
url.pathname = '/dynamic-fallback/catch-all'
|
|
return NextResponse.rewrite(url)
|
|
}
|
|
|
|
if (url.pathname.startsWith('/country')) {
|
|
const locale = url.searchParams.get('my-locale')
|
|
if (locale) {
|
|
url.locale = locale
|
|
}
|
|
|
|
const country = url.searchParams.get('country') || 'us'
|
|
if (!PUBLIC_FILE.test(url.pathname) && !url.pathname.includes('/api/')) {
|
|
url.pathname = `/country/${country}`
|
|
return NextResponse.rewrite(url)
|
|
}
|
|
}
|
|
|
|
if (url.pathname.startsWith('/i18n')) {
|
|
url.searchParams.set('locale', url.locale)
|
|
return NextResponse.rewrite(url)
|
|
}
|
|
|
|
return NextResponse.rewrite(request.nextUrl)
|
|
}
|