[Vuejs]-Which parameters are better to be stored in localStorage?

4👍

There’s no correct answer here; just some thoughts:

Unless you have a good reason to, I wouldn’t store the username in local storage. What advantage would you have by dong so? If the user logged in to their account from a different browser and changed their username, that would invalidate the username stored in local storage on other browsers. Most users expect that reloading the page should completely refresh all the data. People will think your site is broken if they reload the page and the username didn’t update. The less stuff you store in local storage, the less you need to worry about the data becoming stale.

At a minimum, you should definitely store authentication token in local storage. From that, you can make requests to the server on page load to obtain all other data including the user itself.

1👍

I do not think that you need to store anything in local storage at all. If you have some authentication setup, you could store just your token in a session cookie and retrieve all needed data from the server.

Trying to keep the saved data on the client to a minimum is a good idea. Just keep the token in cookie, retrieve username (and so on) from the server when first loading the page. The retrieved data can be stored in some javascript vars. These values get only cleared on a page reload and then you have to retrieve them once from the server.

Reloads should not occur often or not at all, when you have a single page app.

Leave a comment