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

shared-session-storage

v1.1.0

Published

Shared sessionStorage's sessions between tabs and windows

Downloads

252

Readme

Shared Session Storage

DEMO

原因

假设有这么一个需求:需要临时保存一些数据,可以在多个 tab/window 中共享,当所有的页面关闭后,可以自动销毁这些数据。

首先可能会想到是 cookie,但由于 cookie 会在请求时将数据发送到服务器,因此这里不考虑它了。

接着就是 sessionStorage 和 localStorage 这两个 API 了。

sessionStorage 一般用于保存临时数据,只有在关闭页面,退出浏览器时,才会销毁这些数据,刷新这个页面并不会丢失数据。

但 sessionStorage 有个比较大的缺点,如果同时打开多个页面,这些页面之间无法共享数据。

如果需要共享数据,网络上一般会推荐使用 localStorage。确实,localStorage 可以在多个页面中共享数据,但由于 localStorage 保存的数据是永久的,而上面的需求需要的只是临时保存而已。

由于无法找到其他更好的 API 了,而 sessionStorage 和 localStorage 又同时满足上面需求的一部分,因此这里打算将其封装一下。

刚开始时,打算只有 localStorage,然后当浏览器关闭后手动来销毁这些临时数据,但在深入分析后,发现实现起来比较麻烦。

于是就使用 sessionStorage 来保存数据,然后使用 localStorage 同步这些数据作为最终的方案了(在同步数据时,有想过使用其他的 API,比如 SharedWorker、MessageChannel、BroadcastChannel 等,但由于兼容性的原因,最终选择 localStorage + storage 事件来实现同步)。

安装

npm install --save shared-session-storage

使用方式

ES6 module

import sharedSessionStorage from "shared-session-storage";

// API like sessionStorage and localStorage

Load js file

<script src=".../shared-session-storage/dist/index.js"></script>
<script>
    // window.sharedSessionStorage
</script>

已知问题

当前已知有一个问题,当一个 TabA 里保存一些临时数据,在其关闭后,接着打开 TabB,这里在 TabB 里又保存新的一些临时数据。如果这里重新恢复打开 TabA,会导致 TabA 与 TabB 的数据不一致。