DEV Community

Yasunori Tanaka
Yasunori Tanaka

Posted on

[From May 28 to June 3] The things I knew and thought last week

[Nuxt] set/get cookie in SSR
https://github.com/nuxt/nuxt.js/issues/2215

Another way

import Cookies from 'js-cookie'
https://github.com/js-cookie/js-cookie
Cookies.set('company.id', companyId, { expires: 7 })

I created a cookie util that gets cookie by key and parse

export const getCookie = (cookie: string, key: string): string | undefined => {
  let item = cookie.split(' ').find((v) => v.includes(key))
  if (item) {
    // remove semi colon "job.id=32;"
    if (item.includes(';')) {
      item = item.slice(0, -1)
    }
    // "job.id=32"
    const kv = item.split('=')
    item = kv[1]
  }

  return item
}

Top comments (0)