From d56eb4e5f06a3f8d5059f735e47008a17b590f2a Mon Sep 17 00:00:00 2001 From: Tony Sullivan Date: Tue, 3 May 2022 17:18:17 -0500 Subject: [PATCH] also cleanup injected styles on page navigation --- packages/integrations/turbolinks/client.js | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/packages/integrations/turbolinks/client.js b/packages/integrations/turbolinks/client.js index 6e4a413bf07d..2305f3cf48bf 100644 --- a/packages/integrations/turbolinks/client.js +++ b/packages/integrations/turbolinks/client.js @@ -2,9 +2,17 @@ import Turbolinks from 'turbolinks'; export { Turbolinks }; // Before every page navigation, remove any previously added component hydration scripts -document.addEventListener("turbolinks:before-render", function() { - const scripts = document.querySelectorAll("script[data-astro-component-hydration]"); - for(const script of scripts) { - script.remove(); - } -}) +document.addEventListener('turbolinks:before-render', function () { + const scripts = document.querySelectorAll('script[data-astro-component-hydration]'); + for (const script of scripts) { + script.remove(); + } +}); + +// After every page navigation, move the bundled styles into the body +document.addEventListener('turbolinks:render', function () { + const styles = document.querySelectorAll('link[href^="/assets/asset"][href$=".css"]'); + for (const style of styles) { + document.body.append(style); + } +});