|
| 1 | +import type { Head } from 'unhead/types' |
| 2 | +import { InferSeoMetaPlugin } from '@unhead/addons' |
| 3 | +import { bench, describe } from 'vitest' |
| 4 | +import { useHead, useSeoMeta, useServerHead } from '../../src' |
| 5 | +import { createHead as createServerHead, renderSSRHead } from '../../src/server' |
| 6 | + |
| 7 | +describe('ssr e2e bench', () => { |
| 8 | + bench('e2e', async () => { |
| 9 | + // we're going to replicate the logic needed to render the tags for a harlanzw.com page |
| 10 | + |
| 11 | + // 1. Add nuxt.config meta tags |
| 12 | + const head = createServerHead() |
| 13 | + // nuxt.config app.head |
| 14 | + head.push({ |
| 15 | + title: 'Harlan Wilton', |
| 16 | + templateParams: { |
| 17 | + separator: '·', |
| 18 | + }, |
| 19 | + script: [ |
| 20 | + { |
| 21 | + 'src': 'https://idea-lets-dance.harlanzw.com/script.js', |
| 22 | + 'data-spa': 'auto', |
| 23 | + 'data-site': 'VDJUVDNA', |
| 24 | + 'defer': true, |
| 25 | + }, |
| 26 | + ], |
| 27 | + }) |
| 28 | + const options = { mode: 'server' } as const |
| 29 | + // 1. payload |
| 30 | + head.push({ |
| 31 | + link: [ |
| 32 | + // resource hints for vue chunks |
| 33 | + { rel: 'preload', as: 'fetch', href: '/payload.json' }, |
| 34 | + ], |
| 35 | + }, options) |
| 36 | + // 2. styles |
| 37 | + head.push({ |
| 38 | + link: [ |
| 39 | + // page css, assume 5 of so css files |
| 40 | + { rel: 'stylesheet', href: '/page.css' }, |
| 41 | + { rel: 'stylesheet', href: '/page2.css' }, |
| 42 | + { rel: 'stylesheet', href: '/page3.css' }, |
| 43 | + { rel: 'stylesheet', href: '/page4.css' }, |
| 44 | + { rel: 'stylesheet', href: '/page5.css' }, |
| 45 | + ], |
| 46 | + }, options) |
| 47 | + // 3. resource hints |
| 48 | + head.push({ |
| 49 | + link: [ |
| 50 | + { rel: 'preload', as: 'script', href: '/_nuxt/runtime.js' }, |
| 51 | + { rel: 'preload', as: 'script', href: '/_nuxt/vendors.js' }, |
| 52 | + { rel: 'preload', as: 'script', href: '/_nuxt/app.js' }, |
| 53 | + ], |
| 54 | + }, options) |
| 55 | + // 4. payloads |
| 56 | + head.push({ |
| 57 | + script: [ |
| 58 | + // 4. payloads |
| 59 | + { innerHTML: { id: '__NUXT_DATA__', data: { initial: { bar: 'foo' }, payload: { foo: 'bar' } } } }, |
| 60 | + ], |
| 61 | + }, { |
| 62 | + ...options, |
| 63 | + tagPosition: 'bodyClose', |
| 64 | + tagPriority: 'high', |
| 65 | + }) |
| 66 | + // 5. scripts |
| 67 | + head.push({ |
| 68 | + script: [ |
| 69 | + { |
| 70 | + type: 'module', |
| 71 | + src: '/module.js', |
| 72 | + tagPosition: 'bodyClose', |
| 73 | + crossorigin: '', |
| 74 | + }, |
| 75 | + { |
| 76 | + src: '/non-module.js', |
| 77 | + tagPosition: 'bodyClose', |
| 78 | + defer: true, |
| 79 | + crossorigin: '', |
| 80 | + }, |
| 81 | + ], |
| 82 | + }, options) |
| 83 | + // start the vue rendererer |
| 84 | + // Nuxt SEO experiments |
| 85 | + head.use({ |
| 86 | + key: 'nuxt-seo-experiments', |
| 87 | + hooks: { |
| 88 | + 'tags:resolve': async ({ tags }) => { |
| 89 | + // iterate through tags that require absolute URLs and add the host base |
| 90 | + for (const tag of tags) { |
| 91 | + // og:image and twitter:image need to be absolute |
| 92 | + if (tag.tag !== 'meta') |
| 93 | + continue |
| 94 | + if (tag.props.property !== 'og:image:url' && tag.props.property !== 'og:image' && tag.props.name !== 'twitter:image') |
| 95 | + continue |
| 96 | + if (typeof tag.props.content !== 'string' || !tag.props.content.trim() || tag.props.content.startsWith('http') || tag.props.content.startsWith('//')) |
| 97 | + continue |
| 98 | + tag.props.content = `https://harlanzw.com${tag.props.content}` |
| 99 | + } |
| 100 | + }, |
| 101 | + }, |
| 102 | + }) |
| 103 | + head.use(InferSeoMetaPlugin()) |
| 104 | + const input: Head = { |
| 105 | + meta: [], |
| 106 | + templateParams: { |
| 107 | + site: { |
| 108 | + name: 'Harlan Wilton', |
| 109 | + url: 'https://harlanzw.com', |
| 110 | + description: 'Open source developer, contributing to the Vue, Nuxt, and Vite ecosystems.', |
| 111 | + }, |
| 112 | + // support legacy |
| 113 | + siteUrl: 'https://harlanzw.com', |
| 114 | + siteName: 'Harlan Wilton', |
| 115 | + }, |
| 116 | + } |
| 117 | + input.templateParams!.siteDescription = 'Open source developer, contributing to the Vue, Nuxt, and Vite ecosystems.' |
| 118 | + // we can setup a meta description |
| 119 | + input.meta!.push( |
| 120 | + { |
| 121 | + name: 'description', |
| 122 | + content: '%site.description', |
| 123 | + }, |
| 124 | + ) |
| 125 | + head.push(input, { tagPriority: 150 }) |
| 126 | + // Nuxt SEO |
| 127 | + const minimalPriority = { |
| 128 | + tagPriority: 101, |
| 129 | + head, |
| 130 | + } as const |
| 131 | + // needs higher priority |
| 132 | + useHead({ |
| 133 | + link: [{ rel: 'canonical', href: 'https://harlanzw.com/' }], |
| 134 | + }, { |
| 135 | + head, |
| 136 | + }) |
| 137 | + useServerHead({ |
| 138 | + htmlAttrs: { lang: 'en' }, |
| 139 | + }, { |
| 140 | + head, |
| 141 | + }) |
| 142 | + useHead({ |
| 143 | + templateParams: { site: { |
| 144 | + name: 'Harlan Wilton', |
| 145 | + url: 'https://harlanzw.com', |
| 146 | + description: 'Open source developer, contributing to the Vue, Nuxt, and Vite ecosystems.', |
| 147 | + }, siteName: 'Harlan Wilton' || '' }, |
| 148 | + titleTemplate: '%s %separator %siteName', |
| 149 | + }, minimalPriority) |
| 150 | + useSeoMeta({ |
| 151 | + ogType: 'website', |
| 152 | + ogUrl: 'https://harlanzw.com', |
| 153 | + ogLocale: 'en', |
| 154 | + ogSiteName: 'Harlan Wilton', |
| 155 | + description: 'Open source developer, contributing to the Vue, Nuxt, and Vite ecosystems.', |
| 156 | + twitterCreator: '@harlan_zw', |
| 157 | + twitterSite: '@harlan_zw', |
| 158 | + }, { ...minimalPriority, head }) |
| 159 | + // inferred from path /about (example) |
| 160 | + useHead({ |
| 161 | + title: 'About', |
| 162 | + }, { ...minimalPriority, head }) |
| 163 | + // OG Image |
| 164 | + const meta: Head['meta'] = [ |
| 165 | + { property: 'og:image', content: 'https://harlanzw.com/__og-image__/og.png' }, |
| 166 | + { property: 'og:image:type', content: `image/png` }, |
| 167 | + { name: 'twitter:card', content: 'summary_large_image' }, |
| 168 | + // we don't need this but avoids issue when using useSeoMeta({ twitterImage }) |
| 169 | + { name: 'twitter:image', content: 'https://harlanzw.com/__og-image__/og.png' }, |
| 170 | + { name: 'twitter:image:src', content: 'https://harlanzw.com/__og-image__/og.png' }, |
| 171 | + { property: 'og:image:width', content: 1200 }, |
| 172 | + { name: 'twitter:image:width', content: 1200 }, |
| 173 | + { property: 'og:image:height', content: 600 }, |
| 174 | + { name: 'twitter:image:height', content: 600 }, |
| 175 | + { property: 'og:image:alt', content: 'My Image' }, |
| 176 | + { name: 'twitter:image:alt', content: 'My Image' }, |
| 177 | + ] |
| 178 | + const script: Head['script'] = [{ |
| 179 | + id: 'nuxt-og-image-options', |
| 180 | + type: 'application/json', |
| 181 | + processTemplateParams: true, |
| 182 | + innerHTML: () => { |
| 183 | + const _input: Record<string, any> = { |
| 184 | + props: { |
| 185 | + color: 'red', |
| 186 | + }, |
| 187 | + } |
| 188 | + if (typeof _input.props.title === 'undefined') |
| 189 | + _input.props.title = '%s' |
| 190 | + delete _input.url |
| 191 | + // don't apply defaults |
| 192 | + return _input |
| 193 | + }, |
| 194 | + // we want this to be last in our head |
| 195 | + tagPosition: 'bodyClose', |
| 196 | + }] |
| 197 | + useServerHead({ |
| 198 | + script, |
| 199 | + meta, |
| 200 | + }, { |
| 201 | + head, |
| 202 | + tagPriority: 35, |
| 203 | + }) |
| 204 | + // Schema.org |
| 205 | + // entry |
| 206 | + useServerHead({ |
| 207 | + script: [{ |
| 208 | + type: 'application/ld+json', |
| 209 | + key: 'schema-org-graph', |
| 210 | + nodes: [ |
| 211 | + |
| 212 | + ], |
| 213 | + }], |
| 214 | + }, { |
| 215 | + head, |
| 216 | + }) |
| 217 | + // Robots |
| 218 | + useHead({ |
| 219 | + meta: [ |
| 220 | + { name: 'robots', content: 'index, follow' }, |
| 221 | + ], |
| 222 | + }, { |
| 223 | + head, |
| 224 | + }) |
| 225 | + // app.vue |
| 226 | + // [...all].vue |
| 227 | + useSeoMeta({ |
| 228 | + title: 'Home', |
| 229 | + description: 'Home page description', |
| 230 | + }, { |
| 231 | + head, |
| 232 | + }) |
| 233 | + // index.md |
| 234 | + useSeoMeta({ |
| 235 | + title: 'Home', |
| 236 | + description: 'Home page description', |
| 237 | + }, { |
| 238 | + head, |
| 239 | + }) |
| 240 | + |
| 241 | + const { headTags, bodyTags, bodyTagsOpen, htmlAttrs, bodyAttrs } = await renderSSRHead(head, { |
| 242 | + omitLineBreaks: true, |
| 243 | + }) |
| 244 | + function normalizeChunks(chunks: (string | undefined)[]) { |
| 245 | + return chunks.filter(Boolean).map(i => i!.trim()) |
| 246 | + } |
| 247 | + const htmlContext = { |
| 248 | + htmlAttrs: htmlAttrs ? [htmlAttrs] : [], |
| 249 | + head: normalizeChunks([headTags]), |
| 250 | + bodyAttrs: bodyAttrs ? [bodyAttrs] : [], |
| 251 | + bodyPrepend: normalizeChunks([bodyTagsOpen]), |
| 252 | + bodyAppend: [bodyTags], |
| 253 | + } |
| 254 | + // eslint-disable-next-line unused-imports/no-unused-vars |
| 255 | + const html = ` |
| 256 | +<!DOCTYPE html> |
| 257 | +<html${htmlContext.htmlAttrs.join(' ')}> |
| 258 | +<head> |
| 259 | +${htmlContext.head.join('\n')} |
| 260 | +</head> |
| 261 | +<body${htmlContext.bodyAttrs.join(' ')}> |
| 262 | +${htmlContext.bodyPrepend.join('\n')} |
| 263 | +${htmlContext.bodyAppend.join('\n')} |
| 264 | +</body> |
| 265 | +` |
| 266 | + }, { |
| 267 | + iterations: 5000, |
| 268 | + }) |
| 269 | + |
| 270 | + bench('simple', async () => { |
| 271 | + // 1. Add nuxt.config meta tags |
| 272 | + const head = createServerHead() |
| 273 | + // nuxt.config app.head |
| 274 | + head.push({ |
| 275 | + title: 'Harlan Wilton', |
| 276 | + templateParams: { |
| 277 | + separator: '·', |
| 278 | + }, |
| 279 | + script: [ |
| 280 | + { |
| 281 | + 'src': 'https://idea-lets-dance.harlanzw.com/script.js', |
| 282 | + 'data-spa': 'auto', |
| 283 | + 'data-site': 'VDJUVDNA', |
| 284 | + 'defer': true, |
| 285 | + }, |
| 286 | + ], |
| 287 | + }) |
| 288 | + await renderSSRHead(head) |
| 289 | + }) |
| 290 | +}) |
0 commit comments