aa3e043bbf
Adding a `_rsc` query for RSC payload requests so that they can be differentiated on resources level for CDN cache for the ones that didn't fully respect to VARY header. Also stripped them for node/edge servers so that they won't show up in the url x-ref: https://github.com/vercel/next.js/pull/49140#issuecomment-1549405009 Closes #49140 Closes NEXT-1268
25 lines
781 B
JavaScript
25 lines
781 B
JavaScript
// @ts-check
|
|
import { NextResponse } from 'next/server'
|
|
// @ts-ignore
|
|
import { NEXT_RSC_UNION_QUERY } from 'next/dist/client/components/app-router-headers'
|
|
|
|
if (NEXT_RSC_UNION_QUERY !== '_rsc') {
|
|
throw new Error(`NEXT_RSC_UNION_QUERY should be _rsc`)
|
|
}
|
|
|
|
/**
|
|
* @param {import('next/server').NextRequest} request
|
|
* @returns {NextResponse | undefined}
|
|
*/
|
|
export function middleware(request) {
|
|
const rscQuery = request.nextUrl.searchParams.get(NEXT_RSC_UNION_QUERY)
|
|
|
|
// Test that the RSC query is not present in the middleware
|
|
if (rscQuery) {
|
|
throw new Error('RSC query should not be present in the middleware')
|
|
}
|
|
|
|
if (request.nextUrl.pathname === '/redirect-middleware-to-dashboard') {
|
|
return NextResponse.redirect(new URL('/redirect-dest', request.url))
|
|
}
|
|
}
|