| 131 | // Only the post page is mirrored — every mirrored route doubles its ISR |
| 132 | // entries and its revalidation traffic. |
| 133 | export const getLayoutVariantResponse = ( |
| 134 | req: NextRequest, |
| 135 | ): NextResponse | undefined => { |
| 136 | const { pathname } = req.nextUrl; |
| 137 | |
| 138 | // Next normalises `/_next/data/<buildId>/posts/x.json` down to the page |
| 139 | // path, so a soft navigation reaches here looking exactly like a document |
| 140 | // request. Rewriting it answers with the mirror's route, which moves the |
| 141 | // client onto a route it never asked for; the shell is already resolved by |
| 142 | // then, so only the initial document needs the mirror. |
| 143 | if ( |
| 144 | req.headers.has('x-nextjs-data') || |
| 145 | req.cookies.get(LAYOUT_VARIANT_COOKIE)?.value !== 'v2' || |
| 146 | !isPostPermalinkPath(pathname) |
| 147 | ) { |
| 148 | return undefined; |
| 149 | } |
| 150 | |
| 151 | const url = req.nextUrl.clone(); |
| 152 | url.pathname = `${LAYOUT_VARIANT_ROUTE_PREFIX}${pathname}`; |
| 153 | |
| 154 | // Names the shell the response was built with, so the mirror can be told |
| 155 | // apart from the plain page in logs and when debugging a cached page. |
| 156 | return NextResponse.rewrite(url, { |
| 157 | headers: { 'x-layout-variant': 'v2' }, |
| 158 | }); |
| 159 | }; |
| 160 | |
| 161 | export async function proxy(req: NextRequest): Promise<NextResponse> { |
| 162 | if (isMarkdownRequest(req)) { |