-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Unmount framework components when islands are destroyed #8264
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
e0c36b5
fix(view-transitions): update persistence logic for improved unmount …
natemoo-re b204529
feat(astro): add `astro:unmount` event
natemoo-re 5a98953
feat(vue): automatically unmount islands
natemoo-re 1a10c6a
feat(react): automatically unmount islands
natemoo-re 641b344
feat(react): automatically unmount islands
natemoo-re fd914ea
feat(solid): automatically dispose of islands
natemoo-re fc01cb8
feat(svelte): automatically destroy of islands
natemoo-re 774b96d
feat(svelte): automatically destroy of islands
natemoo-re 21bf59d
feat(solid): automatically dispose of islands
natemoo-re ad6c6f3
feat(preact): automatically unmount islands
natemoo-re eb6446c
chore: update changeset
natemoo-re c0b75be
fix: rebase issue
natemoo-re b0a3573
chore: add clarifying comment
natemoo-re a07cda2
chore: remove duplicate changeset
natemoo-re bf81bb8
chore: add changeset
natemoo-re 7b1c4f3
Merge branch 'main' into feat/unmount
natemoo-re File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
'@astrojs/react': patch | ||
'@astrojs/preact': patch | ||
'@astrojs/vue': patch | ||
'@astrojs/solid-js': patch | ||
'@astrojs/svelte': patch | ||
--- | ||
|
||
Automatically unmount islands when `astro:unmount` is fired |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'astro': patch | ||
--- | ||
|
||
Fire `astro:unmount` event when island is disconnected |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,15 @@ | ||
import { h, render, type JSX } from 'preact'; | ||
import StaticHtml from './static-html.js'; | ||
import type { SignalLike } from './types'; | ||
import { h, render, hydrate } from 'preact'; | ||
import StaticHtml from './static-html.js'; | ||
|
||
const sharedSignalMap = new Map<string, SignalLike>(); | ||
|
||
export default (element: HTMLElement) => | ||
async ( | ||
Component: any, | ||
props: Record<string, any>, | ||
{ default: children, ...slotted }: Record<string, any> | ||
{ default: children, ...slotted }: Record<string, any>, | ||
{ client }: Record<string, string> | ||
) => { | ||
if (!element.hasAttribute('ssr')) return; | ||
for (const [key, value] of Object.entries(slotted)) { | ||
|
@@ -27,23 +28,13 @@ export default (element: HTMLElement) => | |
} | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-shadow | ||
function Wrapper({ children }: { children: JSX.Element }) { | ||
let attrs = Object.fromEntries( | ||
Array.from(element.attributes).map((attr) => [attr.name, attr.value]) | ||
); | ||
return h(element.localName, attrs, children); | ||
} | ||
|
||
let parent = element.parentNode as Element; | ||
const bootstrap = client !== 'only' ? hydrate : render; | ||
|
||
render( | ||
h( | ||
Wrapper, | ||
null, | ||
h(Component, props, children != null ? h(StaticHtml, { value: children }) : children) | ||
), | ||
parent, | ||
element | ||
bootstrap( | ||
Comment on lines
-40
to
-47
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was a hacky solution instead of just using the built-in |
||
h(Component, props, children != null ? h(StaticHtml, { value: children }) : children), | ||
element, | ||
); | ||
|
||
// Preact has no "unmount" option, but you can use `render(null, element)` | ||
element.addEventListener('astro:unmount', () => render(null, element), { once: true }) | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change was actually important! We replace the persistent islands first, then swap the
<body>
element. Previously, we would trigger multipledisconnectedCallback
in a row when usingtransition:persist
.