Skip to content
This repository has been archived by the owner on Feb 4, 2022. It is now read-only.

argument str must be a string on nuxt server persist #398

Open
ilyapisany opened this issue Feb 28, 2021 · 7 comments
Open

argument str must be a string on nuxt server persist #398

ilyapisany opened this issue Feb 28, 2021 · 7 comments

Comments

@ilyapisany
Copy link

  • vuex-persistedstate version: ^4.0.0-beta.3
  • node version: v10.19.0
  • npm (or yarn) version: npm v16.14.4

What you did: Install package and use default plugin from readme (nuxt cookie server persist)

What happened: If no cookie set (example on first page loading).

if (process.server) {
     const parsedCookies = cookie.parse(req.headers.cookie);
     return parsedCookies[key];
}

throws type error argument str must be a string

Reproduction sandbox:

Problem description: cookie package receive string as first parameter but undefined given on empty cookie in browser

Suggested solution: Check is request.headers.cookie is string and set it to blank if not

@VconexID
Copy link

VconexID commented Mar 6, 2021

i have this issue as well in new version, currently i use vuex-persistedstate 3.1.0 and it works perfectly.

@ilyapisany
Copy link
Author

I fixed this issue by checking is request.headers.cookie is string like this

let headerCookie = req.headers.cookie;
if (typeof headerCookie !== 'string') {
  headerCookie = '';
}
const parsedCookies = cookie.parse(headerCookie);
return parsedCookies[key];

@robinvdvleuten
Copy link
Owner

This seem to be more like a Nuxt issue on how it handles incoming cookies.

@ilyapisany
Copy link
Author

I this it's problem of cookie package and it ca be resolved by fix readme plugin template (changes in related mr)

@dosstx
Copy link

dosstx commented Oct 4, 2021

@ilyapisany I still seem to get the argument str must be a string error.

I have added to Nuxt like so:

/plugins/persistedState.js:


import createPersistedState from 'vuex-persistedstate'
import * as Cookies from 'js-cookie'
import cookie from 'cookie'

export default ({ store, req }) => {
  createPersistedState({
    storage: {
      getItem: (key) => {
        // See https://nuxtjs.org/guide/plugins/#using-process-flags
        if (process.server) {
          let cookieHeader = cookie.parse(req.headers.cookie)
          if (typeof cookieHeader !== 'string') {
            cookieHeader = ''
          }
          const parsedCookies = cookie.parse(cookieHeader)
          return parsedCookies[key]
        } else {
          return Cookies.get(key)
        }
      },
      // Please see https://github.com/js-cookie/js-cookie#json, on how to handle JSON.
      setItem: (key, value) =>
        Cookies.set(key, value, { expires: 365, secure: false }),
      removeItem: (key) => Cookies.remove(key)
    }
  })(store)
}

And plugins:

plugins: [
    { src: '~/plugins/persistedState.js' }
]

Any ideas?

@alfredomtzrmz
Copy link

same here.. has anyone can fixed it?

@Jimmar
Copy link

Jimmar commented Nov 4, 2021

a one liner alternative that worked for me was

const parsedCookies = cookie.parse(req.headers.cookie ?? "");
return parsedCookies[key];

or the full the plugin

// plugins/persistedState.js

import createPersistedState from "vuex-persistedstate";
import Cookies from "js-cookie";
import cookie from "cookie";

export default ({ store, req }) => {
  createPersistedState({
    paths: [...],
    storage: {
      getItem: key => {
        if (process.server) {
          const parsedCookies = cookie.parse(req.headers.cookie ?? "");
          return parsedCookies[key];
        } else {
          return Cookies.get(key);
        }
      },
      setItem: (key, value) => Cookies.set(key, value, { expires: 1, secure: false }),
      removeItem: key => Cookies.remove(key)
    }
  })(store);
};

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants