p3ol / buddy

🐕 Dead simple cross-domain iframe/window messaging
MIT License
8 stars 0 forks source link

✨ feat: add queue option #743

Closed dackmin closed 1 year ago

dackmin commented 1 year ago

On recent browsers (although the problem is only noticeable on chrome), cache loading order messes up events registering & messages when the target window/frame loads after the parent has already sent messages (the problem does not apply on messages sent after an iframe or a new window is manually loaded/opened).

This PR adds an option called queue that, when enabled on the parent will register a target:loaded event handler to queue messages in case the child registers its handlers too soon, and when enabled on the child will send that same target:loaded event to drain the parent messages queue, ensuring messages are not sent into oblivion.

The best way I found to test this was to delay child event handlers registration by 100ms to let the parent try to send the messages (into the void), to finally let the child trigger the messages queue and correctly receive the said messages. If anyone has a better/more accurate way to test this, that'd be really appreciated ❤️

emileNetter commented 1 year ago

Why not make the queue options true by default ? On chrome it's never working on page load or refresh with or without cache (with queue option set to false)

dackmin commented 1 year ago

I chose to set it to false by default for now to avoid any breaking change, but will be set to true by default some time in the future.