Skip to content
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

init doesn't work as intended in production #13347

Open
retr0cube opened this issue Jan 19, 2025 · 3 comments
Open

init doesn't work as intended in production #13347

retr0cube opened this issue Jan 19, 2025 · 3 comments

Comments

@retr0cube
Copy link

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?

@paoloricciuti
Copy link
Member

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)

@retr0cube
Copy link
Author

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)

@paoloricciuti Hi thanks for the reply, but in my case the code inside the init hook doesn't even run

@elliott-with-the-longest-name-on-github
Copy link
Contributor

@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 😉

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

No branches or pull requests

4 participants