How it works
An FSN app is an Electron app whose main process is written by FSN. That one process runs your server code and opens your windows. This page describes what it does, which is worth knowing before you hook into it.
Hosts#
A host is a SvelteKit or Next.js package that the app serves. fsn.config.js gives each one a name:
/** @type {import('@fsn.dev/cli').Config} */
export default {
hosts: {
native: './native',
},
};Inside the app, a host named native is at http://native.localhost/. Most apps have a single host. An app can have several, each at its own origin and each with its own window.
Requests#
FSN handles the http: scheme itself, with Electron's protocol.handle. When a window requests a URL at http://<host-name>.localhost, FSN passes the request to that host's server as a standard Request, and gives the window the Response that comes back.
The request never leaves the process: no port is open, and there is nothing for another program to connect to. Requests to any other address, such as an API on the internet, go out over the network as usual.
A browser's network stack does some work that a request handled this way would miss, so FSN does it instead:
- Cookies. A
Set-Cookieheader from a host is stored in the app's cookie store, and sent back in theCookieheader of later requests. OriginandReferer. FSN sets both on the requests it passes on, so checks that rely on them, like SvelteKit's CSRF protection, work.- Requests from the server to itself. When code in the main process fetches a host's URL, FSN answers that in-process too. That covers server code calling its own origin, and
fetchin a hook.
Startup#
When the app starts, FSN's main process:
- Loads the project's
fsn.ts, if it has one. - Waits for Electron to be ready.
- Loads each host's server and starts serving it.
- Calls the
readyhook. - Opens a window for each host, at
http://<host-name>.localhost/.
From then on it follows the conventions of the platform. Closing the last window quits the app, except on macOS, where the app stays running and reopens its windows when it's activated. Hooks can change both.
Files and data#
A built app shouldn't write inside its own folder: it may be installed somewhere read-only, and on macOS changing it breaks the app's code signature.
For data that has to outlive a session, FSN sets process.env.FSN_DATA_DIR to the app's per-user data folder, the one Electron calls userData. On macOS that's ~/Library/Application Support/<app-name>. Server code can keep its files there, such as a SQLite database:
import { join } from 'node:path';
const database = join(process.env.FSN_DATA_DIR ?? '.', 'app.db');The variable is only set in the built app, so give it a fallback for when the host runs under its dev server.