Consent tooling that rewrites your script tags
A consent manager that blocks scripts until the visitor agrees usually activates each one by cloning the tag and swapping the clone in. The clone keeps the attributes. It drops every handler set from JavaScript, and the node your code still holds is now detached. Any code that assumes “my script element” breaks without an error.
I hit all three consequences on a Shopify storefront I worked on, between July and November 2026. The client is anonymized. The consent manager is Pandectes, a third-party Shopify app, and the numbers come from the repository’s commits, its tests and a browser probe I ran on 2026-11-13.
Why a consent manager has to clone the script tag
The usual blocking pattern marks a third-party tag as inert and labels its consent category:
<script type="javascript/blocked" data-cookiecategory="targeting" src="…"></script>
The browser does not run a script whose type it does not recognise. When the
visitor grants that category, the consent manager must make the tag run. Changing
type in place does not do it: the browser decides whether to run a script when the
element is inserted, not when an attribute changes. I checked this in headless
Chromium 149. A blocked inline script with its type flipped to text/javascript in
place ran 0 times. The same script, cloned, retyped and swapped in with
replaceWith, ran 1 time.
So the clone-swap is not a vendor quirk. It is the only simple way to make a blocked
tag execute, and any consent tool that blocks by type has to do something like it.
What the clone keeps and what it drops
cloneNode copies attributes. It does not copy properties or listeners. In the same
probe:
| After the swap | Result |
|---|---|
data-* attribute on the clone |
kept |
onload property on the clone |
null |
onload calls on the original |
0 |
addEventListener('load') calls on the original |
0 |
original.isConnected |
false |
document.body.removeChild(original) |
throws NotFoundError |
original.remove() |
no error |
Each row is a separate way for site code to fail. React and framework Script
components attach onLoad and onError as listeners, so they fall in the dropped
column too.
Failure one: start-up code that waited for onload
The storefront tagged seven third-party scripts for blocking on 2026-07-18: a
support-chat widget, an SMS-marketing tag, a personalisation SDK and its helper, a
session-replay tag, an email-marketing tag and a reviews widget. Several of them started their vendor’s widget in
an onload handler. After consent, the script loaded and nothing called it, because
the handler was on the detached original.
The fix landed the next day, 2026-07-19, and took three forms, one per script shape:
- Listen for the vendor’s own event. The chat widget fires a “loaded” event on
window, so start-up runs on whichever arrives first, with a flag so it runs once. - Poll for the global. The personalisation SDK has no event, so a 500 ms interval checks for its global object. The poll stays pending while consent is withheld, which is the correct outcome: the feature stays off.
- Move the handler into the script body. The SMS tag had a fallback CDN in
onError. The bootstrap became an inline blocked script that creates the real tag and sets itsonerroritself, so the handler is born after activation.
A review on 2026-07-30 found two more. One tag was typed text/blocked, not
javascript/blocked, so no consent manager would ever activate it and the browser would
never run it. And the SMS fallback moved again, to a capture-phase error listener on
window. That choice matters, and the probe shows why:
| Listener on a clone-swapped tag | Calls |
|---|---|
window, capture, error |
1 |
document, capture, load |
1 |
window, capture, load |
0 |
A capture listener above the tag sees events from any clone, because it never held a
reference to a node. But load events from elements stop at document; they do not
reach window. A window listener works for error and silently never fires for
load.
Failure two: cleanup that removed a node it no longer owned
The chat widget is mounted from a React effect that appends the loader tag and removes it in cleanup. The cleanup was:
return () => {
document.body.removeChild(script);
};
After a clone-swap, script is the detached original. removeChild throws
NotFoundError on a node that is not a child, and an exception thrown from effect
cleanup takes the route to the error page. The effect depends on whether the route is
a product page, so any navigation into or out of /products/ ran the cleanup. Consent
granted, then click a product: error screen.
The fix on 2026-07-30 was 2 files, 21 lines added and 1 removed:
return () => {
// The node we appended may already be detached; remove() is a no-op then.
script.remove();
// Drop whichever activated clone is live, found by id, not by reference.
document.getElementById('chat-widget-loader')?.remove();
};
The test that pins it does what the consent manager does: cloneNode(true), set
type to text/javascript, replaceWith, then assert that unmount does not throw and
no loader is left in the DOM. That test is the useful part. Unit tests that render a
blocked script and stop there never see the swap, because no consent manager runs in
the test environment.
Failure three: a tag injected after the unblock pass
The personalisation SDK was injected from a React effect, after hydration. The release that shipped consent blocking for it went out on 2026-08-06 at 12:23. The revert merged 3 hours and 4 minutes later. The commit records the cause: the consent manager activated blocked scripts in one pass at start-up, the effect appended the tag after that pass, and nothing activated it. The SDK’s global stayed undefined and every campaign it drove was dead.
The polling fix from failure one could not help here. It waits correctly for a script that will run; this script would never run.
The other tags kept their blocking, but two of them were also mounted late, inside an idle-time loader. On 2026-11-06, 111 days after the first tagging, both moved into the server-rendered HTML, so they are in the DOM before the consent manager scans. That created one more ordering problem: a returning visitor with saved consent can now have the tag activated before React hydrates, so an error listener registered in an effect may arrive too late. The fallback listener moved into an inline script in the head, ahead of the consent manager’s own scripts.
Rules I now follow for consent-blocked scripts
- Never hold a reference to a tag you hand to a consent manager. Find it again by
idor by attribute when you need it. - Never put start-up logic in the tag’s own handlers. Use the vendor’s event, a poll for its global, or code inside the script body.
- Listen above the tag, at the right level.
windowcapture forerror,documentcapture forload. - Render blocked tags in the server HTML when you can. A tag that arrives after the consent manager’s pass may never run.
- Test the swap.
cloneNode, retype,replaceWith, then unmount. In the repository today, 7 source files still render a blocked tag, and the DOM test files for the chat, SMS and session-replay components hold 11 tests, 11 of 11 passing on 2026-11-13.
The same idea sits under Shadow DOM vs iframes for third-party embeds, from the other side: when your code runs in someone else’s page, the host owns the DOM. A consent manager makes your own page a host you do not control.
Revisions
- Published as a backfill of the missed Nov 13 slot; auto-drafted by the publishing run because no draft existed for the row.
- Created from the storefront repository's commits and tests (anonymized), and a headless Chromium 149 probe of clone-swap behaviour, run that day.