-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Add FAQ entry about client-side init functions #2965
Conversation
Demonstrate how to guarantee that a client-side initialization function will be called only once per client.
|
The unit test failure is likely a flaky test that needs fixing, as it failed due to a timeout rather than an actual error. Plus, since this PR made no code changes, it's almost certainly not caused by the PR. I don't seem to have sufficient repo permissions to request a re-run of all jobs on the failing workflow, though. Someone with write access to the repo will probably have to do that. |
If no unsubscription is required, isn't this equivalent? // clientConnection.ts
import { initalizeConnection } from 'some-library';
export const connection = initializeConnection('params'); import { connection } from '$lib/clientConnection.ts';
function getResult(query) {
return connection.get(query);
} Added a similar comment to #1538 |
|
||
```js | ||
// clientConnection.ts | ||
import { initalizeConnection } from 'some-library'; |
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.
initalizeConnection
typo
} | ||
``` | ||
|
||
This technique uses the [initalization function of Svelte stores](https://svelte.dev/docs#svelte_store), which is called when the number of subscribers goes from zero to one (but not from one to two, etc) to set up a connection on demand. And because the connection is cached in an external variable, when one page unsubscribes from the store and a second page subscribes, the store's initialization function will be called again, but it will return the cached connection instead of creating a new one. |
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.
initalization
typo
Going to close this as I don't think there's any advantage to the store-based approach — thanks |
Demonstrate how to guarantee that a client-side initialization function will be called only once per client.
After writing this comment explaining how to call a library's init function only once on the client, I thought that that might be a generally-useful technique that should be documented. The FAQ section on integrations seems like the right place to put it.
No tests or changesets because this is a documentation-only PR. If a changeset should be added even to doc-only PRs, let me know and I'll be happy to add one.
Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
Tests
pnpm test
and lint the project withpnpm lint
andpnpm check
Changesets
pnpx changeset
and following the prompts. All changesets should bepatch
until SvelteKit 1.0