Skip to content

Instantly share code, notes, and snippets.

@PierBover
Last active December 18, 2025 19:27
Show Gist options
  • Select an option

  • Save PierBover/1eb1dc122ce20859fd6c6a2e1c47f598 to your computer and use it in GitHub Desktop.

Select an option

Save PierBover/1eb1dc122ce20859fd6c6a2e1c47f598 to your computer and use it in GitHub Desktop.
Keep alive Svelte action
const instances = {};
export default function (node, {id, componentClass}) {
if (instances[id]) {
node.appendChild(instances[id]);
} else {
const wrapper = document.createElement('div');
const instance = new componentClass({
target: wrapper
});
instances[id] = wrapper;
node.appendChild(wrapper);
}
}
<script>
import keepAlive from "./action.js";
import Component from './Component.svelte';
</script>
<div use:keepAlive={{id: 'some-manual-id', componentClass: Component}}/>
@Heniker
Copy link

Heniker commented Dec 18, 2025

A sample solution for Svelte 5.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment