Skip to content

Commit

Permalink
feat: read cookie from server
Browse files Browse the repository at this point in the history
  • Loading branch information
manniL committed Nov 25, 2023
1 parent 957d9d4 commit c8eab90
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/runtime/plugin.server.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { reactive, ref } from 'vue'

import type { ColorModeInstance } from './types'
import { defineNuxtPlugin, isVue2, isVue3, useHead, useState, useRouter } from '#imports'
import { preference, hid, script, dataValue } from '#color-mode-options'
import { defineNuxtPlugin, isVue2, isVue3, useHead, useState, useRouter, useRequestHeaders } from '#imports'
import { preference, hid, script, dataValue, storage, storageKey } from '#color-mode-options'

const addScript = (head) => {
head.script = head.script || []
Expand Down Expand Up @@ -45,6 +45,13 @@ export default defineNuxtPlugin((nuxtApp) => {
}

if (isVue3) {
if (storage === 'cookie') {
const { cookie } = useRequestHeaders(['cookie'])
const [, value] = cookie?.split('; ').map(s => s.split('=')).find(([k]) => k === storageKey) ?? []
if (value) {
colorMode.preference = value
}
}
useHead({ htmlAttrs })
}

Expand Down

0 comments on commit c8eab90

Please sign in to comment.