Giter VIP home page Giter VIP logo

vuex-iframe-sync's Introduction

vuex-iframe-sync

English | 中文

Vuex state synchronization between iframe/window

build npm downloads

Your star is the greatest encouragement to me.

✨ Features:

  • support iframes/window sync
  • initialization sync when iframe loaded
  • configure the sync behavior for your specific needs

🔧 Requirements

Note window.postMessage has limition on message, works like JSON.parse() and JSON.stringfy().If you have trouble with it, configure a convert function in broadcast and transfer API.

🔧 Installation

CDN

<script src="https://cdn.jsdelivr.net/npm/vuex-iframe-sync/dist/vuex-iframe-sync.umd.js"></script>

NPM

npm install vuex-iframe-sync --save

YARN

yarn add vuex-iframe-sync

📦 Examples

📦 Usage

// in parent's component with iframe
<iframe id="frameId1"/>
<iframe id="frameId2"/>

// in parent's store.js
import {broadcast} from 'vuex-iframe-sync'

export default new Vuex.store({
  // ...
  plugins: [
    broadcast('frameId1,frameId2')
  ]
})

// in iframe's store.js
import {transfer} from 'vuex-iframe-sync'

export default new Vuex.store({
  // same state and mutations with parent
  plugins: [
    transfer()
  ]
})

🔧 API

broadcast(ids: String, [options])

Send state changes payload to iframes through postMessage API while parent state change.

ids <String|Array>: frameIds split by ',' or [{id: iframeId, origin: iframeOrigin}...]

options : The following options can be provided to configure the parent behavior for your specific needs:

  • convert <Function(payload)>: convert payload before pass to iframes.

transfer([options])

Receive state changes from parent. Send state changes to parent while self state change.

options : The following options can be provided to configure the iframe behavior for your specific needs:

  • convert <Function(payload)>: convert payload before pass to parent.
  • created <Function(id, store, send)>: call after iframe created. id: iframeId、store: this.store、send<Function(type, payload)>:parent.$store.commit
  • destroyed <Function(id, store, send)>: call after iframe destroyed. id: iframeId、store: this.store、send<Function(type, payload)>:parent.$store.commit

Build Setup

# serve with with-webpack example at localhost:8080
npm run dev

# serve with simple example at localhost:8080
npm run dev:simple

# test with jest
npm run test

# build for production with minification
npm run build

# build for production with live example
npm run build:docs

🛣 Pending

  • Need your advice

🥂 License

MIT

vuex-iframe-sync's People

Contributors

l-chris avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

vuex-iframe-sync's Issues

Uncaught TypeError: Cannot read property 'id' of null

My main app has an iframe within a component that looks as follows:
<iframe id="frameId1" src="http://localhost:51219/" style="width: 100%;" scrolling="no"/>
with
plugins: [ broadcast('frameId1') ]
within the store.

The localhost URL is obviously what the iframe is displaying. It displays it fine (it is also a Vue application) and I need to sync my Vuex state with it. (Is it possible to share state with a Vue application specified in the iframe as a URL like this??? I am trying to move towards microfrontends and thus want components to be deployable separately so they will live on a different server/domain.)

It has
plugins: [ transfer() ]

Within its store in order to sync the Vuex state from the main app in to the child app.

However, I just get this same error on the frame app displayed in console:

vuex-iframe-sync.es.js?4ec3:1
Uncaught TypeError: Cannot read property 'id' of null
at eval (vuex-iframe-sync.es.js?4ec3:1)
at eval (vuex.esm.js?edaa:343)
at Array.forEach ()
at new Store (vuex.esm.js?edaa:343)
at eval (store.js?ac69:12)
at Object. (commons.js?v=636849159444181352:170)
at webpack_require (commons.js?v=636849159444181352:55)
at eval (layout.js?5b7c:1)
at Object.9 (layout.js?v=636849159444111345:29)
at webpack_require (commons.js?v=636849159444181352:55)

I cannot for the life of me figure out what is causing this error?
Would really appreciate it if we could figure out the problem.

Error when setting `postMessage` origin through broadcast options

Hi

My application requires an iframe window which can can navigate between pages. Because the ObserverIframe class defaults the origin to window.location as soon as the src attribute is updated, the iframe sync breaks with the following error:

DOMException: Failed to execute 'postMessage' on 'Window': Invalid target origin '' in a call to 'postMessage'.

I think it would be a good idea to set the default option to "*", make it configurable, or provide an API to update the origin through some event hook of some kind (if this does not exist already?)

Cheers

transfer 时 一直报index.js:23 Uncaught TypeError: Cannot read properties of null (reading 'id')

在store.js里面放入,transfer时,一直报index.js:23 Uncaught TypeError: Cannot read properties of null (reading 'id'),希望各位大神可以帮忙解决一下。谢谢非常感谢

index.js:23 Uncaught TypeError: Cannot read properties of null (reading 'id') at index.js:23:43 at vuex.min.js:6:6498 at Array.forEach (<anonymous>) at new C (vuex.min.js:6:6471) at eval (iframeIndex.js?6120:6:1) at ./src/store/iframeIndex.js (app.js:2624:1) at __webpack_require__ (app.js:833:30) at fn (app.js:130:20) at eval (main.js:17:77) at ./src/main.js (app.js:2589:1)

Sandbox attribute

Hi, I find vuex-iframe-sync load faster than paypal krakenjs's zoid. This project is great in feature and performance.
My use case would be to reasonably sandbox the third party components, as seamless as state as possible. I might not need callback, just passing the information for third party components. Does it work with sandbox attribute?
I change the simple example with sandbox attribute, then run npm run dev:simple , but iframe1 not loading.
Not also sandbox="allow-scripts". It doesn't need allow-same-origin right? as both are from same webserver.

new Vue({
  el: '#app',
  store,
  template:
  '<div>' +
    '<iframe id="frameId1" src="./appFrame.html" sandbox style="width:100%;"/>' +

How it compare with Zoid (Paypal's Kraken Team)

Advantages of this library:

  • Lightweight
  • Designed with Vue State in mind

Zoid

  • Support by larger community,
  • More use case.
  • Large Payload

Do you consider sharing how to work with this library vs Zoid in Simple use case?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.