Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: fix usage in middleware errors #37364

Merged
merged 2 commits into from
Jun 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/advanced-features/middleware.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export function middleware(req: NextRequest) {
if (areCredentialsValid(req.headers.get('authorization')) {
return NextResponse.next()
}
return NextResponse.redirect(`/login?from=${req.nextUrl.pathname}`)
return NextResponse.redirect(new URL(`/login?from=${req.nextUrl.pathname}`, req.url))
}
```

Expand Down
8 changes: 5 additions & 3 deletions errors/returning-response-body-in-middleware.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

Your [`middleware`](https://nextjs.org/docs/advanced-features/middleware) function returns a response body, which is not supported.

Letting middleware respond to incoming requests would bypass Next.js routing mechanism, creating an unecessary escape hatch.
Letting middleware respond to incoming requests would bypass Next.js routing mechanism, creating an unnecessary escape hatch.

#### Possible Ways to Fix It

Expand Down Expand Up @@ -51,7 +51,9 @@ It is intended for use cases like:
}
}

return NextResponse.redirect(`/login?from=${req.nextUrl.pathname}`)
return NextResponse.redirect(
new URL(`/login?from=${req.nextUrl.pathname}`, req.url)
)
}
```

Expand All @@ -78,7 +80,7 @@ It is intended for use cases like:
headers: { 'response-greetings': 'Hej!' },
})
// configures cookies
response.cookies.set('hello', 'world')
res.cookies.set('hello', 'world')
return res
}
```