refactor: use head script instead of cookie
This commit is contained in:
parent
dcf0dd7018
commit
55aff4778b
8 changed files with 62 additions and 45 deletions
38
plugins/setup-head-script.server.ts
Normal file
38
plugins/setup-head-script.server.ts
Normal file
|
@ -0,0 +1,38 @@
|
|||
import { STORAGE_KEY_CURRENT_USER_HANDLE, STORAGE_KEY_SETTINGS } from '~/constants'
|
||||
import { fontSizeMap } from '~/constants/options'
|
||||
|
||||
/**
|
||||
* Injecting scripts before renders
|
||||
*/
|
||||
export default defineNuxtPlugin(() => {
|
||||
useHead({
|
||||
script: [
|
||||
{
|
||||
innerHTML: `
|
||||
;(function() {
|
||||
const handle = localStorage.getItem('${STORAGE_KEY_CURRENT_USER_HANDLE}')
|
||||
if (!handle)
|
||||
return
|
||||
const allSettings = JSON.parse(localStorage.getItem('${STORAGE_KEY_SETTINGS}') || '{}')
|
||||
const settings = allSettings[handle]
|
||||
if (!settings)
|
||||
return
|
||||
|
||||
const html = document.querySelector('html')
|
||||
${process.dev ? 'console.log(\'settings\', settings)' : ''}
|
||||
|
||||
const { fontSize, language } = settings || {}
|
||||
|
||||
if (fontSize) {
|
||||
const fontSizeMap = ${JSON.stringify(fontSizeMap)}
|
||||
html.style.setProperty('--font-size', fontSizeMap[fontSize])
|
||||
}
|
||||
|
||||
if (language) {
|
||||
html.setAttribute('lang', language)
|
||||
}
|
||||
})()`.trim().replace(/\s*\n+\s*/g, ';'),
|
||||
},
|
||||
],
|
||||
})
|
||||
})
|
Loading…
Add table
Add a link
Reference in a new issue