Skip to content

Instantly share code, notes, and snippets.

@PierBover
Last active March 18, 2024 17:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PierBover/1eb1dc122ce20859fd6c6a2e1c47f598 to your computer and use it in GitHub Desktop.
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}}/>
@kirkbushell
Copy link

This is cool - how do you get it working for page routes?

@khromov
Copy link

khromov commented Mar 18, 2024

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