|
| 1 | +import { NextResponse, URLPattern } from 'next/server' |
| 2 | + |
| 3 | +export async function middleware(request) { |
| 4 | + const url = request.nextUrl |
| 5 | + |
| 6 | + // this is needed for tests to get the BUILD_ID |
| 7 | + if (url.pathname.startsWith('/_next/static/__BUILD_ID')) { |
| 8 | + return NextResponse.next() |
| 9 | + } |
| 10 | + |
| 11 | + if (request.headers.get('x-prerender-revalidate')) { |
| 12 | + return NextResponse.next({ |
| 13 | + headers: { 'x-middleware': 'hi' }, |
| 14 | + }) |
| 15 | + } |
| 16 | + |
| 17 | + if (url.pathname === '/about/') { |
| 18 | + return NextResponse.rewrite(new URL('/about/a', request.url)) |
| 19 | + } |
| 20 | + |
| 21 | + if (url.pathname === '/ssr-page/') { |
| 22 | + url.pathname = '/ssr-page-2' |
| 23 | + return NextResponse.rewrite(url) |
| 24 | + } |
| 25 | + |
| 26 | + if (url.pathname === '/') { |
| 27 | + url.pathname = '/ssg/first' |
| 28 | + return NextResponse.rewrite(url) |
| 29 | + } |
| 30 | + |
| 31 | + if (url.pathname === '/to-ssg/') { |
| 32 | + url.pathname = '/ssg/hello' |
| 33 | + url.searchParams.set('from', 'middleware') |
| 34 | + return NextResponse.rewrite(url) |
| 35 | + } |
| 36 | + |
| 37 | + if (url.pathname === '/sha/') { |
| 38 | + url.pathname = '/shallow' |
| 39 | + return NextResponse.rewrite(url) |
| 40 | + } |
| 41 | + |
| 42 | + if (url.pathname === '/rewrite-to-dynamic/') { |
| 43 | + url.pathname = '/blog/from-middleware' |
| 44 | + url.searchParams.set('some', 'middleware') |
| 45 | + return NextResponse.rewrite(url) |
| 46 | + } |
| 47 | + |
| 48 | + if (url.pathname === '/rewrite-to-config-rewrite/') { |
| 49 | + url.pathname = '/rewrite-3' |
| 50 | + url.searchParams.set('some', 'middleware') |
| 51 | + return NextResponse.rewrite(url) |
| 52 | + } |
| 53 | + |
| 54 | + if (url.pathname === '/redirect-to-somewhere/') { |
| 55 | + url.pathname = '/somewhere' |
| 56 | + return NextResponse.redirect(url, { |
| 57 | + headers: { |
| 58 | + 'x-redirect-header': 'hi', |
| 59 | + }, |
| 60 | + }) |
| 61 | + } |
| 62 | + |
| 63 | + const original = new URL(request.url) |
| 64 | + return NextResponse.next({ |
| 65 | + headers: { |
| 66 | + 'req-url-path': `${original.pathname}${original.search}`, |
| 67 | + 'req-url-basepath': request.nextUrl.basePath, |
| 68 | + 'req-url-pathname': request.nextUrl.pathname, |
| 69 | + 'req-url-query': request.nextUrl.searchParams.get('foo'), |
| 70 | + 'req-url-locale': request.nextUrl.locale, |
| 71 | + 'req-url-params': |
| 72 | + url.pathname !== '/static' ? JSON.stringify(params(request.url)) : '{}', |
| 73 | + }, |
| 74 | + }) |
| 75 | +} |
| 76 | + |
| 77 | +const PATTERNS = [ |
| 78 | + [ |
| 79 | + new URLPattern({ pathname: '/:locale/:id' }), |
| 80 | + ({ pathname }) => ({ |
| 81 | + pathname: '/:locale/:id', |
| 82 | + params: pathname.groups, |
| 83 | + }), |
| 84 | + ], |
| 85 | + [ |
| 86 | + new URLPattern({ pathname: '/:id' }), |
| 87 | + ({ pathname }) => ({ |
| 88 | + pathname: '/:id', |
| 89 | + params: pathname.groups, |
| 90 | + }), |
| 91 | + ], |
| 92 | +] |
| 93 | + |
| 94 | +const params = (url) => { |
| 95 | + const input = url.split('?')[0] |
| 96 | + let result = {} |
| 97 | + |
| 98 | + for (const [pattern, handler] of PATTERNS) { |
| 99 | + const patternResult = pattern.exec(input) |
| 100 | + if (patternResult !== null && 'pathname' in patternResult) { |
| 101 | + result = handler(patternResult) |
| 102 | + break |
| 103 | + } |
| 104 | + } |
| 105 | + return result |
| 106 | +} |
0 commit comments