Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: loadScriptsOnMainThread breaks when using a regexp #540

Merged
merged 2 commits into from
Jan 30, 2024

Conversation

franpeza
Copy link
Contributor

@franpeza franpeza commented Jan 26, 2024

What is it?

  • Feature / enhancement
  • Bug
  • Docs / tests

Description

This PR fixes two issues with loadScriptsOnMainThread:

  • When using a regexp in loadScriptsOnMainThread all scripts will be executed in the main thread, even if their url doesn't match the regexp.
  • If a string is used and it includes some characters reserved by regexp (such as interrogation marks), even if the script src seems to match, the script won't be executed in the main thread.

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 in loadScriptsOnMainThread. If you execute the test before the fix is applied this is what is displayed:

Screenshot 2024-01-26 at 14 03 41

The script is executed on the main thread but it shouldn't. After the fix we get the expected behavior:

Screenshot 2024-01-26 at 14 04 20

In the solution I provide the regexp is serialised using its source property, and loadScriptsOnMainThread itself with this type [type: 'string' | 'regexp', value: string][]. So if we have these values:

loadScriptsOnMainThread = [/.*mywebsite\.com/, 'https://www.anotherawesomewebsite.com/resource'];

the property will be serialised and used internally as:

loadScriptsOnMainThread = [
  ['regexp', '/.*mywebsite.com/'],
  ['string', 'https://www.anotherawesomewebsite.com/resource'],
];

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:

loadScriptsOnMainThread = [
  /.*mywebsite.com/,
  /https:\/\/www\.anotherawesomewebsite\.com\/resource/,
];

It would be great to discuss and apply any suggestion 😄

Checklist:

  • My code follows the developer guidelines of this project
  • I have performed a self-review of my own code
  • I have made corresponding changes to the documentation
  • Added new tests to cover the fix / functionality

Copy link

vercel bot commented Jan 26, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
partytown ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 27, 2024 8:04am

@gioboa gioboa merged commit 3bb8e9b into QwikDev:main Jan 30, 2024
8 checks passed
@gioboa gioboa mentioned this pull request Jan 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants