[2.x] Add wrapper hack for turbo #618
Closed
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.
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-wrapperelement (with a fallback to still use the body so it's not a big breaking change).(Note that this PR goes to
2.xfor now, I will make a 3.x PR only if/when we're sure this is final)