admin管理员组

文章数量:1287817

I tried to access the request.headers in the Next.js Middleware but the data is not showing up.

If I access something else, the data can appear. If I display headers it gets an error:

Server Error
TypeError: Cannot delete property 'Symbol(set-cookie)' of #<HeadersList2>

Code in Middleware:

export function middleware(request) {
    console.log(request.credentials); // show -> same-origin
    console.log(request.method); // show -> GET

    console.log(request.headers); // page error
    console.log(request.headers.referer); // not show / undefined
}

export const config = {
    matcher: "/:path*/generate",
};

Fill in the data in the request variable:

Error when I display request.headers:

I tried to access the request.headers in the Next.js Middleware but the data is not showing up.

If I access something else, the data can appear. If I display headers it gets an error:

Server Error
TypeError: Cannot delete property 'Symbol(set-cookie)' of #<HeadersList2>

Code in Middleware:

export function middleware(request) {
    console.log(request.credentials); // show -> same-origin
    console.log(request.method); // show -> GET

    console.log(request.headers); // page error
    console.log(request.headers.referer); // not show / undefined
}

export const config = {
    matcher: "/:path*/generate",
};

Fill in the data in the request variable:

Error when I display request.headers:

Share Improve this question edited Aug 20, 2022 at 23:47 juliomalves 50.4k23 gold badges177 silver badges168 bronze badges asked Aug 20, 2022 at 5:38 Kuli UploaderKuli Uploader 211 silver badge4 bronze badges 1
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Bot Commented Aug 20, 2022 at 8:02
Add a ment  | 

1 Answer 1

Reset to default 11

You can't access the headers object directly inside the middleware. If you want to access a specific header you should use the get method.

export function middleware(request) {
    console.log(request.headers.get('referer')); // Will output `referer` header value
    
    return NextResponse.next();
}

本文标签: javascriptError accessing requestheaders in Nextjs middlewareStack Overflow