Share your Svelte store across origins, including iFrames.
Features
npm install svelte-crossorigin-store
import { createStore } from 'svelte-crossorigin-store';
const store = createStore('Hello, world');
const unsubscribe = store.subscribe(value => console.log('State updated:', value));
Or, in your Svelte component:
<script>
import { createStore } from 'svelte-crossorigin-store';
const store = createStore('Hello, world');
</script>
<p>Current State: {$store}</p>
:warning: Take note that the API doesn't follow a publisher/subscriber approach. Hence, you need to make sure that instances are not initialized with the same value. Otherwise, an infinite loop will occur!
createStore
createStore<T>(initialValue?: T, {
allowedOrigins?: string[];
id?: string;
iframeSelector?: string;
onChange?: (value: any) => void;
});
The created store exposes the same API methods like a writable svelte/store
:
set()
update()
subscribe()
Please refer to the official documentation.
This repository contains a demo application of a classic counter that synchronizes its state across three origins: the parent page and two iFrames running on different ports.
To launch the application, run the following:
pnpm start
This should automatically open https://localhost:3030
in your default browser.
This work is licensed under The MIT License.