-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
ssr-plugin.mjs
40 lines (37 loc) · 964 Bytes
/
ssr-plugin.mjs
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
import { setSSRHandler } from '@vueuse/core'
import { defineNuxtPlugin, useCookie, useMeta } from '#imports'
setSSRHandler('getDefaultStorage', () => {
const cookieMap = new Map()
const get = (key) => {
if (!cookieMap.get(key))
cookieMap.set(key, useCookie(key, { maxAge: 2147483646 }))
return cookieMap.get(key)
}
return {
getItem: key => get(key).value,
setItem: (key, value) => get(key).value = value,
removeItem: key => get(key).value = undefined,
}
})
if (process.server) {
setSSRHandler('updateHTMLAttrs', (selector, attr, value) => {
if (selector === 'html') {
useMeta({
htmlAttrs: {
[attr]: value,
},
})
}
else if (selector === 'body') {
useMeta({
bodyAttrs: {
[attr]: value,
},
})
}
else {
throw new Error(`Unsupported meta selector "${selector}" in SSR`)
}
})
}
export default defineNuxtPlugin(() => { })