npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

svelte-on-demand

v1.0.0

Published

Compile and serve on-demand

Downloads

98

Readme

⚡ Svelte On-Demand

Compile, render and hydrate Svelte components at runtime. No build step required.

Svelte On-Demand is a Node.js runtime engine that compiles and renders Svelte components on demand, directly in production.

It enables dynamic SSR without a pre-build pipeline, making it ideal for environments where Svelte code changes frequently after deployment.


🚀 Why Svelte On-Demand?

Modern Svelte setups assume that:

All code is known before deploy.

Svelte On-Demand assumes the opposite:

Code changes. The runtime adapts.

Instead of building everything upfront, components are compiled only when requested, cached by content hash, and reused until they change.


✨ Key Features

  • On-demand Svelte compilation (SSR + DOM)
  • 🧠 Content-hash–based cache
  • ♻️ Automatic cleanup of outdated builds
  • 🔥 Real hot reload in production
  • 🌐 SSR with automatic browser hydration
  • 🧩 Simple Express integration
  • 📦 No external bundler or build pipeline

💡 Ideal Use Cases

  • CMS platforms
  • Admin panels
  • Multi-tenant apps
  • White-label products
  • Internal tools
  • Rapid prototyping
  • Custom SSR / template engines

If your Svelte code can change after deploy, this library is designed for you.


📦 Installation

npm install svelte-on-demand

Or install dependencies manually:

npm install svelte svelte/compiler rollup rollup-plugin-svelte \
@rollup/plugin-node-resolve @rollup/plugin-commonjs express

🗂 Expected Project Structure

project/
├─ components/
│  ├─ Home.svelte
│  ├─ About.svelte
├─ svelteEngine.js
├─ server.js

🚀 Basic Usage

server.js

const express = require('express');
const SvelteEngine = require('svelte-on-demand');

const app = express();
const engine = new SvelteEngine();

engine.mountSvelteCache(app);

app.get(
  '/view/:component',
  engine.renderView('components', {
    appName: 'Svelte On-Demand'
  })
);

app.listen(3000, () => {
  console.log('Server running at http://localhost:3000');
});

Now access:

http://localhost:3000/view/Home

🧠 How It Works

  1. The .svelte file is read from disk

  2. A content hash (MD5) is generated

  3. If no cached build exists:

    • Compile SSR output
    • Compile DOM output (via Rollup)
  4. SSR renders HTML and CSS

  5. The DOM bundle is loaded in the browser and hydrated

All of this happens at runtime.


🗃 Cache Strategy

Compiled artifacts are stored in:

.svelte-cache/

Example:

Home.SSR.abc123.mjs
Home.DOM.abc123.mjs
Home.hash
  • Cache is immutable per content hash
  • Any source change generates a new build
  • Old builds are automatically removed
  • Cache is CDN-friendly

🔥 Hot Reload in Production (Real One)

Svelte On-Demand invalidates the Node.js module cache before loading SSR output:

delete require.cache[modulePath];

This guarantees:

  • No stale SSR code in memory
  • No process restart
  • No global rebuild
  • Reload happens only when the component changes

This is not frontend HMR — it’s runtime module reloading.


🧩 API

new SvelteEngine(cacheDir?)

const engine = new SvelteEngine('.svelte-cache');

engine.renderView(componentsDir, globalData?)

Returns an Express middleware.

app.get(
  '/view/:component',
  engine.renderView('components', {
    user: 'John'
  })
);

Final component props:

{ ...globalData, ...req.query }

engine.mountSvelteCache(app)

Serves compiled DOM bundles:

engine.mountSvelteCache(app);

engine.listViews(dir)

Lists available components:

engine.listViews('components');

🧪 Performance Notes

  • First request triggers compilation
  • Subsequent requests use cached SSR
  • DOM bundles are static assets
  • Works very well behind a CDN (Cloudflare, Fastly)

In real production usage, compilation cost happens once per version, not per request.


⚠️ When NOT to Use

  • Static marketing sites
  • Fully predictable apps
  • Ultra-latency-sensitive cold starts

For these cases, traditional build-based SSR still wins.


🛣 Roadmap

  • Native ESM support
  • Concurrent build pipeline
  • Advanced cache GC (LRU / TTL)
  • Plugin system
  • Watch mode
  • Cloudflare Workers adapter

🧪 Status

🟡 Experimental, but functional and production-tested in controlled environments.


📜 License

MIT


Build is an optimization. Runtime is a choice. 🚀