fix: loadScriptsOnMainThread breaks when using a regexp #540
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What is it?
Description
This PR fixes two issues with loadScriptsOnMainThread:
You can check the issue if you execute the new tests added (checking out the first commit of the PR) without the fix included in the PR (second commit).
I added new tests in
tests/integrations/load-scripts-on-main-thread
to display whether the scripts in the page have been executed on the main thread or in the background. In addition I added a new script,background-test-script.js
that should be executed in the background since is not included inloadScriptsOnMainThread
. If you execute the test before the fix is applied this is what is displayed:The script is executed on the main thread but it shouldn't. After the fix we get the expected behavior:
The main problem in the first issue is: when the config is serialised using
JSON.stringify
https://github.com/BuilderIO/partytown/blob/486b5a9df92dad22137bd0e07b8deeb6f9c2e8c5/src/lib/sandbox/read-main-platform.ts#L64 the regexp gets serialised unexpectedly as an empty object. Once this empty object is used to create a RegExp object it will match any string.For the second issue since the string provided is used to create a RegExp https://github.com/BuilderIO/partytown/blob/486b5a9df92dad22137bd0e07b8deeb6f9c2e8c5/src/lib/web-worker/worker-script.ts#L31, if it contains a regexp wildcard/operator (such as dots or question marks) it will be handled as those regexp operators and wildcards instead of an actual character. If this is the case, in most cases (when the resulting regexp is not equivalent to the one expected) the regexp won't match the url to test.
In the solution I provide the regexp is serialised using its
source
property, andloadScriptsOnMainThread
itself with this type[type: 'string' | 'regexp', value: string][]
. So if we have these values:the property will be serialised and used internally as:
Note that the type of the public property won't change.
This way we can know beforehand if the string provided is a plain string (that will be escaped when creating the regexp) or a regexp source (and won't be escaped). Therefore, internally the prop will be used as you would expect:
It would be great to discuss and apply any suggestion 😄
Checklist: