mirror of
https://github.com/nuxt/nuxt.git
synced 2025-02-18 14:41:25 +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,6 +152,18 @@ describe('basic ssr', () => {
|
|||||||
expect(html).toContain('<h1>Index page</h1>')
|
expect(html).toContain('<h1>Index page</h1>')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('/redirect', () => {
|
||||||
|
let _headers, _status
|
||||||
|
const renderContext = {
|
||||||
|
res: {
|
||||||
|
writeHead (status, headers) {
|
||||||
|
_status = status
|
||||||
|
_headers = headers
|
||||||
|
},
|
||||||
|
end () { }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
test('/redirect', async () => {
|
test('/redirect', async () => {
|
||||||
const { html, redirected } = await nuxt.server.renderRoute('/redirect')
|
const { html, redirected } = await nuxt.server.renderRoute('/redirect')
|
||||||
expect(html).toContain('<div id="__nuxt"></div>')
|
expect(html).toContain('<div id="__nuxt"></div>')
|
||||||
@ -168,21 +180,27 @@ describe('basic ssr', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
test('/redirect -> external link', async () => {
|
test('/redirect -> external link', async () => {
|
||||||
let _headers, _status
|
const { html } = await nuxt.server.renderRoute('/redirect-external', renderContext)
|
||||||
const { html } = await nuxt.server.renderRoute('/redirect-external', {
|
|
||||||
res: {
|
|
||||||
writeHead (status, headers) {
|
|
||||||
_status = status
|
|
||||||
_headers = headers
|
|
||||||
},
|
|
||||||
end () { }
|
|
||||||
}
|
|
||||||
})
|
|
||||||
expect(_status).toBe(302)
|
expect(_status).toBe(302)
|
||||||
expect(_headers.Location).toBe('https://nuxtjs.org')
|
expect(_headers.Location).toBe('https://nuxtjs.org/api/')
|
||||||
expect(html).toContain('<div data-server-rendered="true"></div>')
|
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>')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
test('/special-state -> check window.__NUXT__.test = true', async () => {
|
test('/special-state -> check window.__NUXT__.test = true', async () => {
|
||||||
const window = await nuxt.server.renderAndGetWindow(url('/special-state'))
|
const window = await nuxt.server.renderAndGetWindow(url('/special-state'))
|
||||||
expect(window.document.title).toBe('Nuxt.js')
|
expect(window.document.title).toBe('Nuxt.js')
|
||||||
|
@ -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