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: update worker-script innerHTML implementation #572

Merged
merged 1 commit into from
Mar 24, 2024

Conversation

slawekkolodziej
Copy link
Contributor

@slawekkolodziej slawekkolodziej commented Mar 20, 2024

What is it?

  • Feature / enhancement
  • Bug
  • Docs / tests

Description

There are some bugs in Partytown's <script> implementation that were reported looong time ago along with MRs that were supposed to fix those bugs, unfortunately none was merged yet:

In summary:

1. Partytown does not correctly handle nested innerHTML on <script> elements.

Take a look at this code:

div = document.createElement('div');
div.innerHTML = '<script>console.log(42);</script>';
console.log(div.firstChild.innerHTML);

It should log the content of the script. Unfortunately when run inside Partytown it returns empty string.

2. Partytown does not properly handle code like this:

script = document.createElement('script');
script.text = 'console.log(42);';

GTM uses this internally when setting content of scripts added as custom HTML tags.

Use cases and why

I use GTM through Partytown. However I want to have some custom HTML tags that are always executed on main thread.

Code for custom HTML tag looks somewhat like this:

 <script type="text/javascript" id="load-on-main">
  (function () {
    ... some third-party js code ...
  })()
</script>

I then whitelist script IDs with loadScriptsOnMainThread. When this is deployed, I end up with nested calls for script.innerHTML and script.text. Changes in this MR fixes the problem.

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 Mar 20, 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 Mar 20, 2024 10:22pm

Copy link
Member

@gioboa gioboa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @slawekkolodziej for your commitment

@gioboa gioboa merged commit d72574b into QwikDev:main Mar 24, 2024
12 checks passed
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