Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | 1x 2x 2x 2x 2x 2x 2x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x | import { inject, InjectionKey, App } from 'vue'
interface CssrSsrContext {
styles: string[]
ids: Set<string>
}
const ssrContextKey =
'@css-render/vue3-ssr' as unknown as InjectionKey<CssrSsrContext>
function createStyleString (id: string, style: string): string {
return `<style cssr-id="${id}">\n${style}\n</style>`
}
function ssrAdapter (
id: string,
style: string,
ssrContext: CssrSsrContext
): void {
const { styles, ids } = ssrContext
// we need to impl other options to make it behaves the same as the client side
Iif (ids.has(id)) return
if (styles !== null) {
ids.add(id)
styles.push(createStyleString(id, style))
}
}
const isBrowser = typeof document !== 'undefined'
export function useSsrAdapter ():
| {
adapter: (id: string, style: string) => void
context: CssrSsrContext
}
| undefined {
Iif (isBrowser) return undefined
const context = inject(ssrContextKey, null)
Iif (context === null) return undefined
return {
adapter: (id, style) => ssrAdapter(id, style, context),
context
}
}
interface SsrHandle {
collect: () => string
}
export function setup (app: App): SsrHandle {
const styles: string[] = []
const ssrContext: CssrSsrContext = {
styles,
ids: new Set<string>()
}
app.provide(ssrContextKey, ssrContext)
return {
collect () {
const res = styles.join('\n')
styles.length = 0
return res
}
}
}
|