-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Description
Is your feature request related to a problem? Please describe.
This is a request for discussion.
One problem brought up in the Discord server pretty often revolves around one-time setup that should happen at startup, where "startup" is either defined as "whenever the server starts" or, for adapter-static, "whenever the app runs on the user's browser".
In the FAQs, we have a "How do I setup a database?" section. This section documents functionality that is otherwise not documented anywhere, specifically around hooks.js. It says "You can execute any one-time setup code in hooks.js and import your database helpers into any endpoint that needs them." I imagine that would look something like this:
let ready = false;
const executeSetupActions = async (): void => {
const t1 = setupDb1();
const t2 = setupDb2();
const t3 = setupTelemetry();
const t4 = setupAuth();
await Promise.all([t1, t2, t3, t4]);
ready = true;
}();
const handle: Handle = () => {
// For server-side code, this shouldn't REALLY matter unless you receive a request in the short
// amount of time AFTER the server has started but BEFORE the connections are initialized
while (!ready) {
await new Promise(resolve => setTimeout(resolve, 2000));
}
}Describe the solution you'd like
- In the Hooks section of the documentation, document that setup code can be run in hooks outside of the two exported functions
getSessionandhandle. (It might be nice to also document what this does if you're usingadapter-static.) If the SvelteKit-approved place to run setup code is inhooks.js, it should be documented with the documentation abouthooks.js. - Include something like the code examples above in the "setting up a database" FAQ.'
- It might also be a good idea to just document how to set up singletons. Yes, you should know this as a JavaScript developer, but it's such a common use case that it might fit in nicely at the bottom of the FAQs.
Describe alternatives you've considered
- Just leave it alone and continue answering this question in the Discord... often.
How important is this feature to you?
Important enough that I'm willing to make said adjustments in a pull request if I get the go-ahead from a maintainer :)