mirror of
https://github.com/nuxt/nuxt.git
synced 2025-02-17 06:01:34 +00:00
feature: redirect by route name
This commit is contained in:
parent
b385ee74db
commit
83d5f059ee
@ -127,11 +127,16 @@ export async function setContext(app, context) {
|
||||
if (!status) return
|
||||
app.context._redirected = true // Used in middleware
|
||||
// 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 || {}
|
||||
path = status
|
||||
pathType = typeof path
|
||||
status = 302
|
||||
}
|
||||
if (pathType === 'object') {
|
||||
path = app.router.resolve(path).href
|
||||
}
|
||||
// "/absolute/route", "./relative/route" or "../relative/route"
|
||||
if (/(^[.]{1,2}\/)|(^\/(?!\/))/.test(path)) {
|
||||
app.context.next({
|
||||
|
@ -148,21 +148,27 @@ test.serial('/error2', async t => {
|
||||
t.deepEqual(await page.nuxt.errorData(), { message: 'Custom error' })
|
||||
})
|
||||
|
||||
test.serial('/redirect2', async t => {
|
||||
await page.nuxt.navigate('/redirect2')
|
||||
test.serial('/redirect-middleware', async t => {
|
||||
await page.nuxt.navigate('/redirect-middleware')
|
||||
|
||||
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.
|
||||
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/')
|
||||
page.close()
|
||||
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 => {
|
||||
await page.nuxt.navigate('/no-ssr')
|
||||
|
||||
|
@ -132,7 +132,7 @@ test('/redirect -> check redirected source', async t => {
|
||||
|
||||
test('/redirect -> external link', async t => {
|
||||
const headers = {}
|
||||
const { html } = await nuxt.renderRoute('/redirect3', {
|
||||
const { html } = await nuxt.renderRoute('/redirect-external', {
|
||||
res: {
|
||||
setHeader(k, v) {
|
||||
headers[k] = v
|
||||
@ -187,14 +187,21 @@ test.serial('/error-midd', async t => {
|
||||
t.true(errorSpy.notCalled)
|
||||
})
|
||||
|
||||
test.serial('/redirect2', async t => {
|
||||
test.serial('/redirect-middleware', async t => {
|
||||
const errorSpy = await interceptError()
|
||||
await rp(url('/redirect2')) // Should not console.error
|
||||
await rp(url('/redirect-middleware')) // Should not console.error
|
||||
release()
|
||||
// Don't display error since redirect returns a noopApp
|
||||
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 => {
|
||||
const { html } = await nuxt.renderRoute('/no-ssr')
|
||||
t.true(html.includes('<div class="no-ssr-placeholder"><p>Loading...</p></div>'))
|
||||
|
11
test/fixtures/basic/pages/redirect-name.vue
vendored
Normal file
11
test/fixtures/basic/pages/redirect-name.vue
vendored
Normal file
@ -0,0 +1,11 @@
|
||||
<template>
|
||||
<div>Redirecting...</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
export default {
|
||||
fetch({ redirect }) {
|
||||
return redirect({name: 'stateless'})
|
||||
}
|
||||
}
|
||||
</script>
|
Loading…
Reference in New Issue
Block a user