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

Add res.redirect response helper #14705

Merged
merged 22 commits into from
Jul 7, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
cad0d12
Add `res.redirect` response helper
botv Jun 30, 2020
88cdad2
Merge branch 'canary' into response-helpers
botv Jun 30, 2020
514d6bf
Revert unnecessary changes
botv Jun 30, 2020
dff7b14
Match Express signature for `res.redirect`
botv Jun 30, 2020
001dcff
Merge branch 'response-helpers' of https://github.com/botv/next.js in…
botv Jun 30, 2020
a741d04
Add documentation for `res.redirect`
botv Jun 30, 2020
c7b82a7
Update packages/next/next-server/lib/utils.ts
botv Jul 1, 2020
f256bb4
Update packages/next/next-server/server/api-utils.ts
botv Jul 1, 2020
c43e2cd
Update packages/next/next-server/server/api-utils.ts
botv Jul 1, 2020
5ffe4f7
Update packages/next/next-server/server/api-utils.ts
botv Jul 1, 2020
3849a25
Fix errors from renamed parameter
botv Jul 1, 2020
2b4f62e
Add tests
botv Jul 1, 2020
a227119
Merge branch 'canary' into response-helpers
botv Jul 1, 2020
58c54fc
Change status code to 307
botv Jul 3, 2020
35d96f8
Merge branch 'response-helpers' of https://github.com/botv/next.js in…
botv Jul 3, 2020
540c229
Update examples with new redirect helper
botv Jul 3, 2020
76d702c
Update docs with new redirect helper
botv Jul 3, 2020
b0cc648
Merge branch 'canary' into response-helpers
botv Jul 3, 2020
6fb56ba
Merge branch 'canary' into response-helpers
botv Jul 5, 2020
babea12
Undo examples updates
botv Jul 5, 2020
7350832
Merge branch 'response-helpers' of https://github.com/botv/next.js in…
botv Jul 5, 2020
73c7fb7
Merge branch 'canary' into response-helpers
kodiakhq[bot] Jul 7, 2020
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
1 change: 1 addition & 0 deletions packages/next/next-server/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ export type NextApiResponse<T = any> = ServerResponse & {
*/
json: Send<T>
status: (statusCode: number) => NextApiResponse<T>
redirect: (status: string | number, url: string) => NextApiResponse<T>
botv marked this conversation as resolved.
Show resolved Hide resolved

/**
* Set preview data for Next.js' prerender mode
Expand Down
22 changes: 22 additions & 0 deletions packages/next/next-server/server/api-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export async function apiResolver(
apiRes.status = (statusCode) => sendStatusCode(apiRes, statusCode)
apiRes.send = (data) => sendData(apiReq, apiRes, data)
apiRes.json = (data) => sendJson(apiRes, data)
apiRes.redirect = (status, url) => redirect(apiRes, status, url)
botv marked this conversation as resolved.
Show resolved Hide resolved
apiRes.setPreviewData = (data, options = {}) =>
setPreviewData(apiRes, data, Object.assign({}, apiContext, options))
apiRes.clearPreviewData = () => clearPreviewData(apiRes)
Expand Down Expand Up @@ -209,6 +210,27 @@ export function sendStatusCode(
return res
}

/**
*
* @param res response object
* @param [status] `HTTP` status code of redirect
* @param url URL of redirect
*/
export function redirect(
res: NextApiResponse,
status: string | number, // If this is a number it's the URL
botv marked this conversation as resolved.
Show resolved Hide resolved
url: string
botv marked this conversation as resolved.
Show resolved Hide resolved
): NextApiResponse<any> {
if (typeof status === 'string') {
url = status
status = 302
}

res.writeHead(status, { Location: url })
res.end()
return res
}

function sendEtagResponse(
req: NextApiRequest,
res: NextApiResponse,
Expand Down