mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-11 08:33:53 +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 path = parts.join('/')
|
||||
if (path === '' && parts.length === 1) {
|
||||
result += '/'
|
||||
}
|
||||
|
||||
let hash
|
||||
parts = path.split('#')
|
||||
if (parts.length === 2) {
|
||||
|
@ -152,6 +152,18 @@ describe('basic ssr', () => {
|
||||
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 () => {
|
||||
const { html, redirected } = await nuxt.server.renderRoute('/redirect')
|
||||
expect(html).toContain('<div id="__nuxt"></div>')
|
||||
@ -168,21 +180,27 @@ describe('basic ssr', () => {
|
||||
})
|
||||
|
||||
test('/redirect -> external link', async () => {
|
||||
let _headers, _status
|
||||
const { html } = await nuxt.server.renderRoute('/redirect-external', {
|
||||
res: {
|
||||
writeHead (status, headers) {
|
||||
_status = status
|
||||
_headers = headers
|
||||
},
|
||||
end () { }
|
||||
}
|
||||
})
|
||||
const { html } = await nuxt.server.renderRoute('/redirect-external', renderContext)
|
||||
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>')
|
||||
})
|
||||
|
||||
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 () => {
|
||||
const window = await nuxt.server.renderAndGetWindow(url('/special-state'))
|
||||
expect(window.document.title).toBe('Nuxt.js')
|
||||
|
@ -261,7 +261,7 @@ describe('basic browser', () => {
|
||||
await page.nuxt.navigate('/redirect-external', false)
|
||||
|
||||
await page.waitForFunction(
|
||||
() => window.location.href === 'https://nuxtjs.org/'
|
||||
() => window.location.href === 'https://nuxtjs.org/api/'
|
||||
)
|
||||
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>
|
||||
export default {
|
||||
middleware ({ redirect }) {
|
||||
return redirect('https://nuxtjs.org/')
|
||||
return redirect('https://nuxtjs.org/api/')
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
Loading…
Reference in New Issue
Block a user