├── .all-contributorsrc ├── .github ├── CODE_OF_CONDUCT.md ├── ISSUE_TEMPLATE.md ├── PULL_REQUEST_TEMPLATE.md ├── dependabot.yml └── workflows │ └── test.yml ├── .gitignore ├── CHANGELOG.md ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── package.json ├── src └── index.ts ├── test.js └── tsconfig.json /.all-contributorsrc: -------------------------------------------------------------------------------- 1 | { 2 | "projectName": "vuex-persistedstate", 3 | "projectOwner": "robinvdvleuten", 4 | "repoType": "github", 5 | "files": [ 6 | "README.md" 7 | ], 8 | "imageSize": 100, 9 | "commit": true, 10 | "contributors": [ 11 | { 12 | "login": "robinvdvleuten", 13 | "name": "Robin van der Vleuten", 14 | "avatar_url": "https://avatars3.githubusercontent.com/u/238295?v=4", 15 | "profile": "https://robinvdvleuten.nl", 16 | "contributions": [ 17 | "code", 18 | "doc", 19 | "infra", 20 | "test" 21 | ] 22 | }, 23 | { 24 | "login": "zweizeichen", 25 | "name": "Sebastian", 26 | "avatar_url": "https://avatars1.githubusercontent.com/u/654071?v=4", 27 | "profile": "https://github.com/zweizeichen", 28 | "contributions": [ 29 | "code", 30 | "doc" 31 | ] 32 | }, 33 | { 34 | "login": "boris-graeff", 35 | "name": "Boris Graeff", 36 | "avatar_url": "https://avatars1.githubusercontent.com/u/3204379?v=4", 37 | "profile": "https://github.com/boris-graeff", 38 | "contributions": [ 39 | "code" 40 | ] 41 | }, 42 | { 43 | "login": "ciceropablo", 44 | "name": "Cícero Pablo", 45 | "avatar_url": "https://avatars3.githubusercontent.com/u/174275?v=4", 46 | "profile": "http://ciceropablo.github.io", 47 | "contributions": [ 48 | "doc" 49 | ] 50 | }, 51 | { 52 | "login": "gurpreetatwal", 53 | "name": "Gurpreet Atwal", 54 | "avatar_url": "https://avatars1.githubusercontent.com/u/7547554?v=4", 55 | "profile": "https://gatwal.com", 56 | "contributions": [ 57 | "test" 58 | ] 59 | }, 60 | { 61 | "login": "JakubKoralewski", 62 | "name": "Jakub Koralewski", 63 | "avatar_url": "https://avatars0.githubusercontent.com/u/43069023?v=4", 64 | "profile": "https://jcubed.me", 65 | "contributions": [ 66 | "code" 67 | ] 68 | }, 69 | { 70 | "login": "jankeesvw", 71 | "name": "Jankees van Woezik", 72 | "avatar_url": "https://avatars0.githubusercontent.com/u/167882?v=4", 73 | "profile": "http://jankeesvw.com", 74 | "contributions": [ 75 | "doc" 76 | ] 77 | }, 78 | { 79 | "login": "jofftiquez", 80 | "name": "Jofferson Ramirez Tiquez", 81 | "avatar_url": "https://avatars2.githubusercontent.com/u/8638243?v=4", 82 | "profile": "https://randomcodetips.com", 83 | "contributions": [ 84 | "doc" 85 | ] 86 | }, 87 | { 88 | "login": "DevoidCoding", 89 | "name": "Jordan Deprez", 90 | "avatar_url": "https://avatars1.githubusercontent.com/u/21159634?v=4", 91 | "profile": "https://github.com/DevoidCoding", 92 | "contributions": [ 93 | "doc" 94 | ] 95 | }, 96 | { 97 | "login": "juanvillegas", 98 | "name": "Juan Villegas", 99 | "avatar_url": "https://avatars3.githubusercontent.com/u/773149?v=4", 100 | "profile": "https://github.com/juanvillegas", 101 | "contributions": [ 102 | "doc" 103 | ] 104 | }, 105 | { 106 | "login": "jrast", 107 | "name": "Jürg Rast", 108 | "avatar_url": "https://avatars3.githubusercontent.com/u/146369?v=4", 109 | "profile": "http://jrast.ch", 110 | "contributions": [ 111 | "code" 112 | ] 113 | }, 114 | { 115 | "login": "antixrist", 116 | "name": "Kartashov Alexey", 117 | "avatar_url": "https://avatars3.githubusercontent.com/u/2387592?v=4", 118 | "profile": "https://github.com/antixrist", 119 | "contributions": [ 120 | "code" 121 | ] 122 | }, 123 | { 124 | "login": "leonardpauli", 125 | "name": "Leonard Pauli", 126 | "avatar_url": "https://avatars0.githubusercontent.com/u/1329834?v=4", 127 | "profile": "http://twitter.com/LeonardPauli", 128 | "contributions": [ 129 | "code", 130 | "doc" 131 | ] 132 | }, 133 | { 134 | "login": "nelsliu9121", 135 | "name": "Nelson Liu", 136 | "avatar_url": "https://avatars2.githubusercontent.com/u/1268682?v=4", 137 | "profile": "https://github.com/nelsliu9121", 138 | "contributions": [ 139 | "code", 140 | "doc", 141 | "test" 142 | ] 143 | }, 144 | { 145 | "login": "NLNicoo", 146 | "name": "Nico", 147 | "avatar_url": "https://avatars2.githubusercontent.com/u/6526666?v=4", 148 | "profile": "https://github.com/NLNicoo", 149 | "contributions": [ 150 | "code", 151 | "test" 152 | ] 153 | }, 154 | { 155 | "login": "qkdreyer", 156 | "name": "Quentin Dreyer", 157 | "avatar_url": "https://avatars3.githubusercontent.com/u/717869?v=4", 158 | "profile": "https://www.qkdreyer.dev", 159 | "contributions": [ 160 | "code" 161 | ] 162 | }, 163 | { 164 | "login": "raphaelsaunier", 165 | "name": "Raphael Saunier", 166 | "avatar_url": "https://avatars2.githubusercontent.com/u/170256?v=4", 167 | "profile": "http://raphaelsaunier.com", 168 | "contributions": [ 169 | "code" 170 | ] 171 | }, 172 | { 173 | "login": "rodneyrehm", 174 | "name": "Rodney Rehm", 175 | "avatar_url": "https://avatars3.githubusercontent.com/u/186837?v=4", 176 | "profile": "http://rodneyrehm.de", 177 | "contributions": [ 178 | "code", 179 | "test" 180 | ] 181 | }, 182 | { 183 | "login": "wongyouth", 184 | "name": "Ryan Wang", 185 | "avatar_url": "https://avatars1.githubusercontent.com/u/944583?v=4", 186 | "profile": "http://wongyouth.github.io", 187 | "contributions": [ 188 | "code", 189 | "doc", 190 | "test" 191 | ] 192 | }, 193 | { 194 | "login": "Atinux", 195 | "name": "Sébastien Chopin", 196 | "avatar_url": "https://avatars2.githubusercontent.com/u/904724?v=4", 197 | "profile": "https://atinux.com", 198 | "contributions": [ 199 | "doc" 200 | ] 201 | }, 202 | { 203 | "login": "zgayjjf", 204 | "name": "jeffjing", 205 | "avatar_url": "https://avatars1.githubusercontent.com/u/24718872?v=4", 206 | "profile": "https://github.com/zgayjjf", 207 | "contributions": [ 208 | "code" 209 | ] 210 | }, 211 | { 212 | "login": "macarthuror", 213 | "name": "macarthuror", 214 | "avatar_url": "https://avatars0.githubusercontent.com/u/24395219?v=4", 215 | "profile": "https://github.com/macarthuror", 216 | "contributions": [ 217 | "doc" 218 | ] 219 | }, 220 | { 221 | "login": "gangsthub", 222 | "name": "Paul Melero", 223 | "avatar_url": "https://avatars2.githubusercontent.com/u/6775220?s=460&v=4", 224 | "profile": "https://github.com/gangsthub", 225 | "contributions": [ 226 | "doc", 227 | "code", 228 | "test" 229 | ] 230 | }, 231 | { 232 | "login": "WTDuck", 233 | "name": "Guillaume da Silva", 234 | "avatar_url": "https://avatars0.githubusercontent.com/u/16686729?v=4", 235 | "profile": "https://github.com/WTDuck", 236 | "contributions": [ 237 | "code" 238 | ] 239 | }, 240 | { 241 | "login": "SanterreJo", 242 | "name": "Jonathan Santerre", 243 | "avatar_url": "https://avatars2.githubusercontent.com/u/6465769?v=4", 244 | "profile": "https://github.com/SanterreJo", 245 | "contributions": [ 246 | "code" 247 | ] 248 | }, 249 | { 250 | "login": "fabiofdsantos", 251 | "name": "Fábio Santos", 252 | "avatar_url": "https://avatars3.githubusercontent.com/u/8303937?v=4", 253 | "profile": "https://www.linkedin.com/in/fabiofdsantos/", 254 | "contributions": [ 255 | "doc" 256 | ] 257 | }, 258 | { 259 | "login": "robertgr991", 260 | "name": "robertgr991", 261 | "avatar_url": "https://avatars0.githubusercontent.com/u/36689800?v=4", 262 | "profile": "https://github.com/robertgr991", 263 | "contributions": [ 264 | "code" 265 | ] 266 | }, 267 | { 268 | "login": "YuraKolesnikov", 269 | "name": "JurijsKolesnikovs", 270 | "avatar_url": "https://avatars3.githubusercontent.com/u/28485518?v=4", 271 | "profile": "https://github.com/YuraKolesnikov", 272 | "contributions": [ 273 | "doc" 274 | ] 275 | }, 276 | { 277 | "login": "davidsbond", 278 | "name": "David Bond", 279 | "avatar_url": "https://avatars3.githubusercontent.com/u/6227720?v=4", 280 | "profile": "https://davidsbond.github.io", 281 | "contributions": [ 282 | "doc" 283 | ] 284 | }, 285 | { 286 | "login": "FreekVR", 287 | "name": "Freek van Rijt", 288 | "avatar_url": "https://avatars1.githubusercontent.com/u/417416?v=4", 289 | "profile": "http://www.freekvanrijt.nl", 290 | "contributions": [ 291 | "doc" 292 | ] 293 | }, 294 | { 295 | "login": "yachaka", 296 | "name": "Ilyes Hermellin", 297 | "avatar_url": "https://avatars2.githubusercontent.com/u/8074336?v=4", 298 | "profile": "https://github.com/yachaka", 299 | "contributions": [ 300 | "code" 301 | ] 302 | }, 303 | { 304 | "login": "peschee", 305 | "name": "Peter Siska", 306 | "avatar_url": "https://avatars1.githubusercontent.com/u/63866?v=4", 307 | "profile": "http://www.inventage.com", 308 | "contributions": [ 309 | "doc" 310 | ] 311 | }, 312 | { 313 | "login": "adm1t", 314 | "name": "Dmitry Filippov", 315 | "avatar_url": "https://avatars2.githubusercontent.com/u/26100455?v=4", 316 | "profile": "http://adm1t.github.io", 317 | "contributions": [ 318 | "doc" 319 | ] 320 | }, 321 | { 322 | "login": "retailify", 323 | "name": "Thomas Meitz", 324 | "avatar_url": "https://avatars0.githubusercontent.com/u/5236353?v=4", 325 | "profile": "https://retailify.de", 326 | "contributions": [ 327 | "doc", 328 | "test" 329 | ] 330 | }, 331 | { 332 | "login": "NeuronButter", 333 | "name": "Neeron Bhatta", 334 | "avatar_url": "https://avatars.githubusercontent.com/u/33238007?v=4", 335 | "profile": "http://neeron.me", 336 | "contributions": [ 337 | "doc" 338 | ] 339 | }, 340 | { 341 | "login": "joaoaraujo-hotmart", 342 | "name": "joaoaraujo-hotmart", 343 | "avatar_url": "https://avatars.githubusercontent.com/u/15874735?v=4", 344 | "profile": "https://github.com/joaoaraujo-hotmart", 345 | "contributions": [ 346 | "code" 347 | ] 348 | } 349 | ], 350 | "contributorsPerLine": 7, 351 | "commitConvention": "none", 352 | "repoHost": "https://github.com", 353 | "skipCi": true 354 | } 355 | -------------------------------------------------------------------------------- /.github/CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Code of Conduct 2 | 3 | As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities. 4 | 5 | We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion. 6 | 7 | Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct. 8 | 9 | Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team. 10 | 11 | Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers. 12 | 13 | This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/) 14 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | <!-- 2 | Thanks for your interest in the project. Bugs filed and PRs submitted are appreciated! 3 | 4 | Please make sure that you are familiar with and follow the Code of Conduct for 5 | this project (found in the CODE_OF_CONDUCT.md file). 6 | 7 | Please fill out this template with all the relevant information so we can 8 | understand what's going on and fix the issue. 9 | --> 10 | 11 | - `vuex-persistedstate` version: 12 | - `node` version: 13 | - `npm` (or `yarn`) version: 14 | 15 | Relevant code or config 16 | 17 | ```javascript 18 | ``` 19 | 20 | What you did: 21 | 22 | What happened: 23 | 24 | <!-- Please provide the full error message/screenshots/anything --> 25 | 26 | Reproduction sandbox: 27 | 28 | <!-- 29 | If possible, please create a CodeSandbox or Fiddle that reproduces the issue with the 30 | minimal amount of code possible. 31 | 32 | When trying to report a bug but do not not provide a reproducible CodeSandbox or Fiddle, 33 | the issue will be closed without further notice. 34 | --> 35 | 36 | Problem description: 37 | 38 | Suggested solution: 39 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | <!-- 2 | Thanks for your interest in the project. Bugs filed and PRs submitted are appreciated! 3 | 4 | Please make sure that you are familiar with and follow the Code of Conduct for 5 | this project (found in the CODE_OF_CONDUCT.md file). 6 | 7 | Also, please make sure you're familiar with and follow the instructions in the 8 | contributing guidelines (found in the CONTRIBUTING.md file). 9 | 10 | Please fill out the information below to expedite the review and (hopefully) 11 | merge of your pull request! 12 | --> 13 | 14 | <!-- What changes are being made? (What feature/bug is being fixed here?) --> 15 | 16 | **What**: 17 | 18 | <!-- Why are these changes necessary? --> 19 | 20 | **Why**: 21 | 22 | <!-- How were these changes implemented? --> 23 | 24 | **How**: 25 | 26 | <!-- Have you done all of these things? --> 27 | 28 | **Checklist**: 29 | 30 | <!-- add "N/A" to the end of each line that's irrelevant to your changes --> 31 | <!-- to check an item, place an "x" in the box like so: "- [x] Documentation" --> 32 | 33 | - [ ] Documentation 34 | - [ ] Tests 35 | - [ ] Ready to be merged 36 | <!-- In your opinion, is this ready to be merged as soon as it's reviewed? --> 37 | - [ ] Added myself to contributors table 38 | <!-- this is optional, see the contributing guidelines for instructions --> 39 | 40 | <!-- feel free to add additional comments --> 41 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: "npm" 4 | directory: "/" 5 | schedule: 6 | interval: "weekly" 7 | -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: test 2 | 3 | on: 4 | push: { branches: [master, 4.x.x] } 5 | pull_request: { branches: [master, 4.x.x] } 6 | 7 | jobs: 8 | test: 9 | # ignore all-contributors PRs 10 | if: ${{ !contains(github.head_ref, 'all-contributors') }} 11 | 12 | name: Test 13 | runs-on: ubuntu-latest 14 | 15 | steps: 16 | - name: 🛑 Cancel Previous Runs 17 | uses: styfle/cancel-workflow-action@0.6.0 18 | with: 19 | access_token: ${{ secrets.GITHUB_TOKEN }} 20 | 21 | - name: ⬇️ Checkout repo 22 | uses: actions/checkout@v2 23 | 24 | - name: ⎔ Setup node 25 | uses: actions/setup-node@v1 26 | with: 27 | node-version: 14 28 | 29 | - name: 📥 Download deps 30 | uses: bahmutov/npm-install@v1 31 | with: 32 | useLockFile: false 33 | 34 | - name: 🏗 Run build script 35 | run: npm run build 36 | 37 | - name: ▶️ Run test script 38 | run: npm run test 39 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /*.log 2 | /.rts2_cache_*/ 3 | /dist/ 4 | /node_modules/ 5 | 6 | # these cause more harm than good 7 | # when working with contributors 8 | /package-lock.json 9 | /yarn.lock 10 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | This project adheres to [Semantic Versioning](http://semver.org/). 4 | Every release is documented on the Github [Releases](https://github.com/robinvdvleuten/vuex-persistedstate/releases) page. 5 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | Thanks for being willing to contribute! 4 | 5 | **Working on your first Pull Request?** You can learn how from this _free_ 6 | series [How to Contribute to an Open Source Project on GitHub][egghead] 7 | 8 | ## Project setup 9 | 10 | 1. Fork and clone the repo 11 | 2. Run `npm install` to install dependencies. 12 | 3. Create a branch for your PR with `git checkout -b your-branch-name` 13 | 14 | > Tip: Keep your `master` branch pointing at the original repository and make 15 | > pull requests from branches on your fork. To do this, run: 16 | > 17 | > ``` 18 | > git remote add upstream https://github.com/robinvdvleuten/vuex-persistedstate.git 19 | > git fetch upstream 20 | > git branch --set-upstream-to=upstream/master master 21 | > ``` 22 | > 23 | > This will add the original repository as a "remote" called "upstream," Then 24 | > fetch the git information from that remote, then set your local `master` 25 | > branch to use the upstream master branch whenever you run `git pull`. Then you 26 | > can make all of your pull request branches based on this `master` branch. 27 | > Whenever you want to update your version of `master`, do a regular `git pull`. 28 | 29 | ## Committing and Pushing changes 30 | 31 | Please make sure to run the tests before you commit your changes. 32 | 33 | ## Help needed 34 | 35 | Please checkout the [the open issues][issues] 36 | 37 | Also, please watch the repo and respond to questions/bug reports/feature 38 | requests! Thanks! 39 | 40 | [egghead]: https://egghead.io/series/how-to-contribute-to-an-open-source-project-on-github 41 | [issues]: https://github.com/robinvdvleuten/vuex-persistedstate/issues 42 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Robin van der Vleuten <robin@webstronauts.co> 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # vuex-persistedstate 2 | 3 | Persist and rehydrate your [Vuex](http://vuex.vuejs.org/) state between page reloads. 4 | 5 | <hr /> 6 | 7 | > 🚨 Not maintained anymore! As I don't use Vue in my day to day work, it becomes very hard to stay up to date with any changes with things like Vuex, Nuxt.js and other tools used by the community. That's why I decided to stop spending my spare time to this repository. Feel free to reach out if you would like to take over ownership of the package on NPM. Thank you for any contribution any of you had made to this project 🙏. 8 | 9 | <hr /> 10 | 11 | [](https://github.com/robinvdvleuten/vuex-persistedstate/actions?query=workflow%3Atest) 12 | [](https://www.npmjs.com/package/vuex-persistedstate) 13 | [](https://www.npmjs.com/package/vuex-persistedstate) 14 | [](https://github.com/prettier/prettier) 15 | [](https://github.com/robinvdvleuten/vuex-persistedstate/blob/master/LICENSE) 16 | 17 | [](http://makeapullrequest.com) 18 | [](https://github.com/robinvdvleuten/vuex-persistedstate/blob/master/.github/CODE_OF_CONDUCT.md) 19 | 20 | <a href="https://webstronauts.com/"> 21 | <img src="https://webstronauts.com/badges/sponsored-by-webstronauts.svg" alt="Sponsored by The Webstronauts" width="200" height="65"> 22 | </a> 23 | 24 | ## Install 25 | 26 | ```bash 27 | npm install --save vuex-persistedstate 28 | ``` 29 | 30 | The [UMD](https://github.com/umdjs/umd) build is also available on [unpkg](https://unpkg.com): 31 | 32 | ```html 33 | <script src="https://unpkg.com/vuex-persistedstate/dist/vuex-persistedstate.umd.js"></script> 34 | ``` 35 | 36 | You can find the library on `window.createPersistedState`. 37 | 38 | ## Usage 39 | 40 | ```js 41 | import { createStore } from "vuex"; 42 | import createPersistedState from "vuex-persistedstate"; 43 | 44 | const store = createStore({ 45 | // ... 46 | plugins: [createPersistedState()], 47 | }); 48 | ``` 49 | 50 | For usage with for Vuex 3 and Vue 2, please see [3.x.x branch](https://github.com/robinvdvleuten/vuex-persistedstate/tree/3.x.x). 51 | 52 | ## Examples 53 | 54 | Check out a basic example on [CodeSandbox](https://codesandbox.io). 55 | 56 | [](https://codesandbox.io/s/80k4m2598) 57 | 58 | Or configured to use with [js-cookie](https://github.com/js-cookie/js-cookie). 59 | 60 | [](https://codesandbox.io/s/xl356qvvkz) 61 | 62 | Or configured to use with [secure-ls](https://github.com/softvar/secure-ls) 63 | 64 | [](https://codesandbox.io/s/vuex-persistedstate-with-secure-ls-encrypted-data-7l9wb?fontsize=14) 65 | 66 | ### Example with Vuex modules 67 | 68 | New plugin instances can be created in separate files, but must be imported and added to plugins object in the main Vuex file. 69 | 70 | ```js 71 | /* module.js */ 72 | export const dataStore = { 73 | state: { 74 | data: [] 75 | } 76 | } 77 | 78 | /* store.js */ 79 | import { dataStore } from './module' 80 | 81 | const dataState = createPersistedState({ 82 | paths: ['data'] 83 | }) 84 | 85 | export new Vuex.Store({ 86 | modules: { 87 | dataStore 88 | }, 89 | plugins: [dataState] 90 | }) 91 | ``` 92 | 93 | ### Example with Nuxt.js 94 | 95 | It is possible to use vuex-persistedstate with Nuxt.js. It must be included as a NuxtJS plugin: 96 | 97 | #### With local storage (client-side only) 98 | 99 | ```javascript 100 | // nuxt.config.js 101 | 102 | ... 103 | /* 104 | * Naming your plugin 'xxx.client.js' will make it execute only on the client-side. 105 | * https://nuxtjs.org/guide/plugins/#name-conventional-plugin 106 | */ 107 | plugins: [{ src: '~/plugins/persistedState.client.js' }] 108 | ... 109 | ``` 110 | 111 | ```javascript 112 | // ~/plugins/persistedState.client.js 113 | 114 | import createPersistedState from 'vuex-persistedstate' 115 | 116 | export default ({store}) => { 117 | createPersistedState({ 118 | key: 'yourkey', 119 | paths: [...] 120 | ... 121 | })(store) 122 | } 123 | ``` 124 | 125 | #### Using cookies (universal client + server-side) 126 | 127 | Add `cookie` and `js-cookie`: 128 | 129 | `npm install --save cookie js-cookie` 130 | or `yarn add cookie js-cookie` 131 | 132 | ```javascript 133 | // nuxt.config.js 134 | ... 135 | plugins: [{ src: '~/plugins/persistedState.js'}] 136 | ... 137 | ``` 138 | 139 | ```javascript 140 | // ~/plugins/persistedState.js 141 | 142 | import createPersistedState from 'vuex-persistedstate'; 143 | import * as Cookies from 'js-cookie'; 144 | import cookie from 'cookie'; 145 | 146 | export default ({ store, req }) => { 147 | createPersistedState({ 148 | paths: [...], 149 | storage: { 150 | getItem: (key) => { 151 | // See https://nuxtjs.org/guide/plugins/#using-process-flags 152 | if (process.server) { 153 | const parsedCookies = cookie.parse(req.headers.cookie); 154 | return parsedCookies[key]; 155 | } else { 156 | return Cookies.get(key); 157 | } 158 | }, 159 | // Please see https://github.com/js-cookie/js-cookie#json, on how to handle JSON. 160 | setItem: (key, value) => 161 | Cookies.set(key, value, { expires: 365, secure: false }), 162 | removeItem: key => Cookies.remove(key) 163 | } 164 | })(store); 165 | }; 166 | ``` 167 | 168 | ## API 169 | 170 | ### `createPersistedState([options])` 171 | 172 | Creates a new instance of the plugin with the given options. The following options 173 | can be provided to configure the plugin for your specific needs: 174 | 175 | - `key <String>`: The key to store the persisted state under. Defaults to `vuex`. 176 | - `paths <Array>`: An array of any paths to partially persist the state. If no paths are given, the complete state is persisted. If an empty array is given, no state is persisted. Paths must be specified using dot notation. If using modules, include the module name. eg: "auth.user" Defaults to `undefined`. 177 | - `reducer <Function>`: A function that will be called to reduce the state to persist based on the given paths. Defaults to include the values. 178 | - `subscriber <Function>`: A function called to setup mutation subscription. Defaults to `store => handler => store.subscribe(handler)`. 179 | 180 | - `storage <Object>`: Instead of (or in combination with) `getState` and `setState`. Defaults to localStorage. 181 | - `getState <Function>`: A function that will be called to rehydrate a previously persisted state. Defaults to using `storage`. 182 | - `setState <Function>`: A function that will be called to persist the given state. Defaults to using `storage`. 183 | - `filter <Function>`: A function that will be called to filter any mutations which will trigger `setState` on storage eventually. Defaults to `() => true`. 184 | - `overwrite <Boolean>`: When rehydrating, whether to overwrite the existing state with the output from `getState` directly, instead of merging the two objects with `deepmerge`. Defaults to `false`. 185 | - `arrayMerger <Function>`: A function for merging arrays when rehydrating state. Defaults to `function (store, saved) { return saved }` (saved state replaces supplied state). 186 | - `rehydrated <Function>`: A function that will be called when the rehydration is finished. Useful when you are using Nuxt.js, which the rehydration of the persisted state happens asynchronously. Defaults to `store => {}` 187 | - `fetchBeforeUse <Boolean>`: A boolean indicating if the state should be fetched from storage before the plugin is used. Defaults to `false`. 188 | - `assertStorage <Function>`: An overridable function to ensure storage is available, fired on plugins's initialization. Default one is performing a Write-Delete operation on the given Storage instance. Note, default behaviour could throw an error (like `DOMException: QuotaExceededError`). 189 | 190 | ## Customize Storage 191 | 192 | If it's not ideal to have the state of the Vuex store inside localstorage. One can easily implement the functionality to use [cookies](https://github.com/js-cookie/js-cookie) for that (or any other you can think of); 193 | 194 | [](https://codesandbox.io/s/xl356qvvkz?autoresize=1) 195 | 196 | ```js 197 | import { Store } from "vuex"; 198 | import createPersistedState from "vuex-persistedstate"; 199 | import * as Cookies from "js-cookie"; 200 | 201 | const store = new Store({ 202 | // ... 203 | plugins: [ 204 | createPersistedState({ 205 | storage: { 206 | getItem: (key) => Cookies.get(key), 207 | // Please see https://github.com/js-cookie/js-cookie#json, on how to handle JSON. 208 | setItem: (key, value) => 209 | Cookies.set(key, value, { expires: 3, secure: true }), 210 | removeItem: (key) => Cookies.remove(key), 211 | }, 212 | }), 213 | ], 214 | }); 215 | ``` 216 | 217 | In fact, any object following the Storage protocol (getItem, setItem, removeItem, etc) could be passed: 218 | 219 | ```js 220 | createPersistedState({ storage: window.sessionStorage }); 221 | ``` 222 | 223 | This is especially useful when you are using this plugin in combination with server-side rendering, where one could pass an instance of [dom-storage](https://www.npmjs.com/package/dom-storage). 224 | 225 | ### 🔐Obfuscate Local Storage 226 | 227 | If you need to use **Local Storage** (or you want to) but want to prevent attackers from easily inspecting the stored data, you can [obfuscate it]('https://github.com/softvar/secure-ls'). 228 | 229 | **Important ⚠️** Obfuscating the Vuex store means to prevent attackers from easily gaining access to the data. This is not a secure way of storing sensitive data (like passwords, personal information, etc.), and always needs to be used in conjunction with some other authentication method of keeping the data (such as Firebase or your own server). 230 | 231 | [](https://codesandbox.io/s/vuex-persistedstate-with-secure-ls-encrypted-data-7l9wb?fontsize=14) 232 | 233 | ```js 234 | import { Store } from "vuex"; 235 | import createPersistedState from "vuex-persistedstate"; 236 | import SecureLS from "secure-ls"; 237 | var ls = new SecureLS({ isCompression: false }); 238 | 239 | // https://github.com/softvar/secure-ls 240 | 241 | const store = new Store({ 242 | // ... 243 | plugins: [ 244 | createPersistedState({ 245 | storage: { 246 | getItem: (key) => ls.get(key), 247 | setItem: (key, value) => ls.set(key, value), 248 | removeItem: (key) => ls.remove(key), 249 | }, 250 | }), 251 | ], 252 | }); 253 | ``` 254 | 255 | ### ⚠️ LocalForage ⚠️ 256 | 257 | As it maybe seems at first sight, it's not possible to pass a [LocalForage](https://github.com/localForage/localForage) instance as `storage` property. This is due the fact that all getters and setters must be synchronous and [LocalForage's methods](https://github.com/localForage/localForage#callbacks-vs-promises) are asynchronous. 258 | 259 | ## Changelog 260 | 261 | Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. 262 | 263 | ## Contributors ✨ 264 | 265 | Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)): 266 | 267 | <!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section --> 268 | <!-- prettier-ignore-start --> 269 | <!-- markdownlint-disable --> 270 | <table> 271 | <tr> 272 | <td align="center"><a href="https://robinvdvleuten.nl"><img src="https://avatars3.githubusercontent.com/u/238295?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Robin van der Vleuten</b></sub></a><br /><a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=robinvdvleuten" title="Code">💻</a> <a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=robinvdvleuten" title="Documentation">📖</a> <a href="#infra-robinvdvleuten" title="Infrastructure (Hosting, Build-Tools, etc)">🚇</a> <a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=robinvdvleuten" title="Tests">⚠️</a></td> 273 | <td align="center"><a href="https://github.com/zweizeichen"><img src="https://avatars1.githubusercontent.com/u/654071?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Sebastian</b></sub></a><br /><a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=zweizeichen" title="Code">💻</a> <a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=zweizeichen" title="Documentation">📖</a></td> 274 | <td align="center"><a href="https://github.com/boris-graeff"><img src="https://avatars1.githubusercontent.com/u/3204379?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Boris Graeff</b></sub></a><br /><a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=boris-graeff" title="Code">💻</a></td> 275 | <td align="center"><a href="http://ciceropablo.github.io"><img src="https://avatars3.githubusercontent.com/u/174275?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Cícero Pablo</b></sub></a><br /><a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=ciceropablo" title="Documentation">📖</a></td> 276 | <td align="center"><a href="https://gatwal.com"><img src="https://avatars1.githubusercontent.com/u/7547554?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Gurpreet Atwal</b></sub></a><br /><a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=gurpreetatwal" title="Tests">⚠️</a></td> 277 | <td align="center"><a href="https://jcubed.me"><img src="https://avatars0.githubusercontent.com/u/43069023?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Jakub Koralewski</b></sub></a><br /><a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=JakubKoralewski" title="Code">💻</a></td> 278 | <td align="center"><a href="http://jankeesvw.com"><img src="https://avatars0.githubusercontent.com/u/167882?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Jankees van Woezik</b></sub></a><br /><a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=jankeesvw" title="Documentation">📖</a></td> 279 | </tr> 280 | <tr> 281 | <td align="center"><a href="https://randomcodetips.com"><img src="https://avatars2.githubusercontent.com/u/8638243?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Jofferson Ramirez Tiquez</b></sub></a><br /><a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=jofftiquez" title="Documentation">📖</a></td> 282 | <td align="center"><a href="https://github.com/DevoidCoding"><img src="https://avatars1.githubusercontent.com/u/21159634?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Jordan Deprez</b></sub></a><br /><a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=DevoidCoding" title="Documentation">📖</a></td> 283 | <td align="center"><a href="https://github.com/juanvillegas"><img src="https://avatars3.githubusercontent.com/u/773149?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Juan Villegas</b></sub></a><br /><a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=juanvillegas" title="Documentation">📖</a></td> 284 | <td align="center"><a href="http://jrast.ch"><img src="https://avatars3.githubusercontent.com/u/146369?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Jürg Rast</b></sub></a><br /><a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=jrast" title="Code">💻</a></td> 285 | <td align="center"><a href="https://github.com/antixrist"><img src="https://avatars3.githubusercontent.com/u/2387592?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Kartashov Alexey</b></sub></a><br /><a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=antixrist" title="Code">💻</a></td> 286 | <td align="center"><a href="http://twitter.com/LeonardPauli"><img src="https://avatars0.githubusercontent.com/u/1329834?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Leonard Pauli</b></sub></a><br /><a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=leonardpauli" title="Code">💻</a> <a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=leonardpauli" title="Documentation">📖</a></td> 287 | <td align="center"><a href="https://github.com/nelsliu9121"><img src="https://avatars2.githubusercontent.com/u/1268682?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Nelson Liu</b></sub></a><br /><a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=nelsliu9121" title="Code">💻</a> <a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=nelsliu9121" title="Documentation">📖</a> <a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=nelsliu9121" title="Tests">⚠️</a></td> 288 | </tr> 289 | <tr> 290 | <td align="center"><a href="https://github.com/NLNicoo"><img src="https://avatars2.githubusercontent.com/u/6526666?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Nico</b></sub></a><br /><a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=NLNicoo" title="Code">💻</a> <a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=NLNicoo" title="Tests">⚠️</a></td> 291 | <td align="center"><a href="https://www.qkdreyer.dev"><img src="https://avatars3.githubusercontent.com/u/717869?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Quentin Dreyer</b></sub></a><br /><a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=qkdreyer" title="Code">💻</a></td> 292 | <td align="center"><a href="http://raphaelsaunier.com"><img src="https://avatars2.githubusercontent.com/u/170256?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Raphael Saunier</b></sub></a><br /><a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=raphaelsaunier" title="Code">💻</a></td> 293 | <td align="center"><a href="http://rodneyrehm.de"><img src="https://avatars3.githubusercontent.com/u/186837?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Rodney Rehm</b></sub></a><br /><a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=rodneyrehm" title="Code">💻</a> <a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=rodneyrehm" title="Tests">⚠️</a></td> 294 | <td align="center"><a href="http://wongyouth.github.io"><img src="https://avatars1.githubusercontent.com/u/944583?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Ryan Wang</b></sub></a><br /><a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=wongyouth" title="Code">💻</a> <a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=wongyouth" title="Documentation">📖</a> <a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=wongyouth" title="Tests">⚠️</a></td> 295 | <td align="center"><a href="https://atinux.com"><img src="https://avatars2.githubusercontent.com/u/904724?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Sébastien Chopin</b></sub></a><br /><a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=Atinux" title="Documentation">📖</a></td> 296 | <td align="center"><a href="https://github.com/zgayjjf"><img src="https://avatars1.githubusercontent.com/u/24718872?v=4?s=100" width="100px;" alt=""/><br /><sub><b>jeffjing</b></sub></a><br /><a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=zgayjjf" title="Code">💻</a></td> 297 | </tr> 298 | <tr> 299 | <td align="center"><a href="https://github.com/macarthuror"><img src="https://avatars0.githubusercontent.com/u/24395219?v=4?s=100" width="100px;" alt=""/><br /><sub><b>macarthuror</b></sub></a><br /><a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=macarthuror" title="Documentation">📖</a></td> 300 | <td align="center"><a href="https://github.com/gangsthub"><img src="https://avatars2.githubusercontent.com/u/6775220?s=460&v=4?s=100" width="100px;" alt=""/><br /><sub><b>Paul Melero</b></sub></a><br /><a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=gangsthub" title="Documentation">📖</a> <a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=gangsthub" title="Code">💻</a> <a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=gangsthub" title="Tests">⚠️</a></td> 301 | <td align="center"><a href="https://github.com/WTDuck"><img src="https://avatars0.githubusercontent.com/u/16686729?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Guillaume da Silva</b></sub></a><br /><a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=WTDuck" title="Code">💻</a></td> 302 | <td align="center"><a href="https://github.com/SanterreJo"><img src="https://avatars2.githubusercontent.com/u/6465769?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Jonathan Santerre</b></sub></a><br /><a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=SanterreJo" title="Code">💻</a></td> 303 | <td align="center"><a href="https://www.linkedin.com/in/fabiofdsantos/"><img src="https://avatars3.githubusercontent.com/u/8303937?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Fábio Santos</b></sub></a><br /><a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=fabiofdsantos" title="Documentation">📖</a></td> 304 | <td align="center"><a href="https://github.com/robertgr991"><img src="https://avatars0.githubusercontent.com/u/36689800?v=4?s=100" width="100px;" alt=""/><br /><sub><b>robertgr991</b></sub></a><br /><a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=robertgr991" title="Code">💻</a></td> 305 | <td align="center"><a href="https://github.com/YuraKolesnikov"><img src="https://avatars3.githubusercontent.com/u/28485518?v=4?s=100" width="100px;" alt=""/><br /><sub><b>JurijsKolesnikovs</b></sub></a><br /><a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=YuraKolesnikov" title="Documentation">📖</a></td> 306 | </tr> 307 | <tr> 308 | <td align="center"><a href="https://davidsbond.github.io"><img src="https://avatars3.githubusercontent.com/u/6227720?v=4?s=100" width="100px;" alt=""/><br /><sub><b>David Bond</b></sub></a><br /><a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=davidsbond" title="Documentation">📖</a></td> 309 | <td align="center"><a href="http://www.freekvanrijt.nl"><img src="https://avatars1.githubusercontent.com/u/417416?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Freek van Rijt</b></sub></a><br /><a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=FreekVR" title="Documentation">📖</a></td> 310 | <td align="center"><a href="https://github.com/yachaka"><img src="https://avatars2.githubusercontent.com/u/8074336?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Ilyes Hermellin</b></sub></a><br /><a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=yachaka" title="Code">💻</a></td> 311 | <td align="center"><a href="http://www.inventage.com"><img src="https://avatars1.githubusercontent.com/u/63866?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Peter Siska</b></sub></a><br /><a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=peschee" title="Documentation">📖</a></td> 312 | <td align="center"><a href="http://adm1t.github.io"><img src="https://avatars2.githubusercontent.com/u/26100455?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Dmitry Filippov</b></sub></a><br /><a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=adm1t" title="Documentation">📖</a></td> 313 | <td align="center"><a href="https://retailify.de"><img src="https://avatars0.githubusercontent.com/u/5236353?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Thomas Meitz</b></sub></a><br /><a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=retailify" title="Documentation">📖</a> <a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=retailify" title="Tests">⚠️</a></td> 314 | <td align="center"><a href="http://neeron.me"><img src="https://avatars.githubusercontent.com/u/33238007?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Neeron Bhatta</b></sub></a><br /><a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=NeuronButter" title="Documentation">📖</a></td> 315 | </tr> 316 | <tr> 317 | <td align="center"><a href="https://github.com/joaoaraujo-hotmart"><img src="https://avatars.githubusercontent.com/u/15874735?v=4?s=100" width="100px;" alt=""/><br /><sub><b>joaoaraujo-hotmart</b></sub></a><br /><a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=joaoaraujo-hotmart" title="Code">💻</a></td> 318 | </tr> 319 | </table> 320 | 321 | <!-- markdownlint-restore --> 322 | <!-- prettier-ignore-end --> 323 | 324 | <!-- ALL-CONTRIBUTORS-LIST:END --> 325 | 326 | This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome! 327 | 328 | ## License 329 | 330 | The MIT License (MIT). Please see [License File](LICENSE) for more information. 331 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "vuex-persistedstate", 3 | "description": "Persist and rehydrate your Vuex state between page reloads.", 4 | "version": "4.1.0", 5 | "license": "MIT", 6 | "author": "Robin van der Vleuten <robin@webstronauts.co> (robinvdvleuten.nl)", 7 | "keywords": [ 8 | "vue", 9 | "vuex", 10 | "plugin" 11 | ], 12 | "homepage": "https://github.com/robinvdvleuten/vuex-persistedstate#readme", 13 | "repository": "robinvdvleuten/vuex-persistedstate", 14 | "bugs": { 15 | "url": "https://github.com/robinvdvleuten/vuex-persistedstate/issues" 16 | }, 17 | "source": "src/index.ts", 18 | "main": "dist/vuex-persistedstate.js", 19 | "module": "dist/vuex-persistedstate.es.js", 20 | "unpkg": "dist/vuex-persistedstate.umd.js", 21 | "types": "dist/index.d.ts", 22 | "files": [ 23 | "dist", 24 | "src" 25 | ], 26 | "scripts": { 27 | "build": "rimraf dist && microbundle --external all --name createPersistedState", 28 | "prepare": "npm run build", 29 | "test": "npm-run-all test:**", 30 | "test:jest": "jest --env=jsdom", 31 | "test:size": "bundlesize" 32 | }, 33 | "babel": { 34 | "presets": [ 35 | "@babel/preset-env" 36 | ] 37 | }, 38 | "bundlesize": [ 39 | { 40 | "path": "./dist/*.js", 41 | "threshold": "800b" 42 | } 43 | ], 44 | "husky": { 45 | "hooks": { 46 | "pre-commit": "npm run build && pretty-quick --staged" 47 | } 48 | }, 49 | "jest": { 50 | "testURL": "http://localhost/" 51 | }, 52 | "dependencies": { 53 | "deepmerge": "^4.2.2", 54 | "shvl": "^2.0.3" 55 | }, 56 | "devDependencies": { 57 | "@babel/core": "^7.12.10", 58 | "@babel/preset-env": "^7.12.11", 59 | "all-contributors-cli": "^6.19.0", 60 | "babel-core": "^7.0.0-bridge.0", 61 | "babel-jest": "^27.0.2", 62 | "bundlesize": "^0.18.1", 63 | "dom-storage": "^2.0.2", 64 | "eslint": "^8.0.0", 65 | "husky": "^7.0.0", 66 | "jest": "^27.0.6", 67 | "microbundle": "^0.14.0", 68 | "npm-run-all": "^4.1.2", 69 | "prettier": "^2.2.1", 70 | "pretty-quick": "^3.1.0", 71 | "rimraf": "^3.0.0", 72 | "vue": "^3.0.0", 73 | "vuex": "^4.0.0-rc" 74 | }, 75 | "peerDependencies": { 76 | "vuex": "^3.0 || ^4.0.0-rc" 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | import { Store, MutationPayload } from "vuex"; 2 | import merge from "deepmerge"; 3 | import * as shvl from "shvl"; 4 | 5 | interface Storage { 6 | getItem: (key: string) => any; 7 | setItem: (key: string, value: any) => void; 8 | removeItem: (key: string) => void; 9 | } 10 | 11 | interface Options<State> { 12 | key?: string; 13 | paths?: string[]; 14 | reducer?: (state: State, paths: string[]) => object; 15 | subscriber?: ( 16 | store: Store<State> 17 | ) => (handler: (mutation: any, state: State) => void) => void; 18 | storage?: Storage; 19 | getState?: (key: string, storage: Storage) => any; 20 | setState?: (key: string, state: any, storage: Storage) => void; 21 | filter?: (mutation: MutationPayload) => boolean; 22 | arrayMerger?: (state: any[], saved: any[]) => any; 23 | rehydrated?: (store: Store<State>) => void; 24 | fetchBeforeUse?: boolean; 25 | overwrite?: boolean; 26 | assertStorage?: (storage: Storage) => void | Error; 27 | } 28 | 29 | export default function <State>( 30 | options?: Options<State> 31 | ): (store: Store<State>) => void { 32 | options = options || {}; 33 | 34 | const storage = options.storage || (window && window.localStorage); 35 | const key = options.key || "vuex"; 36 | 37 | function getState(key, storage) { 38 | const value = storage.getItem(key); 39 | 40 | try { 41 | return (typeof value === "string") 42 | ? JSON.parse(value) : (typeof value === "object") 43 | ? value : undefined; 44 | } catch (err) {} 45 | 46 | return undefined; 47 | } 48 | 49 | function filter() { 50 | return true; 51 | } 52 | 53 | function setState(key, state, storage) { 54 | return storage.setItem(key, JSON.stringify(state)); 55 | } 56 | 57 | function reducer(state, paths) { 58 | return Array.isArray(paths) 59 | ? paths.reduce(function (substate, path) { 60 | return shvl.set(substate, path, shvl.get(state, path)); 61 | }, {}) 62 | : state; 63 | } 64 | 65 | function subscriber(store) { 66 | return function (handler) { 67 | return store.subscribe(handler); 68 | }; 69 | } 70 | 71 | const assertStorage = 72 | options.assertStorage || 73 | (() => { 74 | storage.setItem("@@", 1); 75 | storage.removeItem("@@"); 76 | }); 77 | 78 | assertStorage(storage); 79 | 80 | const fetchSavedState = () => (options.getState || getState)(key, storage); 81 | 82 | let savedState; 83 | 84 | if (options.fetchBeforeUse) { 85 | savedState = fetchSavedState(); 86 | } 87 | 88 | return function (store: Store<State>) { 89 | if (!options.fetchBeforeUse) { 90 | savedState = fetchSavedState(); 91 | } 92 | 93 | if (typeof savedState === "object" && savedState !== null) { 94 | store.replaceState( 95 | options.overwrite 96 | ? savedState 97 | : merge(store.state, savedState, { 98 | arrayMerge: 99 | options.arrayMerger || 100 | function (store, saved) { 101 | return saved; 102 | }, 103 | clone: false, 104 | }) 105 | ); 106 | (options.rehydrated || function () {})(store); 107 | } 108 | 109 | (options.subscriber || subscriber)(store)(function (mutation, state) { 110 | if ((options.filter || filter)(mutation)) { 111 | (options.setState || setState)( 112 | key, 113 | (options.reducer || reducer)(state, options.paths), 114 | storage 115 | ); 116 | } 117 | }); 118 | }; 119 | } 120 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import { createStore } from "vuex"; 2 | import Storage from "dom-storage"; 3 | import createPersistedState from "./"; 4 | 5 | it("can be created with the default options", () => { 6 | window.localStorage = new Storage(); 7 | expect(() => createPersistedState()).not.toThrow(); 8 | }); 9 | 10 | it("replaces store's state and subscribes to changes when initializing with JSON", () => { 11 | const storage = new Storage(); 12 | storage.setItem("vuex", JSON.stringify({ persisted: "json" })); 13 | 14 | const store = createStore({ state: { original: "state" } }); 15 | store.replaceState = jest.fn(); 16 | store.subscribe = jest.fn(); 17 | 18 | const plugin = createPersistedState({ storage }); 19 | plugin(store); 20 | 21 | expect(store.replaceState).toBeCalledWith({ 22 | original: "state", 23 | persisted: "json", 24 | }); 25 | expect(store.subscribe).toBeCalled(); 26 | }); 27 | 28 | it("replaces store's state and subscribes to changes when initializing with object", () => { 29 | const storage = { getItem: () => ({ persisted: "object" }) } 30 | 31 | const store = createStore({ state: { original: "state" } }); 32 | store.replaceState = jest.fn(); 33 | store.subscribe = jest.fn(); 34 | 35 | const plugin = createPersistedState({ storage, assertStorage: () => true }); 36 | plugin(store); 37 | 38 | expect(store.replaceState).toBeCalledWith({ 39 | original: "state", 40 | persisted: "object", 41 | }); 42 | expect(store.subscribe).toBeCalled(); 43 | }); 44 | 45 | it("does not replaces store's state when receiving invalid JSON", () => { 46 | const storage = new Storage(); 47 | storage.setItem("vuex", "<invalid JSON>"); 48 | 49 | const store = createStore({ state: { nested: { original: "state" } } }); 50 | store.replaceState = jest.fn(); 51 | store.subscribe = jest.fn(); 52 | 53 | const plugin = createPersistedState({ storage }); 54 | plugin(store); 55 | 56 | expect(store.replaceState).not.toBeCalled(); 57 | expect(store.subscribe).toBeCalled(); 58 | }); 59 | 60 | it("does not replaces store's state when receiving null", () => { 61 | const storage = new Storage(); 62 | storage.setItem("vuex", JSON.stringify(null)); 63 | 64 | const store = createStore({ state: { nested: { original: "state" } } }); 65 | store.replaceState = jest.fn(); 66 | store.subscribe = jest.fn(); 67 | 68 | const plugin = createPersistedState({ storage }); 69 | plugin(store); 70 | 71 | expect(store.replaceState).not.toBeCalled(); 72 | expect(store.subscribe).toBeCalled(); 73 | }); 74 | 75 | it("respects nested values when it replaces store's state on initializing", () => { 76 | const storage = new Storage(); 77 | storage.setItem("vuex", JSON.stringify({ persisted: "json" })); 78 | 79 | const store = createStore({ state: { original: "state" } }); 80 | store.replaceState = jest.fn(); 81 | store.subscribe = jest.fn(); 82 | 83 | const plugin = createPersistedState({ storage }); 84 | plugin(store); 85 | 86 | expect(store.replaceState).toBeCalledWith({ 87 | original: "state", 88 | persisted: "json", 89 | }); 90 | expect(store.subscribe).toBeCalled(); 91 | }); 92 | 93 | it("should persist the changed partial state back to serialized JSON", () => { 94 | const storage = new Storage(); 95 | const store = createStore({ state: {} }); 96 | 97 | const plugin = createPersistedState({ storage, paths: ["changed"] }); 98 | plugin(store); 99 | 100 | store._subscribers[0]("mutation", { changed: "state" }); 101 | 102 | expect(storage.getItem("vuex")).toBe(JSON.stringify({ changed: "state" })); 103 | }); 104 | 105 | it("persist the changed partial state back to serialized JSON under a configured key", () => { 106 | const storage = new Storage(); 107 | const store = createStore({ state: {} }); 108 | 109 | const plugin = createPersistedState({ 110 | storage, 111 | key: "custom", 112 | paths: ["changed"], 113 | }); 114 | plugin(store); 115 | 116 | store._subscribers[0]("mutation", { changed: "state" }); 117 | 118 | expect(storage.getItem("custom")).toBe(JSON.stringify({ changed: "state" })); 119 | }); 120 | 121 | it("persist the changed full state back to serialized JSON when no paths are given", () => { 122 | const storage = new Storage(); 123 | const store = createStore({ state: {} }); 124 | 125 | const plugin = createPersistedState({ storage }); 126 | plugin(store); 127 | 128 | store._subscribers[0]("mutation", { changed: "state" }); 129 | 130 | expect(storage.getItem("vuex")).toBe(JSON.stringify({ changed: "state" })); 131 | }); 132 | 133 | it("persist the changed partial state back to serialized JSON under a nested path", () => { 134 | const storage = new Storage(); 135 | const store = createStore({ state: {} }); 136 | 137 | const plugin = createPersistedState({ 138 | storage, 139 | paths: ["foo.bar", "bar"], 140 | }); 141 | plugin(store); 142 | 143 | store._subscribers[0]("mutation", { foo: { bar: "baz" }, bar: "baz" }); 144 | 145 | expect(storage.getItem("vuex")).toBe( 146 | JSON.stringify({ foo: { bar: "baz" }, bar: "baz" }) 147 | ); 148 | }); 149 | 150 | it("should not persist whole store if paths array is empty", () => { 151 | const storage = new Storage(); 152 | const store = createStore({ 153 | state: { original: "state" }, 154 | }); 155 | 156 | const plugin = createPersistedState({ storage, paths: [] }); 157 | plugin(store); 158 | 159 | store._subscribers[0]("mutation", { changed: "state" }); 160 | 161 | expect(storage.getItem("vuex")).toBe(JSON.stringify({})); 162 | }); 163 | 164 | it("should not persist null values", () => { 165 | const storage = new Storage(); 166 | const store = createStore({ 167 | state: { alpha: { name: null, bravo: { name: null } } }, 168 | }); 169 | 170 | const plugin = createPersistedState({ 171 | storage, 172 | paths: ["alpha.name", "alpha.bravo.name"], 173 | }); 174 | 175 | plugin(store); 176 | 177 | store._subscribers[0]("mutation", { charlie: { name: "charlie" } }); 178 | 179 | expect(storage.getItem("vuex")).toBe( 180 | JSON.stringify({ alpha: { bravo: {} } }) 181 | ); 182 | }); 183 | 184 | it("should not merge array values when rehydrating by default", () => { 185 | const storage = new Storage(); 186 | storage.setItem("vuex", JSON.stringify({ persisted: ["json"] })); 187 | 188 | const store = createStore({ state: { persisted: ["state"] } }); 189 | store.replaceState = jest.fn(); 190 | store.subscribe = jest.fn(); 191 | 192 | const plugin = createPersistedState({ storage }); 193 | plugin(store); 194 | 195 | expect(store.replaceState).toBeCalledWith({ 196 | persisted: ["json"], 197 | }); 198 | 199 | expect(store.subscribe).toBeCalled(); 200 | }); 201 | 202 | it("should not clone circular objects when rehydrating", () => { 203 | const circular = { foo: "bar" }; 204 | circular.foo = circular; 205 | 206 | const storage = new Storage(); 207 | storage.setItem("vuex", JSON.stringify({ persisted: "baz" })); 208 | 209 | const store = createStore({ state: { circular } }); 210 | store.replaceState = jest.fn(); 211 | store.subscribe = jest.fn(); 212 | 213 | const plugin = createPersistedState({ storage }); 214 | plugin(store); 215 | 216 | expect(store.replaceState).toBeCalledWith({ 217 | circular, 218 | persisted: "baz", 219 | }); 220 | 221 | expect(store.subscribe).toBeCalled(); 222 | }); 223 | 224 | it("should apply a custom arrayMerger function", () => { 225 | const storage = new Storage(); 226 | storage.setItem("vuex", JSON.stringify({ persisted: [1, 2] })); 227 | 228 | const store = createStore({ state: { persisted: [1, 2, 3] } }); 229 | store.replaceState = jest.fn(); 230 | store.subscribe = jest.fn(); 231 | 232 | const plugin = createPersistedState({ 233 | storage, 234 | arrayMerger: function (store, saved) { 235 | return ["hello!"]; 236 | }, 237 | }); 238 | plugin(store); 239 | 240 | expect(store.replaceState).toBeCalledWith({ 241 | persisted: ["hello!"], 242 | }); 243 | 244 | expect(store.subscribe).toBeCalled(); 245 | }); 246 | 247 | it("rehydrates store's state through the configured getter", () => { 248 | const storage = new Storage(); 249 | 250 | const store = createStore({ state: {} }); 251 | store.replaceState = jest.fn(); 252 | 253 | const plugin = createPersistedState({ 254 | storage, 255 | getState: () => ({ getter: "item" }), 256 | }); 257 | plugin(store); 258 | 259 | expect(store.replaceState).toBeCalledWith({ getter: "item" }); 260 | }); 261 | 262 | it("persist the changed state back through the configured setter", () => { 263 | expect.assertions(1); 264 | 265 | const storage = new Storage(); 266 | const store = createStore({ state: {} }); 267 | 268 | const plugin = createPersistedState({ 269 | storage, 270 | setState: (key, state) => { 271 | expect(state).toEqual({ setter: "item" }); 272 | }, 273 | }); 274 | 275 | plugin(store); 276 | 277 | store._subscribers[0]("mutation", { setter: "item" }); 278 | }); 279 | 280 | it("uses the configured reducer when persisting the state", () => { 281 | const storage = new Storage(); 282 | const store = createStore({ state: {} }); 283 | 284 | const customReducer = jest.fn(); 285 | 286 | const plugin = createPersistedState({ 287 | storage, 288 | paths: ["custom"], 289 | reducer: customReducer, 290 | }); 291 | plugin(store); 292 | 293 | store._subscribers[0]("mutation", { custom: "value" }); 294 | 295 | expect(customReducer).toBeCalledWith({ custom: "value" }, ["custom"]); 296 | }); 297 | 298 | it("filters to specific mutations", () => { 299 | const storage = new Storage(); 300 | const store = createStore({ state: {} }); 301 | 302 | const plugin = createPersistedState({ 303 | storage, 304 | filter: (mutation) => ["filter"].indexOf(mutation) !== -1, 305 | }); 306 | plugin(store); 307 | 308 | store._subscribers[0]("mutation", { changed: "state" }); 309 | 310 | expect(storage.getItem("vuex")).toBeNull(); 311 | 312 | store._subscribers[0]("filter", { changed: "state" }); 313 | 314 | expect(storage.getItem("vuex")).toBe(JSON.stringify({ changed: "state" })); 315 | }); 316 | 317 | it("should call rehydrated callback once the state is replaced", () => { 318 | const storage = new Storage(); 319 | storage.setItem("vuex", JSON.stringify({ persisted: "json" })); 320 | 321 | const store = createStore({ state: { original: "state" } }); 322 | const rehydrated = jest.fn(); 323 | 324 | const plugin = createPersistedState({ storage, rehydrated }); 325 | plugin(store); 326 | 327 | expect(rehydrated).toBeCalledWith(store); 328 | }); 329 | 330 | it("should call rehydrated if the replacement executed asynchronously", () => { 331 | jest.useFakeTimers(); 332 | 333 | const storage = new Storage(); 334 | storage.setItem("vuex", JSON.stringify({ persisted: "json" })); 335 | 336 | setTimeout(() => { 337 | createPersistedState({ storage, rehydrated })(store); 338 | }, 600); 339 | const store = createStore({ state: { original: "state" } }); 340 | const rehydrated = jest.fn(); 341 | 342 | jest.runAllTimers(); 343 | const plugin = createPersistedState({ storage, rehydrated }); 344 | plugin(store); 345 | 346 | expect(rehydrated).toBeCalled(); 347 | const rehydratedStore = rehydrated.mock.calls[0][0]; 348 | expect(rehydratedStore.state.persisted).toBe("json"); 349 | }); 350 | 351 | it("fetches state from storage when the plugin is used by default", () => { 352 | const storage = new Storage(); 353 | storage.setItem("vuex", JSON.stringify({ persisted: "before" })); 354 | 355 | const plugin = createPersistedState({ storage }); 356 | 357 | const store = createStore(); 358 | store.replaceState = jest.fn(); 359 | 360 | storage.setItem("vuex", JSON.stringify({ persisted: "after" })); 361 | 362 | plugin(store); 363 | 364 | expect(store.replaceState).toBeCalledWith({ 365 | persisted: "after", 366 | }); 367 | }); 368 | 369 | it("fetches state from storage before the plugin is used", () => { 370 | const storage = new Storage(); 371 | storage.setItem("vuex", JSON.stringify({ persisted: "before" })); 372 | 373 | const plugin = createPersistedState({ storage, fetchBeforeUse: true }); 374 | 375 | const store = createStore(); 376 | store.replaceState = jest.fn(); 377 | 378 | storage.setItem("vuex", JSON.stringify({ persisted: "after" })); 379 | 380 | plugin(store); 381 | 382 | expect(store.replaceState).toBeCalledWith({ 383 | persisted: "before", 384 | }); 385 | }); 386 | 387 | it("throws specific error if assertion throws error on initializing plugin", () => { 388 | const errorMessage = "no storage space left"; 389 | const assertStorage = () => { 390 | throw new Error(errorMessage); 391 | }; 392 | expect(() => createPersistedState({ assertStorage })).toThrow(errorMessage); 393 | }); 394 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "allowSyntheticDefaultImports": true 4 | } 5 | } 6 | --------------------------------------------------------------------------------