Skip to content

Commit f9f7714

Browse files
fl0wkevinpeno
andauthored
response.redirect: extract special-cased back, fixes #904 (#1115)
The simple solution to drop special-cased `'back'` in `response.redirect`. I can't label but should be labeled as version-major. This PR **does not** make use of Symbol as purposed in #904. **Edit** If this solution is acceptable, a deprecation should be added to Koa 2 on `'back'` use. --------- Co-authored-by: Kevin Peno <[email protected]>
1 parent ff0f3b6 commit f9f7714

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

__tests__/response/back.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict'
2+
3+
const assert = require('assert')
4+
const context = require('../../test-helpers/context')
5+
6+
describe('ctx.back([alt])', () => {
7+
it('should redirect to Referrer', () => {
8+
const ctx = context()
9+
ctx.req.headers.referrer = '/login'
10+
ctx.back()
11+
assert.equal(ctx.response.header.location, '/login')
12+
})
13+
14+
it('should redirect to Referer', () => {
15+
const ctx = context()
16+
ctx.req.headers.referer = '/login'
17+
ctx.back()
18+
assert.equal(ctx.response.header.location, '/login')
19+
})
20+
21+
it('should default to alt', () => {
22+
const ctx = context()
23+
ctx.back('/index.html')
24+
assert.equal(ctx.response.header.location, '/index.html')
25+
})
26+
27+
it('should default redirect to /', () => {
28+
const ctx = context()
29+
ctx.back()
30+
assert.equal(ctx.response.header.location, '/')
31+
})
32+
})

lib/context.js

+1
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ delegate(proto, 'response')
204204
.method('set')
205205
.method('append')
206206
.method('flushHeaders')
207+
.method('back')
207208
.access('status')
208209
.access('message')
209210
.access('body')

lib/response.js

+24-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const only = require('./only.js')
1818
const util = require('util')
1919
const encodeUrl = require('encodeurl')
2020
const Stream = require('stream')
21+
const deprecate = require('depd')('koa')
2122

2223
/**
2324
* Prototype.
@@ -293,7 +294,11 @@ module.exports = {
293294

294295
redirect (url, alt) {
295296
// location
296-
if (url === 'back') url = this.ctx.get('Referrer') || alt || '/'
297+
if (url === 'back') {
298+
deprecate('Special-cased string "back" through redirect will be removed in v3, ' +
299+
'consider migrating usage to ctx.back() instead.')
300+
url = this.ctx.get('Referrer') || alt || '/'
301+
}
297302
if (/^https?:\/\//i.test(url)) {
298303
// formatting url again avoid security escapes
299304
url = new URL(url).toString()
@@ -316,6 +321,24 @@ module.exports = {
316321
this.body = `Redirecting to ${url}.`
317322
},
318323

324+
/**
325+
* Perform a special-cased "back" to provide Referrer support.
326+
* When Referrer is not present, `alt` or "/" is used.
327+
*
328+
* Examples:
329+
*
330+
* ctx.back()
331+
* ctx.back('/index.html')
332+
*
333+
* @param {String} [alt]
334+
* @api public
335+
*/
336+
337+
back (alt) {
338+
const url = this.ctx.get('Referrer') || alt || '/'
339+
this.redirect(url)
340+
},
341+
319342
/**
320343
* Set Content-Disposition header to "attachment" with optional `filename`.
321344
*

0 commit comments

Comments
 (0)