|
| 1 | +import { useHead, useSeoMeta } from 'unhead' |
| 2 | +import { renderDOMHead } from 'unhead/client' |
| 3 | +import { renderSSRHead } from 'unhead/server' |
| 4 | +import { describe, it } from 'vitest' |
| 5 | +import { createClientHeadWithContext, createServerHeadWithContext, useDom } from '../../util' |
| 6 | + |
| 7 | +describe('unhead e2e useServerSeoMeta', () => { |
| 8 | + it('useServerSeoMeta', async () => { |
| 9 | + // scenario: we are injecting root head schema which will not have a hydration step, |
| 10 | + // but we are also injecting a child head schema which will have a hydration step |
| 11 | + const ssrHead = createServerHeadWithContext() |
| 12 | + useSeoMeta(ssrHead, { |
| 13 | + title: 'title from nuxt.config.ts', |
| 14 | + description: 'Description from nuxt.config.ts', |
| 15 | + }, { |
| 16 | + tagPriority: 'low', |
| 17 | + }) |
| 18 | + useHead(ssrHead, { |
| 19 | + htmlAttrs: { lang: 'nuxt.config' }, |
| 20 | + script: [ |
| 21 | + { |
| 22 | + innerHTML: 'lorem ipsum generate more lorem ipsum', |
| 23 | + }, |
| 24 | + ], |
| 25 | + }, { |
| 26 | + mode: 'server', |
| 27 | + }) |
| 28 | + // i.e App.vue |
| 29 | + useSeoMeta(ssrHead, { |
| 30 | + title: 'title mainpage', |
| 31 | + description: 'description mainpage', |
| 32 | + }, { |
| 33 | + mode: 'server', |
| 34 | + }) |
| 35 | + useHead(ssrHead, { |
| 36 | + htmlAttrs: { lang: 'app.vue' }, |
| 37 | + }, { |
| 38 | + mode: 'server', |
| 39 | + }) |
| 40 | + |
| 41 | + const data = await renderSSRHead(ssrHead) |
| 42 | + |
| 43 | + expect(data).toMatchInlineSnapshot(` |
| 44 | + { |
| 45 | + "bodyAttrs": "", |
| 46 | + "bodyTags": "", |
| 47 | + "bodyTagsOpen": "", |
| 48 | + "headTags": "<title>title mainpage</title> |
| 49 | + <script>lorem ipsum generate more lorem ipsum</script> |
| 50 | + <meta name="description" content="description mainpage"> |
| 51 | + <script id="unhead:payload" type="application/json">{"title":"title mainpage"}</script>", |
| 52 | + "htmlAttrs": " lang="app.vue"", |
| 53 | + } |
| 54 | + `) |
| 55 | + |
| 56 | + const dom = useDom(data) |
| 57 | + |
| 58 | + const csrHead = createClientHeadWithContext({ |
| 59 | + document: dom.window.document, |
| 60 | + }) |
| 61 | + useHead(ssrHead, { |
| 62 | + htmlAttrs: { lang: 'nuxt.config' }, |
| 63 | + }, { |
| 64 | + tagPriority: 'low', |
| 65 | + }) |
| 66 | + useSeoMeta(csrHead, { |
| 67 | + title: 'Some cool reproduction repo :)', |
| 68 | + description: 'Description from nuxt.config.ts', |
| 69 | + }, { |
| 70 | + tagPriority: 'low', |
| 71 | + }) |
| 72 | + |
| 73 | + await renderDOMHead(csrHead, { document: dom.window.document }) |
| 74 | + |
| 75 | + expect(dom.serialize()).toMatchInlineSnapshot(` |
| 76 | + "<!DOCTYPE html><html lang="app.vue"><head> |
| 77 | + <title>title mainpage</title> |
| 78 | + <script>lorem ipsum generate more lorem ipsum</script> |
| 79 | + <meta name="description" content="Description from nuxt.config.ts"> |
| 80 | + <script id="unhead:payload" type="application/json">{"title":"title mainpage"}</script> |
| 81 | + </head> |
| 82 | + <body> |
| 83 | +
|
| 84 | + <div> |
| 85 | + <h1>hello world</h1> |
| 86 | + </div> |
| 87 | +
|
| 88 | +
|
| 89 | +
|
| 90 | + </body></html>" |
| 91 | + `) |
| 92 | + }) |
| 93 | +}) |
0 commit comments