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

Turbolinks: clean up injected scripts and styles on page navigation #3283

Merged
merged 3 commits into from
May 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tall-forks-cross.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/turbolinks': patch
---

Fixes an issue that prevented client components from rehydrating when navigating back to a page
16 changes: 16 additions & 0 deletions packages/integrations/turbolinks/client.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,18 @@
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();
}
});

// 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);
}
});