Skip to content

Commit d601d7b

Browse files
committed
breaking: remove redirect(back) for v3
1 parent 7716028 commit d601d7b

File tree

3 files changed

+7
-49
lines changed

3 files changed

+7
-49
lines changed

__tests__/response/redirect.test.js

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -45,34 +45,6 @@ describe('ctx.redirect(url)', () => {
4545
})
4646
})
4747

48-
describe('with "back"', () => {
49-
it('should redirect to Referrer', () => {
50-
const ctx = context()
51-
ctx.req.headers.referrer = '/login'
52-
ctx.redirect('back')
53-
assert.strictEqual(ctx.response.header.location, '/login')
54-
})
55-
56-
it('should redirect to Referer', () => {
57-
const ctx = context()
58-
ctx.req.headers.referer = '/login'
59-
ctx.redirect('back')
60-
assert.strictEqual(ctx.response.header.location, '/login')
61-
})
62-
63-
it('should default to alt', () => {
64-
const ctx = context()
65-
ctx.redirect('back', '/index.html')
66-
assert.strictEqual(ctx.response.header.location, '/index.html')
67-
})
68-
69-
it('should default redirect to /', () => {
70-
const ctx = context()
71-
ctx.redirect('back')
72-
assert.strictEqual(ctx.response.header.location, '/')
73-
})
74-
})
75-
7648
describe('when html is accepted', () => {
7749
it('should respond with html', () => {
7850
const ctx = context()

docs/api/response.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -287,17 +287,11 @@ app.use(async (ctx, next) => {
287287
});
288288
```
289289

290-
### response.redirect(url, [alt])
290+
### response.redirect(url)
291291

292292
Perform a [302] redirect to `url`.
293293

294-
The string "back" is special-cased
295-
to provide Referrer support, when Referrer
296-
is not present `alt` or "/" is used.
297-
298294
```js
299-
ctx.redirect('back');
300-
ctx.redirect('back', '/index.html');
301295
ctx.redirect('/login');
302296
ctx.redirect('http://google.com');
303297
```
@@ -311,6 +305,11 @@ ctx.redirect('/cart');
311305
ctx.body = 'Redirecting to shopping cart';
312306
```
313307

308+
### response.back(url)
309+
310+
Similar to `.redirect(url)`, but first checks the `referer` header to redirect.
311+
This is new in v3 as `.redirect(url, alt)` removes the special case `url = 'back'` option.
312+
314313
### response.attachment([filename], [options])
315314

316315
Set `Content-Disposition` to "attachment" to signal the client

lib/response.js

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -273,29 +273,16 @@ module.exports = {
273273
/**
274274
* Perform a 302 redirect to `url`.
275275
*
276-
* The string "back" is special-cased
277-
* to provide Referrer support, when Referrer
278-
* is not present `alt` or "/" is used.
279-
*
280276
* Examples:
281277
*
282-
* this.redirect('back');
283-
* this.redirect('back', '/index.html');
284278
* this.redirect('/login');
285279
* this.redirect('http://google.com');
286280
*
287281
* @param {String} url
288-
* @param {String} [alt]
289282
* @api public
290283
*/
291284

292-
redirect (url, alt) {
293-
// location
294-
if (url === 'back') {
295-
deprecate('Special-cased string "back" through redirect will be removed in v3, ' +
296-
'consider migrating usage to ctx.back() instead.')
297-
url = this.ctx.get('Referrer') || alt || '/'
298-
}
285+
redirect (url) {
299286
if (/^https?:\/\//i.test(url)) {
300287
// formatting url again avoid security escapes
301288
url = new URL(url).toString()

0 commit comments

Comments
 (0)