41 lines
1.1 KiB
TypeScript
41 lines
1.1 KiB
TypeScript
import { defineNuxtPlugin } from '#app'
|
|
import { setup } from '@css-render/vue3-ssr'
|
|
|
|
export default defineNuxtPlugin((nuxtApp) => {
|
|
if (import.meta.server) {
|
|
const { collect } = setup(nuxtApp.vueApp)
|
|
const originalRenderMeta = nuxtApp.ssrContext?.renderMeta
|
|
nuxtApp.ssrContext!.renderMeta = () => {
|
|
if (!originalRenderMeta) {
|
|
return {
|
|
headTags: collect(),
|
|
}
|
|
}
|
|
const originalMeta = originalRenderMeta()
|
|
if ('headTags' in originalMeta) {
|
|
originalMeta.headTags += collect()
|
|
}
|
|
else {
|
|
originalMeta.headTags = collect()
|
|
}
|
|
return originalMeta
|
|
}
|
|
}
|
|
|
|
const { collect } = setup(nuxtApp.vueApp)
|
|
useServerHead({
|
|
style: () => {
|
|
const stylesString = collect()
|
|
const stylesArray = stylesString.split(/<\/style>/g).filter(style => style)
|
|
return stylesArray.map((styleString: string) => {
|
|
const match = styleString.match(/<style cssr-id="([^"]*)">([\s\S]*)/)
|
|
if (match) {
|
|
const id = match[1]
|
|
return { 'cssr-id': id, 'children': match[2] }
|
|
}
|
|
return {}
|
|
})
|
|
},
|
|
})
|
|
})
|