Skip to content

Are the CSS / JS of a Svelte component loaded once or loaded for each instance of the component ? #10684

Answered by brunnerh
MarArMar asked this question in Q&A
Discussion options

You must be logged in to vote

It is the same with SvelteKit.

In Svelte 4, state is stored as local variables inside a function, if you look at the generated code for a simple component with a counter you will find this:

function instance($$self, $$props, $$invalidate) {
	let count = 0;
	const click_handler = () => $$invalidate(0, count++, count);
	return [count, click_handler];
}

class App extends SvelteComponent {
	constructor(options) {
		super();
		init(this, options, instance, create_fragment, safe_not_equal, {});
	}
}

Each component creates a new instance of the component class (here App) which has its own internal state, created via the instance function.

(In Svelte 5 components themselves are functions rather …

Replies: 1 comment 6 replies

Comment options

You must be logged in to vote
6 replies
@MarArMar
Comment options

@MarArMar
Comment options

@brunnerh
Comment options

Answer selected by MarArMar
@MarArMar
Comment options

@brunnerh
Comment options

@MarArMar
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants