Skip to content

SvelteKit

since: 1.7

@runwisp/js runs scheduled tasks inside your SvelteKit server, in vite dev and with adapter-node. It needs a process that keeps running, so it does not work with serverless adapters.

It works with SvelteKit 2.10.1 or later, including SvelteKit 3, and adapter-node 5.1 or later, on Node.js or on Bun.

Terminal window
npm install @runwisp/js

Install it as a dependency, not a dev dependency. adapter-node bundles dev dependencies into the build, and the bundled package can’t find the runwisp binary once dev dependencies are left out of the deploy.

Register tasks in the init hook, which SvelteKit runs once when the server starts:

src/hooks.server.ts
import { RunWisp } from "@runwisp/js";
import type { ServerInit } from "@sveltejs/kit/hooks";
import { building } from "$app/env";
import { cleanupSessions } from "#lib/server/sessions.ts";
export const init: ServerInit = () => {
// vite build runs init to prerender pages.
if (building) return;
const runwisp = new RunWisp();
runwisp.task("cleanup-sessions", { cron: "0 * * * *" }, async () => {
const removed = await cleanupSessions();
console.log(`removed ${removed} expired sessions`);
});
};

On SvelteKit 2, import ServerInit from @sveltejs/kit, building from $app/environment, and your own modules from $lib.

The server prints the dashboard address when the tasks start. See the JavaScript SDK for every task option.

In vite dev, SvelteKit runs init on the first request, so open the app once to start the tasks. When you edit the hook or a module it imports, the next request runs init again, and the tasks get the new code.

When the built server stops, on SIGTERM or Ctrl-C, RunWisp lets running tasks finish, then stops.

Terminal window
npm run build
node build

The data directory is .runwisp in the directory you start the server from. Start node build from the project root, or set data, since vite build replaces build/ and anything in it.

Build with adapter-node as above and start the server with bun build/index.js. On SvelteKit 3, you can use @sveltejs/adapter-bun instead.

@runwisp/js has node-cron’s API, so a cron.schedule() call in hooks.server.ts keeps working with const cron = new RunWisp(). Give every task a name. See From node-cron.