Skip to content

Conversation

@Jade-GG
Copy link
Collaborator

@Jade-GG Jade-GG commented Oct 29, 2024

This fixes an annoying issue that's been inside Turbo since its inception: Turbo will always replace the entire <body> tag. As a result, this causes widgets that have been loaded into the body by scripts like GTM to be destroyed on navigation (see internal slack for current examples). Even worse the scripts will persist but their elements will not, so we get a bunch of JS errors.

Turbo doesn't support changing this root node to something else, even though this has been asked in issues since at least 2021. There are some events we can hook into with the renderer to change certain behaviors, but this is unwieldy and doesn't really work well for this use case.

This PR hacks into the PageRenderer class and replaces the assignNewBody function with one that does the exact same except with a #turbo-wrapper element (with a fallback to still use the body so it's not a big breaking change).

(Note that this PR goes to 2.x for now, I will make a 3.x PR only if/when we're sure this is final)

@royduin
Copy link
Member

royduin commented Dec 5, 2024

Closing this for now as it was for 1 specific use case.

@royduin royduin closed this Dec 5, 2024
@royduin royduin deleted the feature/fix-turbo-gtm branch December 5, 2024 09:45
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