-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Hooks running twice on build #8795
Comments
This is expected - the server code is initialized once for analysis, so that the optimal client bundle can be built, and once for prerendering once we have the optimal client bundle. If you need to do some processing before building happens then your hooks file is the wrong place for it, it should go in a separate script. If TypeScript is non-negotiable then just use ts-node. |
But I also need svelte-kit features and want to use the same code I'm using in svelte. |
Could you give a little more detail on what exactly your preprocessing thing does, and what SvelteKit features it's using and why? |
It's precompiling data for prerendered pages. |
Are you writing out to the filesystem? Can you bail if the file already exists? |
That is my current workaround. Every file (of many) gets a timestamp and I check if this timestamp was within a predefined time and bail if below that. Is providing a "onBuild" hook handler within the |
Can you point to some examples? It would be good to have a broad understanding of use cases before committing to an approach |
Not everyone has the same use-case. But overall, I do feel there is a question of "where to put a one time executed script which should run at the start / build". On script execution (in svelte.config.js, vite.config.js, ...)
I assume people who just request for there to be a .ts version of the config, that they want that due to more code in there and not just for the config part. On multi time execution I was sure I came accross multiple issues with people being confused on the matter but searching now, every one has diverse use-cases like e.g. this one: #6728 |
I might have a complication here. But at the time it fails, it should actually already have the files. So this seems a bit odd. Reproduction:
I thought this might be something to take into consideration in this issue. |
I have a conditional |
There's import { building } from '$app/environment' which is designed for use cases like this, so you don't need additional CI flags. |
Describe the bug
Pretext
I have some expensive preprocessing methods which need to run on build of my static website.
I first tried executing them in
svelte.config.js
but I couldn't use typescript there, then I triedvite.config.ts
but had other issues, then from inside the+layout.server.ts
but noticed that for my build this is called 15 times (this seems to be expected behavior, see: #6728).@KTibow on the Svelte Discord recommended me to use
hooks.server.ts
instead (https://kit.svelte.dev/docs/hooks).I put my data preprocessing code directly inside the
hooks.server.ts
without any of the hook handlers since they all seem for different purposes than what I need.I need my scripts to execute only once during build.
At the top I put a
console.log
which also printsMath.random()
.During a build I get this print twice.
Once after the console output for the prerendered pages:
And another time after the build completed.
The last one seems a bit odd to me. Why does it need to run again after it already completed the build?
Reproduction
src/hooks.server.ts
with aconsole.log(
hook execution ${Math.random()})
npm run build
/vite build
Logs
System Info
Severity
serious, but I can work around it
Additional Information
No response
The text was updated successfully, but these errors were encountered: