question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SvelteKit Process Hooks

See original GitHub issue

Is your feature request related to a problem? Please describe. As of right now, there’s no real way to hook into the SvelteKit process to run code before build, dev, or start.

The SvelteKit hooks.js (not sure if this idea below works better for the former setup.js file), has hooks for when a route is called serverside. However, it’d be nice to have hooks that would essentially correspond with svelte-kit build, svelte-kit dev, and svelte-kit start. Elder.js has “Build Hooks” that handle hooking into the build process. JungleJS also has ways of configuring async code in between build/start process steps.

Describe the solution you’d like My current idea is code like the following, probably in hooks.js.

export async function devProcess({ dev, port, config }) {
        //Fancy async code here

	return await dev({ port, config }); 
}

export async function buildProcess({ build, adapt, verbose, config }) {
        //Fancy async code here

        await build(config);

        return await adapt(config, { verbose });
}

export async function startProcess({ start, port, config }) {
        //Fancy async code here

        return await start(config);
}

And then the cli.js file would be like it is now, but if one of these above (ofc optional) options is specified it runs it instead of the default code. For example, for the dev command

//Somehow import the devProcess command from above as customDevProcess

.action(async ({ port, open }) => {
	process.env.NODE_ENV = 'development';
	const config = await get_config();

	const { dev } = await import('./core/dev/index.js');

	const devPromise = customDevProcess ? customDevProcess({ dev, port, config }) : dev({ port, config });

	try {
		const watcher = await devPromise;

		watcher.on('stdout', (data) => {
			process.stdout.write(data);
		});

		watcher.on('stderr', (data) => {
			process.stderr.write(data);
		});

		console.log(colors.bold().cyan(`> Listening on http://localhost:${watcher.port}`));
		if (open) launch(watcher.port);
	} catch (error) {
		handle_error(error);
	}
});

Describe alternatives you’ve considered My original idea were options for preDev, preBuild, and preStart just in the svelte.config.cjs, for simplicity sake. This is of course kinda messy, and probably not the most dynamic way to do it. But also, these options for “process hooks” might not make sense in the current hooks.js file.

How important is this feature to you? In my efforts to recreate the ✨magic✨ of JungleJS within SvelteKit (take a look at progress here), I hit a road block where this is kinda critical. As of rn I have a literal temp-cli.js file that I’m using to pull in dataSources in a separate command. I could make a legit CLI for JungleJS, and will probably have to do so if this isn’t pushed forward, but I feel this would be better as hooking into the whole SvelteKit process.

Additional context I decided to go ahead and make this an issue after seeing others ask for stuff that could be solved with this, one example here of where some sort of preStart hook would be useful.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:19 (14 by maintainers)

github_iconTop GitHub Comments

5reactions
Rich-Harriscommented, Mar 26, 2021

You can create db connections etc in src/hooks.js. Whether those connections are short-lived or long-lived depends solely on whether you deploy the app as a Node server or to a serverless platform

https://kit.svelte.dev/docs#hooks

4reactions
GCastilhocommented, Mar 28, 2021

If I can give my opinion, the documentation for the hooks module, https://kit.svelte.dev/docs#hooks, does not let it clear that it will keep a side-effect running in a node environment, like a database connection

I think that’s something that should be addressed, if that’s the case

Read more comments on GitHub >

github_iconTop Results From Across the Web

Hooks • Docs • SvelteKit
'Hooks' are app-wide functions you declare that SvelteKit will call in response to specific events, giving you fine-grained control over the framework's ...
Read more >
How To Make A SvelteKit Hooks Redirect - Kudadam
The Process. I am going to assume you already know how redirects work. Whenever we are making a redirect, we are normally changing...
Read more >
How to shutdown gracefully in sveltekit? - Stack Overflow
The brief answer is to set up process.on() handlers for exit and SIGINT that ... src/hooks.server.ts // One time setup code await import('....
Read more >
Learn How SvelteKit Works As a Framework - Joy of Code
SSR (server-side rendering) is the process of taking a request from the browser and returning a rendered version of the page as a...
Read more >
How to Use Hooks.js in Sveltekit - Morioh
In this video, I will be showing you how to use hooks.js in Sveltekit. ... I hope now you are totally aware of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found