mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-11 08:33:53 +00:00
fix(vue-renderer): clone spa meta to prevent cache modification (#5964)
This commit is contained in:
parent
d1395a032a
commit
ae9d3519f7
@ -1,4 +1,5 @@
|
||||
import { extname } from 'path'
|
||||
import cloneDeep from 'lodash/cloneDeep'
|
||||
import Vue from 'vue'
|
||||
import VueMeta from 'vue-meta'
|
||||
import { createRenderer } from 'vue-server-renderer'
|
||||
@ -43,7 +44,9 @@ export default class SPARenderer extends BaseRenderer {
|
||||
let meta = this.cache.get(cacheKey)
|
||||
|
||||
if (meta) {
|
||||
return meta
|
||||
// Return a copy of the content, so that future
|
||||
// modifications do not effect the data in cache
|
||||
return cloneDeep(meta)
|
||||
}
|
||||
|
||||
meta = {
|
||||
@ -149,7 +152,9 @@ export default class SPARenderer extends BaseRenderer {
|
||||
// Set meta tags inside cache
|
||||
this.cache.set(cacheKey, content)
|
||||
|
||||
return content
|
||||
// Return a copy of the content, so that future
|
||||
// modifications do not effect the data in cache
|
||||
return cloneDeep(content)
|
||||
}
|
||||
|
||||
static normalizeFile(file) {
|
||||
|
16
test/fixtures/spa/nuxt.config.js
vendored
16
test/fixtures/spa/nuxt.config.js
vendored
@ -1,3 +1,10 @@
|
||||
function modifyHtml(html) {
|
||||
return html.replace(
|
||||
'</body>',
|
||||
`<!-- extra html from render:route hook added at ${Date.now()}--></body>`
|
||||
)
|
||||
}
|
||||
|
||||
export default {
|
||||
mode: 'spa',
|
||||
pageTransition: false,
|
||||
@ -18,7 +25,10 @@ export default {
|
||||
router: {
|
||||
middleware: 'middleware'
|
||||
},
|
||||
plugins: [
|
||||
'~/plugins/error.js'
|
||||
]
|
||||
plugins: ['~/plugins/error.js'],
|
||||
hooks: {
|
||||
'render:route': (url, page, { req, res }) => {
|
||||
page.html = modifyHtml(page.html)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -138,6 +138,22 @@ describe('spa', () => {
|
||||
consola.log.mockClear()
|
||||
})
|
||||
|
||||
test('render:route hook does not corrupt the cache', async () => {
|
||||
const window1 = await nuxt.server.renderAndGetWindow(url('/'))
|
||||
const html1 = window1.document.body.innerHTML
|
||||
expect(html1).toContain('extra html from render:route hook')
|
||||
expect(html1.match(/render:route/g).length).toBe(1)
|
||||
|
||||
window1.close()
|
||||
|
||||
const window2 = await nuxt.server.renderAndGetWindow(url('/'))
|
||||
const html2 = window2.document.body.innerHTML
|
||||
expect(html2).toContain('extra html from render:route hook')
|
||||
expect(html2.match(/render:route/g).length).toBe(1)
|
||||
|
||||
window2.close()
|
||||
})
|
||||
|
||||
// Close server and ask nuxt to stop listening to file changes
|
||||
afterAll(async () => {
|
||||
await nuxt.close()
|
||||
|
Loading…
Reference in New Issue
Block a user