Skip to content

wikimint/cross-domain-localstorage

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🌍 Cross-Domain LocalStorage (No Backend, Pure JavaScript)

Access and share localStorage across:

  • βœ… Subdomains
  • βœ… Multiple domains
  • βœ… Static websites
  • βœ… Without any backend

Built using:

  • iframe
  • postMessage
  • Native browser APIs only

πŸš€ Live Demo Scenarios

Demo Type Description
πŸ”Ή Subdomain Sharing app.example.com ↔ admin.example.com
πŸ”Ή Multi-Domain Sharing site1.com ↔ site2.net
πŸ”Ή Restricted Domains Only selected domains allowed
πŸ”Ή Open Global Mode Any domain (secured with token)

🧠 How It Works

All domains communicate with a central storage hub:

your sites β†’ iframe β†’ storage hub β†’ localStorage

  • Data is stored in ONE domain
  • All other domains access via messaging
  • Works even for static hosting

πŸ“ Project Structure

β”œβ”€β”€ demo/ β”‚ β”œβ”€β”€ subdomain/ β”‚ β”œβ”€β”€ multi-domain/ β”‚ β”œβ”€β”€ restricted/ β”‚ └── open/ β”œβ”€β”€ src/ β”œβ”€β”€ dist/ β”œβ”€β”€ storage/ β”‚ └── storage.html β”œβ”€β”€ build.js └── README.md


⚑ Quick Start

1. Host Storage Hub

Deploy:

/storage/storage.html

Example:

https://storage.yourdomain.com/storage.html


2. Include Script

<script src="dist/cross-storage.min.js"></script>

3. Initialize

const storage = new CrossStorage({
  iframeUrl: "https://storage.yourdomain.com/storage.html",
  token: "your-secret-key"
});

4. Use Like localStorage

await storage.set("user", { name: "John" });

const user = await storage.get("user");

await storage.remove("user");

await storage.clear();

πŸ” Modes Explained

1️⃣ Subdomain Mode

  • Works within same root domain
  • Uses strict origin validation

2️⃣ Multi-Domain Mode

  • Works across different domains
  • Requires shared hub

3️⃣ Restricted Mode

  • Only allowed domains can access
  • Uses whitelist

4️⃣ Open Mode (Token Protected)

  • Any domain can connect
  • Requires secret token

⚠️ Limitations

  • ❌ Cannot access storage of another domain directly
  • ❌ Requires iframe hub
  • ⚠️ Safari / Brave may restrict third-party storage

πŸ›‘οΈ Security Best Practices

  • Always validate event.origin
  • Never use "*" without token protection
  • Avoid storing sensitive data in plain text
  • Use encryption if needed

πŸ§ͺ Demo Instructions

Each demo folder contains:

  • index.html
  • Example usage
  • Instructions

Run locally or deploy to test.

πŸ—οΈ Build

npm install
npm run build

πŸ“¦ Features

  • βœ… No dependencies
  • βœ… Works on static hosting
  • βœ… Promise-based API
  • βœ… JSON support
  • βœ… Lightweight

πŸš€ Future Improvements

  • Encryption layer
  • BroadcastChannel sync
  • TypeScript support
  • Advanced caching

πŸ“„ License

MIT License

πŸ™Œ Contributing

PRs are welcome!

⭐ Support

If this helped you, give a ⭐ on GitHub!

About

Share localStorage across subdomains and different domains using iframe and postMessage. Works on static websites without backend.

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Packages

 
 
 

Contributors