import { NextResponse } from 'next/server';
export function middleware(request: Request) {
// Store current request url in a custom header, which you can read later
const requestHeaders = new Headers(request.headers);
requestHeaders.set('x-url', request.url);
return NextResponse.next({
request: {
// Apply new request headers
headers: requestHeaders,
}
});
}
然后从根布局内部使用它:
import { headers } from 'next/headers';
export default function RootLayout() {
const headersList = headers();
// read the custom x-url header
const header_url = headersList.get('x-url') || "";
}
1条答案
按热度按时间waxmsbnn1#
看看https://github.com/vercel/next.js/issues/43704,pauliusuza解决了这个问题
您可以使用中间件来解决此问题
然后从根布局内部使用它:
希望能有所帮助