kit: init doesn't work as intended in production

Solution here

Describe the bug

Hi, Using init in a hooks.client.ts file would work as intended but not in production (Vercel in my case)

Reproduction

  • Create a hooks.client.ts with init that does some logic
  • Make sure the logic works in development environment
  • Host the project in Vercel
  • Check if the logic is working

Logs


System Info

System:
    OS: Windows 10 10.0.19045
  Binaries:
    Node: 20.9.0 - C:\Program Files\nodejs\node.EXE
  npmPackages:
    @sveltejs/adapter-auto: ^3.3.1 => 3.3.1
    @sveltejs/adapter-vercel: ^5.4.6 => 5.5.3
    @sveltejs/kit: ^2.7.3 => 2.16.0
    @sveltejs/vite-plugin-svelte: 4.0.0 => 4.0.0
    svelte: ^5.1.2 => 5.19.0
    vite: ^5.0.0 => 5.4.11

Severity

serious, but I can work around it

Additional Information

According to the docs:

This function runs once, when the server is created or the app starts in the browser, and is a useful place to do asynchronous work such as initializing a database connection.

Does this mean that the init function in a vercel instance runs ONLY when the server has started? But what about the second part of the statement? Am I misunderstanding the docs here?

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 24 (12 by maintainers)

Most upvoted comments

@retr0cube please provide a repro of your problem:

  • npx sv create my-repro
  • git init && git add --all && git commit -m "Initial commit"
  • Add the code that minimally illustrates your issue
  • git add --all && git commit -m "Reproduce issue"
  • Create a repository on GutHub and upload your code
  • Share the link here

We get a lot of issues, so being able to pull your code and deploy it rather than faff about talking to you and trying to figure out what the issue is by ourselves makes it a lot more likely we can get a fix out quickly 😉

There are really two init hooks…the client init hook which you are using runs once when the application start on the client (basically on every hard refresh).

The server one runs once every time the server starts the sveltekit application. In case of server less tho everytime your server less function goes to sleep is just like if the server shut down and with a new request it needs to spin up again (and the init hook will and need to rerun to re initialize everything)