npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vuex-hydra

v1.3.0

Published

> Vuex plugin to hydrate your stores with external data.

Downloads

205

Readme

Vuex Hydra

Vuex plugin to hydrate your stores with external data.

NPM MinZip

It can be used for frontends to instantly access backend data without further API requests. Store data can be passed directly, read from JSON in HTML or the window object.

Check the examples below for more information.

Use cases

  • Pass dynamic/runtime data from backends into Vuex stores
  • Prevent redundant API requests
  • Speed up store initialization

UseCase

Setup

Install the plugin with npm or yarn

npm install --save vuex-hydra
yarn add vuex-hydra

Import vuex-hydra into your project, which makes the $hydrate function available in all components

import Vue from 'vue';
import Vuex from 'vuex';
import VuexHydra from 'vuex-hydra';

Vue.use(Vuex);
Vue.use(VuexHydra); // Use after Vuex

const store = Vuex.Store({
    state: {
        hello: '',
    }
});

new Vue({
    store,
    created() {
        // This is the simplest case for static data
        // Check the examples below for dynamic backend data not available at compile time
        const data = { root: { hello: 'world' } }
        this.$hydrate({ data });
    }
});

Usage

Data structure

Vuex-hydra can hydrate the root state and namespaced modules. The first level properties of your data object should contain the names of your store modules. Root state data is identified by root, namespaced modules should have their respective names.

Example: Assign data to root state and the state of a namespaced module called user.

import Vuex from 'vuex';

export default new Vuex.Store({
    state: {
        foo: '',
    },
    modules: {
        user: {
            namespaced: true,
            state: {
                id: null,
                name: '',
            },
        }
    },
});

This data structure defines store names and their states

{
  "root": {
    "foo": "bar"
  },
  "user": {
    "id": 42,
    "name": "baz"
  }
}

$hydrate([config])

The configuration object can have following properties

|Config|Type|Default|Description| |---|---|---|---| |data|object|{}|Store data| |id|string|null|Id of DOM Element containing JSON| |name|string|null|Property name in window object| |silent|boolean|false|Prevents console messages like logs or errors|

Examples

There are multiple ways to hydrate your store. If the data is provided by a backend, the best way is to hydrate your stores with JSON in HTML.

Hydrate with JSON in HTML

Serialize your data to JSON, place it in a DOM Element and hydrate via id

<body>
    <div id="vuex-hydra">
        { "root": { "foo": "bar" } }
    </div>
</body>
this.$hydrate({ id: 'vuex-hydra' });

Hydrate with window object

Extend the window object with your data before application initialization

<body>
    <script>
        window.backendData = { root: { foo: 'bar' } }
    </script>
</body>
this.$hydrate({ name: 'backendData' });

Hydrate namespaced and nested modules

Use the name your namespaced modules are registered with instead of root. Separate nested modules names with /.

{
    "namespacedModuleName": { "foo": "bar" },
    "nested/moduleName": { "bar": "baz" },
    "really/nested/moduleName": { "baz": "qux" }
}
this.$hydrate({ /* config */ });

Frameworks

This section is in progress...

Laravel

Collect your data in the controller and pass it to a view

public function index()
{
    $data = [
        'root' => [
            'foo' => Bar::first(),
        ],
    ];
    return view('index', [
        'data' => $data,
    ]);
}

Insert hidden JSON without further encoding

<div id="my-data" style="display: none">
    {!! json_encode($data) !!}
</div>
this.$hydrate({ id: 'my-data' })

Development

Clone this project and run

npm install

Start local dev server from application in demo/

npm run serve

Create tests with Jest and run them with

npm run test

Lint and fix files

npm run lint

Create a feature branch on develop und submit it as pull request.

License

MIT