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 3 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
2 changes: 1 addition & 1 deletion packages/next/next-server/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ export type NextApiResponse<T = any> = ServerResponse & {
*/
json: Send<T>
status: (statusCode: number) => NextApiResponse<T>
redirect: (url: string, 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
16 changes: 11 additions & 5 deletions packages/next/next-server/server/api-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +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 = (url, statusCode) => redirect(apiRes, url, statusCode)
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 @@ -213,15 +213,21 @@ export function sendStatusCode(
/**
*
* @param res response object
* @param [status] `HTTP` status code of redirect
* @param url URL of redirect
* @param [statusCode] `HTTP` status code of redirect
*/
export function redirect(
res: NextApiResponse,
url: string,
statusCode?: number
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> {
res.writeHead(statusCode || 302, { Location: url }).end()
if (typeof status === 'string') {
url = status
status = 302
}

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

Expand Down
1 change: 0 additions & 1 deletion test/integration/prerender/next.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
module.exports = {
target: 'serverless',
rewrites() {
return [
{
Expand Down
2 changes: 1 addition & 1 deletion test/integration/prerender/pages/blog/[post]/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react'
import Link from 'next/link'
import { useRouter } from 'next/router'
// import 'firebase/firestore'
import 'firebase/firestore'

export async function getStaticPaths() {
return {
Expand Down