mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-27 08:02:01 +00:00
fix(vue-app): not strip trailing slash for redirect external domain (#7533)
This commit is contained in:
parent
39dd866b7b
commit
7f1429ebb3
@ -597,6 +597,10 @@ function formatUrl (url, query) {
|
|||||||
let result = (protocol ? protocol + '://' : '//') + parts.shift()
|
let result = (protocol ? protocol + '://' : '//') + parts.shift()
|
||||||
|
|
||||||
let path = parts.join('/')
|
let path = parts.join('/')
|
||||||
|
if (path === '' && parts.length === 1) {
|
||||||
|
result += '/'
|
||||||
|
}
|
||||||
|
|
||||||
let hash
|
let hash
|
||||||
parts = path.split('#')
|
parts = path.split('#')
|
||||||
if (parts.length === 2) {
|
if (parts.length === 2) {
|
||||||
|
@ -152,24 +152,9 @@ describe('basic ssr', () => {
|
|||||||
expect(html).toContain('<h1>Index page</h1>')
|
expect(html).toContain('<h1>Index page</h1>')
|
||||||
})
|
})
|
||||||
|
|
||||||
test('/redirect', async () => {
|
describe('/redirect', () => {
|
||||||
const { html, redirected } = await nuxt.server.renderRoute('/redirect')
|
|
||||||
expect(html).toContain('<div id="__nuxt"></div>')
|
|
||||||
expect(html).not.toContain('window.__NUXT__')
|
|
||||||
expect(redirected.path === '/').toBe(true)
|
|
||||||
expect(redirected.status === 302).toBe(true)
|
|
||||||
})
|
|
||||||
|
|
||||||
test('/redirect -> check redirected source', async () => {
|
|
||||||
// there are no transition properties in jsdom, ignore the error log
|
|
||||||
const window = await nuxt.server.renderAndGetWindow(url('/redirect'))
|
|
||||||
const html = window.document.body.innerHTML
|
|
||||||
expect(html).toContain('<h1>Index page</h1>')
|
|
||||||
})
|
|
||||||
|
|
||||||
test('/redirect -> external link', async () => {
|
|
||||||
let _headers, _status
|
let _headers, _status
|
||||||
const { html } = await nuxt.server.renderRoute('/redirect-external', {
|
const renderContext = {
|
||||||
res: {
|
res: {
|
||||||
writeHead (status, headers) {
|
writeHead (status, headers) {
|
||||||
_status = status
|
_status = status
|
||||||
@ -177,10 +162,43 @@ describe('basic ssr', () => {
|
|||||||
},
|
},
|
||||||
end () { }
|
end () { }
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
test('/redirect', async () => {
|
||||||
|
const { html, redirected } = await nuxt.server.renderRoute('/redirect')
|
||||||
|
expect(html).toContain('<div id="__nuxt"></div>')
|
||||||
|
expect(html).not.toContain('window.__NUXT__')
|
||||||
|
expect(redirected.path === '/').toBe(true)
|
||||||
|
expect(redirected.status === 302).toBe(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('/redirect -> check redirected source', async () => {
|
||||||
|
// there are no transition properties in jsdom, ignore the error log
|
||||||
|
const window = await nuxt.server.renderAndGetWindow(url('/redirect'))
|
||||||
|
const html = window.document.body.innerHTML
|
||||||
|
expect(html).toContain('<h1>Index page</h1>')
|
||||||
|
})
|
||||||
|
|
||||||
|
test('/redirect -> external link', async () => {
|
||||||
|
const { html } = await nuxt.server.renderRoute('/redirect-external', renderContext)
|
||||||
|
expect(_status).toBe(302)
|
||||||
|
expect(_headers.Location).toBe('https://nuxtjs.org/api/')
|
||||||
|
expect(html).toContain('<div data-server-rendered="true"></div>')
|
||||||
|
})
|
||||||
|
|
||||||
|
test('/redirect -> external link without trailing slash', async () => {
|
||||||
|
const { html } = await nuxt.server.renderRoute('/redirect-external-no-slash', renderContext)
|
||||||
|
expect(_status).toBe(302)
|
||||||
|
expect(_headers.Location).toBe('https://nuxtjs.org/api')
|
||||||
|
expect(html).toContain('<div data-server-rendered="true"></div>')
|
||||||
|
})
|
||||||
|
|
||||||
|
test('/redirect -> external link with root domain url', async () => {
|
||||||
|
const { html } = await nuxt.server.renderRoute('/redirect-external-root', renderContext)
|
||||||
|
expect(_status).toBe(302)
|
||||||
|
expect(_headers.Location).toBe('https://nuxtjs.org/')
|
||||||
|
expect(html).toContain('<div data-server-rendered="true"></div>')
|
||||||
})
|
})
|
||||||
expect(_status).toBe(302)
|
|
||||||
expect(_headers.Location).toBe('https://nuxtjs.org')
|
|
||||||
expect(html).toContain('<div data-server-rendered="true"></div>')
|
|
||||||
})
|
})
|
||||||
|
|
||||||
test('/special-state -> check window.__NUXT__.test = true', async () => {
|
test('/special-state -> check window.__NUXT__.test = true', async () => {
|
||||||
|
@ -261,7 +261,7 @@ describe('basic browser', () => {
|
|||||||
await page.nuxt.navigate('/redirect-external', false)
|
await page.nuxt.navigate('/redirect-external', false)
|
||||||
|
|
||||||
await page.waitForFunction(
|
await page.waitForFunction(
|
||||||
() => window.location.href === 'https://nuxtjs.org/'
|
() => window.location.href === 'https://nuxtjs.org/api/'
|
||||||
)
|
)
|
||||||
page.close()
|
page.close()
|
||||||
})
|
})
|
||||||
|
11
test/fixtures/basic/pages/redirect-external-no-slash.vue
vendored
Normal file
11
test/fixtures/basic/pages/redirect-external-no-slash.vue
vendored
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
<template>
|
||||||
|
<div>Redirecting...</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
export default {
|
||||||
|
middleware ({ redirect }) {
|
||||||
|
return redirect('https://nuxtjs.org/api')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</script>
|
11
test/fixtures/basic/pages/redirect-external-root.vue
vendored
Normal file
11
test/fixtures/basic/pages/redirect-external-root.vue
vendored
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
<template>
|
||||||
|
<div>Redirecting...</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
export default {
|
||||||
|
middleware ({ redirect }) {
|
||||||
|
return redirect('https://nuxtjs.org/')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</script>
|
@ -5,7 +5,7 @@
|
|||||||
<script>
|
<script>
|
||||||
export default {
|
export default {
|
||||||
middleware ({ redirect }) {
|
middleware ({ redirect }) {
|
||||||
return redirect('https://nuxtjs.org/')
|
return redirect('https://nuxtjs.org/api/')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
Loading…
Reference in New Issue
Block a user