mirror of
https://github.com/tenrok/vue-meta.git
synced 2026-06-20 16:00:33 +03:00
feat: add option for prepending (no)script to body (#410)
* feat: add option for prepending (no)script to body * test: use browser getUrl * refactor: use pbody insteadn of pody * test: add prepend/append body generator test * test: add prepend body updater test * chore: remove typo
This commit is contained in:
@@ -67,14 +67,7 @@ describe(browserString, () => {
|
||||
})
|
||||
|
||||
test('open page', async () => {
|
||||
const webPath = '/index.html'
|
||||
|
||||
let url
|
||||
if (browser.getLocalFolderUrl) {
|
||||
url = browser.getLocalFolderUrl(webPath)
|
||||
} else {
|
||||
url = `file://${path.join(folder, webPath)}`
|
||||
}
|
||||
const url = browser.getUrl('/index.html')
|
||||
|
||||
page = await browser.page(url)
|
||||
|
||||
@@ -91,12 +84,16 @@ describe(browserString, () => {
|
||||
sanitizeCheck.push(...(await page.getTexts('noscript')))
|
||||
sanitizeCheck = sanitizeCheck.filter(v => !!v)
|
||||
|
||||
expect(sanitizeCheck.length).toBe(3)
|
||||
expect(sanitizeCheck.length).toBe(4)
|
||||
expect(() => JSON.parse(sanitizeCheck[0])).not.toThrow()
|
||||
// TODO: check why this doesnt Throw when Home is dynamic loaded
|
||||
// (but that causes hydration error)
|
||||
expect(() => JSON.parse(sanitizeCheck[1])).toThrow()
|
||||
expect(() => JSON.parse(sanitizeCheck[2])).not.toThrow()
|
||||
expect(() => JSON.parse(sanitizeCheck[3])).not.toThrow()
|
||||
|
||||
expect(await page.getElementCount('body noscript:first-child')).toBe(1)
|
||||
expect(await page.getElementCount('body noscript:last-child')).toBe(1)
|
||||
})
|
||||
|
||||
test('/about', async () => {
|
||||
|
||||
@@ -18,6 +18,11 @@ describe('basic browser with ssr page', () => {
|
||||
expect(html.match(/<meta/g).length).toBe(2)
|
||||
expect(html.match(/<meta/g).length).toBe(2)
|
||||
|
||||
// body prepend
|
||||
expect(html.match(/<body[^>]*>\s*<noscript/g).length).toBe(1)
|
||||
// body append
|
||||
expect(html.match(/noscript>\s*<\/body/g).length).toBe(1)
|
||||
|
||||
const re = /<(no)?script[^>]+type="application\/ld\+json"[^>]*>(.*?)</g
|
||||
const sanitizeCheck = []
|
||||
let match
|
||||
@@ -25,9 +30,10 @@ describe('basic browser with ssr page', () => {
|
||||
sanitizeCheck.push(match[2])
|
||||
}
|
||||
|
||||
expect(sanitizeCheck.length).toBe(3)
|
||||
expect(sanitizeCheck.length).toBe(4)
|
||||
expect(() => JSON.parse(sanitizeCheck[0])).not.toThrow()
|
||||
expect(() => JSON.parse(sanitizeCheck[1])).toThrow()
|
||||
expect(() => JSON.parse(sanitizeCheck[2])).not.toThrow()
|
||||
expect(() => JSON.parse(sanitizeCheck[3])).not.toThrow()
|
||||
})
|
||||
})
|
||||
|
||||
Vendored
+2
@@ -10,6 +10,8 @@
|
||||
{{ noscript.text() }}
|
||||
</head>
|
||||
<body {{ bodyAttrs.text() }}>
|
||||
{{ script.text({ pbody: true }) }}
|
||||
{{ noscript.text({ pbody: true }) }}
|
||||
{{ app }}
|
||||
{{ script.text({ body: true }) }}
|
||||
{{ noscript.text({ body: true }) }}
|
||||
|
||||
Vendored
+1
@@ -27,6 +27,7 @@ export default {
|
||||
{ innerHTML: '{ "more": "data" }', type: 'application/ld+json' }
|
||||
],
|
||||
noscript: [
|
||||
{ innerHTML: '{ "pbody": "yes" }', pbody: true, type: 'application/ld+json' },
|
||||
{ innerHTML: '{ "body": "yes" }', body: true, type: 'application/ld+json' }
|
||||
],
|
||||
__dangerouslyDisableSanitizers: ['noscript'],
|
||||
|
||||
@@ -81,4 +81,22 @@ describe('extra tests', () => {
|
||||
const bodyAttrs = generateServerInjector('bodyAttrs', {})
|
||||
expect(bodyAttrs.text(true)).toBe('')
|
||||
})
|
||||
|
||||
test('script prepend body', () => {
|
||||
const tags = [{ src: '/script.js', pbody: true }]
|
||||
const scriptTags = generateServerInjector('script', tags)
|
||||
|
||||
expect(scriptTags.text()).toBe('')
|
||||
expect(scriptTags.text({ body: true })).toBe('')
|
||||
expect(scriptTags.text({ pbody: true })).toBe('<script data-vue-meta="test" src="/script.js" data-pbody="true"></script>')
|
||||
})
|
||||
|
||||
test('script append body', () => {
|
||||
const tags = [{ src: '/script.js', body: true }]
|
||||
const scriptTags = generateServerInjector('script', tags)
|
||||
|
||||
expect(scriptTags.text()).toBe('')
|
||||
expect(scriptTags.text({ body: true })).toBe('<script data-vue-meta="test" src="/script.js" data-body="true"></script>')
|
||||
expect(scriptTags.text({ pbody: true })).toBe('')
|
||||
})
|
||||
})
|
||||
|
||||
@@ -114,10 +114,12 @@ const metaInfoData = {
|
||||
add: {
|
||||
data: [
|
||||
{ src: 'src', async: false, defer: true, [defaultOptions.tagIDKeyName]: 'content' },
|
||||
{ src: 'src-prepend', async: true, defer: false, pbody: true },
|
||||
{ src: 'src', async: false, defer: true, body: true }
|
||||
],
|
||||
expect: [
|
||||
'<script data-vue-meta="test" src="src" defer data-vmid="content"></script>',
|
||||
'<script data-vue-meta="test" src="src-prepend" async data-pbody="true"></script>',
|
||||
'<script data-vue-meta="test" src="src" defer data-body="true"></script>'
|
||||
],
|
||||
test (side, defaultTest) {
|
||||
@@ -130,14 +132,17 @@ const metaInfoData = {
|
||||
|
||||
expect(tags.addedTags.script[0].parentNode.tagName).toBe('HEAD')
|
||||
expect(tags.addedTags.script[1].parentNode.tagName).toBe('BODY')
|
||||
expect(tags.addedTags.script[2].parentNode.tagName).toBe('BODY')
|
||||
} else {
|
||||
// ssr doesnt generate data-body tags
|
||||
const bodyScript = this.expect[1]
|
||||
const bodyPrepended = this.expect[1]
|
||||
const bodyAppended = this.expect[2]
|
||||
this.expect = [this.expect[0]]
|
||||
|
||||
const tags = defaultTest()
|
||||
|
||||
expect(tags.text()).not.toContain(bodyScript)
|
||||
expect(tags.text()).not.toContain(bodyPrepended)
|
||||
expect(tags.text()).not.toContain(bodyAppended)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user