wxt
wxt copied to clipboard
fix: content script is incorrectly invalidated when injected multiple times
Overview
Video of issue and fix: https://www.loom.com/share/97983e5d38904af594d5f486bbf9f1f4?sid=e588c652-c868-4cb0-8db2-521650726919
This PR replaces window.postMessage with document.dispatchEvent in order to avoid race conditions when invalidating content scripts.
Since document.dispatchEvent invokes event handlers synchronously, it prevents issues with invalidation due to the asynchronous nature of window.postMessage.
I cleaned up related code in ContentScriptContext that followed as a result of this change:
- Due to the removal of the race condition, the logic of ignoring the first event used to resolve #884 is no longer necessary.
- The premature invalidation in #1359 should not happen, but to handle the case of a rogue website that somehow replays the custom event, the content script context ignores messages from itself. However, it should not ignore messages from other instances of the same script being injected, so the comparison was changed to simply check the message ID against its own context ID.
- Since
document.dispatchEventoperates within its frame,stopOldScriptsis called in all cases, not just in the top frame so that duplicate content scripts are invalidated when they're injected into a frame.
Related Issue
This PR closes #1937
Deploy Preview for creative-fairy-df92c4 ready!
| Name | Link |
|---|---|
| Latest commit | 3c745a90dbeaac36859abe16ddf2dba1a6656ec2 |
| Latest deploy log | https://app.netlify.com/projects/creative-fairy-df92c4/deploys/69002f8db346d00008de2afd |
| Deploy Preview | https://deploy-preview-1938--creative-fairy-df92c4.netlify.app |
| Preview on mobile | Toggle QR Code...Use your smartphone camera to open QR code link. |
To edit notification comments on pull requests, go to your Netlify project configuration.