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

micro-local-store

v1.0.1

Published

MicroLocalStore is a lightweight, easy-to-use JavaScript library that provides a shared local storage solution across different origins. It creates an isolated local storage for each instance, allowing you to store and retrieve data seamlessly. It's perfe

Downloads

13

Readme

MicroLocalStore

MicroLocalStore is a lightweight, easy-to-use JavaScript library that provides a shared local storage solution across different origins. It creates an isolated local storage for each instance, allowing you to store and retrieve data seamlessly. It's perfect for micro-frontend architectures, enabling different micro-apps to communicate and share state without conflicts.

With MicroLocalStore, you can easily manage state listeners, send and receive messages between parent and child windows, and more. It's compatible with both browser and Node.js environments.

MicroLocalStore Demo

Installation

You can install MicroLocalStore using npm:

npm install micro-local-store

After installation, you can import it in your JavaScript file:

const MicroLocalStore = require("micro-local-store");

Or, if you're using ES6 imports:

import MicroLocalStore from "micro-local-store";

If you want to use it directly in your HTML file, you can use the unpkg CDN:

<script src="https://unpkg.com/micro-local-store"></script>

Usage

const store = new MicroLocalStore("myStore", [
  "https://example.com",
  "https://another-example.com",
]);

// Set state
store.setState({ key: "value" });

// Get state
const state = store.getState();

// Listen for state changes
store.onChange((newState) => {
  console.log(newState);
});

// Stop listening for state changes
store.offChange(listener);

API

new MicroLocalStore(id, allowedUrls)

Creates a new MicroLocalStore instance.

  • id: A unique identifier for the store.
  • allowedUrls: An array of URLs that are allowed to share this store's state. These URLs must be allowed by the CORS policy of the server hosting the application.

setState(newState)

Updates the store's state with the provided state object. This will merge the new state with the existing state.

getState()

Returns the current state.

onChange(listener)

Adds a listener function that will be called whenever the state changes.

offChange(listener)

Removes a previously added listener function.

Cross-Origin Resource Sharing (CORS)

MicroLocalStore uses the postMessage API to communicate between different windows. This requires that the server hosting your application allows the URLs of the other applications to access your application. This is controlled by the server's CORS policy.

When creating a new MicroLocalStore instance, you must provide an array of URLs that are allowed to share the store's state. These URLs must be allowed by the server's CORS policy.

For example, if your application is hosted at https://example.com and you want to share state with https://another-example.com, your server's CORS policy must allow https://another-example.com.

Express.js CORS Configuration

If you're using Express.js, you can use the cors middleware to set up your CORS policy. Here's an example:

const express = require("express");
const cors = require("cors");

const app = express();

const corsOptions = {
  origin: ["https://example.com", "https://another-example.com"],
};

app.use(cors(corsOptions));

// Your routes here

app.listen(3000, () => {
  console.log("Server is running on port 3000");
});

In this example, https://example.com and https://another-example.com are allowed to access your application.

HTML Examples

App1.html

<!DOCTYPE html>
<html>
  <body>
    <button id="trigger">Trigger State Change</button>
    <div id="state"></div>

    <script src="https://unpkg.com/micro-local-store"></script>
    <script>
      const store = new MicroLocalStore("myStore", [
        "https://example.com/App2.html",
      ]);

      // Get state
      const state = store.getState();
      document.getElementById("state").innerHTML = JSON.stringify(state);

      // Listen for state changes
      store.onChange((newState) => {
        document.getElementById("state").innerHTML = JSON.stringify(newState);
      });

      // Set state on button click
      document.getElementById("trigger").addEventListener("click", () => {
        store.setState({ key: "value from App1" });
      });
    </script>
  </body>
</html>

App2.html

<!DOCTYPE html>
<html>
  <body>
    <button id="trigger">Trigger State Change</button>
    <div id="state"></div>

    <script src="https://unpkg.com/micro-local-store"></script>
    <script>
      const store = new MicroLocalStore("myStore", [
        "https://example.com/App1.html",
      ]);

      // Get state
      const state = store.getState();
      document.getElementById("state").innerHTML = JSON.stringify(state);

      // Listen for state changes
      store.onChange((newState) => {
        document.getElementById("state").innerHTML = JSON.stringify(newState);
      });

      // Set state on button click
      document.getElementById("trigger").addEventListener("click", () => {
        store.setState({ key: "value from App2" });
      });
    </script>
  </body>
</html>

Contributing

Contributions are welcome. Please submit a pull request with any improvements.

License

This project is licensed under the MIT License.