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 🙏

© 2026 – Pkg Stats / Ryan Hefner

vite-plugin-cross-origin-storage

v2.1.0

Published

Vite plugin to extract shared dependencies into content-addressed chunks loaded from Cross-Origin Storage

Readme

vite-plugin-cross-origin-storage

[!WARNING] Experimental. The Cross-Origin Storage API is an early-stage proposal with no native browser support yet, and this plugin's chunk format is not stable. Do not depend on it in production.

A Vite plugin that extracts shared dependencies (such as vue) into content-addressed chunks that can be loaded from Cross-Origin Storage (COS). When two sites build the same dependency at the same version, they produce byte-identical chunks with the same SHA-256, so a browser that supports COS can serve the chunk from a shared store instead of fetching it again per origin.

This builds on Thomas Steiner's original vite-plugin-cross-origin-storage, and is intended as an update of it. It explores a content-addressed chunking and decentralised (registry-free) sharing model on top of the loader and import-rewriting approach Thomas established. The aim is to merge these changes back upstream.

How it works

At build time, for each package matched by packages:

  1. The package is externalised from the app graph, and each of its modules is built on its own with rolldown, preserving every export (no tree-shaking). This makes a chunk's bytes depend only on its module, never on which parts of it the app happened to import, so it is identical across sites.
  2. Its imports are discovered and built too, recursively, so managing one package implicitly manages its whole import subgraph (e.g. vue pulls in @vue/*). Each imported module becomes exactly one chunk, so a module that several entry points or packages import is never duplicated, which also preserves singletons like @vue/reactivity. Files loaded with require() are instead bundled into each chunk that requires them.
  3. Chunks are hashed bottom-up: each chunk imports its dependencies by their content hash (cos1:<sha256>), so a chunk can only be hashed once its dependencies are. Modules in an import cycle are hashed as a group and import each other as cos1:<sha256>-<n>. The result is purely a function of the source plus a pinned build recipe.
  4. A runtime loader is injected. It looks each managed chunk up in COS by hash, falling back to the network (verifying the chunk against its hash) and storing the fetched chunk for next time, then wires everything together through an import map. Any import map already in the page is merged into it, so the page ends up with a single import map.

Browsers can cap how many lookups a site makes for files it did not store itself, so the loader only looks up the 8 largest of those per load and fetches the rest. A chunk found through another site is written back, so later lookups for it are the site's own. Where a permissions policy blocks COS, such as in a cross-origin <iframe> without allow="cross-origin-storage", every chunk is loaded from the network.

The cos1: prefix is the specifier format version. Chunks are only byte-identical across builds that use the same build recipe: the rolldown version and options pinned by this plugin. Recipes are numbered by the exported RECIPE constant, also recorded as recipe in the manifest. The number increases whenever the chunks emitted for the same source change, which only happens in a minor release, never a patch. A different recipe produces different bytes, and so different hashes, so its chunks are not shared but never collide.

Installation

npm install -D vite-plugin-cross-origin-storage

Usage

// vite.config.ts
import { defineConfig } from 'vite'
import { cosPlugin } from 'vite-plugin-cross-origin-storage'

export default defineConfig({
  plugins: [
    cosPlugin({
      packages: [/^(?:vue$|@vue\/)/],
    }),
  ],
})

For a plain client build, the plugin injects the loader into index.html and removes the default entry <script> automatically.

Trying it out

The plugin only runs at build time, so verify against a production build, not the dev server:

vite build && vite preview

Then check, in your dist/<assetsDir>/, that the managed packages are emitted as content-hashed chunks (a 64-character hex filename like a1b2c3...e4f5.js), and that opening the preview URL still loads the app normally. The chunks import each other by cos1:<hash> specifiers, resolved at runtime through an injected <script type="importmap">.

Without a COS-capable browser the loader fetches each chunk over the network, so this is the network-fallback path: it confirms the chunking and loader work, but not sharing.

To see real Cross-Origin Storage, install the extension, then:

  1. Open the preview URL. On the first load the chunks are fetched and stored in COS (the extension's toolbar popup shows the activity).
  2. Reload, or open a different site that ships the same dependency at the same version. In DevTools -> Network, the managed hashed .js chunks are no longer fetched; they come from the shared store instead.

For local testing it's safest to load the extension unpacked from web-ai-community/cross-origin-storage-extension.

Options

| Option | Type | Default | Description | | --- | --- | --- | --- | | packages | Array<string \| RegExp> | (required) | Packages to extract into COS chunks. Matched against the imported specifier; a plain string is an exact match. Transitive dependencies are collected automatically. | | base | string | Vite's base + build.assetsDir | Public path the chunks are served from. | | loaderEntry | string | bundled loader | Path to a custom runtime loader entry. | | onGenerated | (scriptContent: string) => void | (unset) | Receives the loader <script> body once chunks are emitted. SSR frameworks inject it into their own rendered HTML and render any import map in the page as <script type="cos-importmap">; when omitted the plugin does both in index.html. |

Browser support

The Cross-Origin Storage API is not yet implemented in any browser. You can try it today with the Cross-Origin Storage browser extension. Without it, the loader falls back to ordinary network requests, so the build still works everywhere; it just doesn't share chunks.

Limitations

  • Managed packages must be self-contained. A package whose source imports build-time virtuals (e.g. #build/*, #imports) cannot be bundled standalone and is rejected with a clear error. It also wouldn't be shareable, since its output would differ per app.
  • Single-entry builds. The loader wires up one entry chunk; multi-page builds with several HTML entries are not yet supported.
  • The app entry is never COS-shared. It is app-specific and is loaded from the network.
  • Chunks run from blob: URLs. A Content Security Policy must allow blob: in script-src.
  • Referenced files load from the network. A file that a managed package locates with new URL('<relative path>', import.meta.url), such as a decoder's .wasm, is emitted under its content hash and fetched only when the app requests it, so it is not stored in COS. Managed chunks run from blob: URLs, so only a literal path to an existing file can be resolved inside one; a computed path, a directory, or a path with a query or hash cannot.
  • Referenced files are emitted as-is. A worker started with new Worker(new URL('./worker.js', import.meta.url)) is copied without bundling, so it must not import other modules.
  • Sharing is recipe-scoped. Chunks are only shared between builds on the same build recipe (the manifest's recipe), which can span several releases of this plugin. Within a recipe, a module whose output is unchanged between two versions of its package is shared between them too.
  • Identical modules share one instance. Two modules whose output is byte-identical, even from different packages or versions, resolve to the same chunk, and so to the same module instance at runtime.
  • Every module is a chunk. A package that ships many small files (such as lodash-es) produces as many chunks, and the loader resolves every managed chunk before the app starts, including those only reached through a dynamic import().

Credits

Original plugin and the COS loader / import-rewriting approach by Thomas Steiner (tomayac/vite-plugin-cross-origin-storage).

License

MIT