feature: redirect by route name

This commit is contained in:
Clark Du 2017-12-21 12:55:32 +08:00 committed by Pooya Parsa
parent b385ee74db
commit 83d5f059ee
6 changed files with 37 additions and 8 deletions

View File

@ -127,11 +127,16 @@ export async function setContext(app, context) {
if (!status) return if (!status) return
app.context._redirected = true // Used in middleware app.context._redirected = true // Used in middleware
// if only 1 or 2 arguments: redirect('/') or redirect('/', { foo: 'bar' }) // if only 1 or 2 arguments: redirect('/') or redirect('/', { foo: 'bar' })
if (typeof status === 'string' && (typeof path === 'undefined' || typeof path === 'object')) { let pathType = typeof path
if (typeof status !== 'number' && (pathType === 'undefined' || pathType === 'object')) {
query = path || {} query = path || {}
path = status path = status
pathType = typeof path
status = 302 status = 302
} }
if (pathType === 'object') {
path = app.router.resolve(path).href
}
// "/absolute/route", "./relative/route" or "../relative/route" // "/absolute/route", "./relative/route" or "../relative/route"
if (/(^[.]{1,2}\/)|(^\/(?!\/))/.test(path)) { if (/(^[.]{1,2}\/)|(^\/(?!\/))/.test(path)) {
app.context.next({ app.context.next({

View File

@ -148,21 +148,27 @@ test.serial('/error2', async t => {
t.deepEqual(await page.nuxt.errorData(), { message: 'Custom error' }) t.deepEqual(await page.nuxt.errorData(), { message: 'Custom error' })
}) })
test.serial('/redirect2', async t => { test.serial('/redirect-middleware', async t => {
await page.nuxt.navigate('/redirect2') await page.nuxt.navigate('/redirect-middleware')
t.is(await page.$text('h1'), 'Index page') t.is(await page.$text('h1'), 'Index page')
}) })
test.serial('/redirect3', async t => { test.serial('/redirect-external', async t => {
// New page for redirecting to external link. // New page for redirecting to external link.
const page = await browser.page(url('/')) const page = await browser.page(url('/'))
await page.nuxt.navigate('/redirect3', false) await page.nuxt.navigate('/redirect-external', false)
await page.waitForFunction(() => window.location.href === 'https://nuxtjs.org/') await page.waitForFunction(() => window.location.href === 'https://nuxtjs.org/')
page.close() page.close()
t.pass() t.pass()
}) })
test.serial('/redirect-name', async t => {
await page.nuxt.navigate('/redirect-name')
t.is(await page.$text('h1'), 'My component!')
})
test.serial('/no-ssr', async t => { test.serial('/no-ssr', async t => {
await page.nuxt.navigate('/no-ssr') await page.nuxt.navigate('/no-ssr')

View File

@ -132,7 +132,7 @@ test('/redirect -> check redirected source', async t => {
test('/redirect -> external link', async t => { test('/redirect -> external link', async t => {
const headers = {} const headers = {}
const { html } = await nuxt.renderRoute('/redirect3', { const { html } = await nuxt.renderRoute('/redirect-external', {
res: { res: {
setHeader(k, v) { setHeader(k, v) {
headers[k] = v headers[k] = v
@ -187,14 +187,21 @@ test.serial('/error-midd', async t => {
t.true(errorSpy.notCalled) t.true(errorSpy.notCalled)
}) })
test.serial('/redirect2', async t => { test.serial('/redirect-middleware', async t => {
const errorSpy = await interceptError() const errorSpy = await interceptError()
await rp(url('/redirect2')) // Should not console.error await rp(url('/redirect-middleware')) // Should not console.error
release() release()
// Don't display error since redirect returns a noopApp // Don't display error since redirect returns a noopApp
t.true(errorSpy.notCalled) t.true(errorSpy.notCalled)
}) })
test('/redirect-name', async t => {
const { html, redirected } = await nuxt.renderRoute('/redirect-name')
t.true(html.includes('<div id="__nuxt"></div>'))
t.true(redirected.path === '/stateless')
t.true(redirected.status === 302)
})
test('/no-ssr', async t => { test('/no-ssr', async t => {
const { html } = await nuxt.renderRoute('/no-ssr') const { html } = await nuxt.renderRoute('/no-ssr')
t.true(html.includes('<div class="no-ssr-placeholder">&lt;p&gt;Loading...&lt;/p&gt;</div>')) t.true(html.includes('<div class="no-ssr-placeholder">&lt;p&gt;Loading...&lt;/p&gt;</div>'))

View File

@ -0,0 +1,11 @@
<template>
<div>Redirecting...</div>
</template>
<script>
export default {
fetch({ redirect }) {
return redirect({name: 'stateless'})
}
}
</script>