Skip to content

Hooks reference

Every hook is optional, and is a named export of fsn.ts. The Hooks type from @fsn.dev/cli has the signature of each:

fsn.ts
import type { Hooks } from '@fsn.dev/cli';

export const quit: Hooks['quit'] = (event, exitCode) => {
  console.log(`Quit with exit code ${exitCode}.`);
};

In the signatures below, Event, BrowserWindow and WebContents are Electron's types. The examples leave out the import of Hooks.

ready#

ready(): void | Promise<void>

Called once, when the app is ready and is serving its hosts, just before FSN opens the first windows. It's the place for set-up that the windows depend on, such as the application menu.

If ready returns a promise, FSN waits for it before opening the windows. Keep the wait short: until it's over, the app is running with nothing on screen.

The hosts already answer requests when it's called, so ready can call them with fetch and open windows of its own.

export const ready: Hooks['ready'] = async () => {
  // Have the server bring its database up to date before any page loads.
  await fetch('http://native.localhost/api/migrate', { method: 'POST' });
};

This isn't Electron's ready event. A listener for that, or code that waits for app.whenReady(), runs earlier: before the hosts are being served.

activate#

activate(event: Event, hasVisibleWindows: boolean): void

macOS only. Called when the app is activated, for example when its Dock icon is clicked. It's the listener for Electron's activate event.

Without this hook, FSN reopens the hosts' windows if none is open, which is what a Mac app that outlives its last window is expected to do. Exporting activate replaces that, so reopening a window becomes the hook's job:

import { BrowserWindow } from 'electron';

export const activate: Hooks['activate'] = (_event, hasVisibleWindows) => {
  if (hasVisibleWindows) return;
  const window = new BrowserWindow({ width: 1024, height: 768 });
  window.loadURL('http://native.localhost/');
};

FSN starts listening once it has opened the first windows, so the hook isn't called for the activation that comes with starting the app.

windowAllClosed#

windowAllClosed(): void

Called when the last window closes. It's the listener for Electron's window-all-closed event.

Without this hook, FSN quits the app, except on macOS, where apps usually stay running. Exporting windowAllClosed replaces that. To quit on macOS as well:

import { app } from 'electron';

export const windowAllClosed: Hooks['windowAllClosed'] = () => {
  app.quit();
};

A hook that does nothing keeps the app running on every platform.

The hook isn't called when the windows close because the app is quitting, as they do after Cmd+Q or app.quit(). That path leads to willQuit.

beforeQuit#

beforeQuit(event: Event): void

Called when the app has been asked to quit, before it closes its windows. event.preventDefault() cancels the quit. It's the listener for Electron's before-quit event.

import { dialog } from 'electron';

export const beforeQuit: Hooks['beforeQuit'] = (event) => {
  const choice = dialog.showMessageBoxSync({
    type: 'question',
    message: 'Quit while the export is still running?',
    buttons: ['Quit', 'Cancel'],
    cancelId: 1,
  });
  if (choice === 1) event.preventDefault();
};

willQuit#

willQuit(event: Event): void

Called when every window has closed and the app is about to quit. event.preventDefault() cancels the quit. It's the listener for Electron's will-quit event.

The hosts still answer fetch here, which makes it the place to have the server finish its work. The quit doesn't wait for a promise, though, so cancel it, do the work, and quit again: see Clean up before quitting.

quit#

quit(event: Event, exitCode: number): void

Called as the app quits, with the code it exits with. It's the listener for Electron's quit event. The quit can't be cancelled any more, and the process ends straight after, so only synchronous work is certain to finish.

openUrl#

openUrl(event: Event, url: string): void

macOS only. Called when the app is asked to open a URL, as when the user follows a link whose scheme the app handles. It's the listener for Electron's open-url event.

When following a link is what starts the app, the hook is called before ready: no host is being served yet, and no window exists. Keep the URL until then:

let started = false;
const waiting: string[] = [];

function handle(url: string) {
  // The app's own endpoint: its server decides what the link means.
  fetch('http://native.localhost/api/open-url', { method: 'POST', body: url });
}

export const openUrl: Hooks['openUrl'] = (event, url) => {
  event.preventDefault();
  if (started) handle(url);
  else waiting.push(url);
};

export const ready: Hooks['ready'] = () => {
  started = true;
  waiting.forEach(handle);
};

On Windows and Linux there is no such event. A link arrives as a command-line argument: in process.argv when it starts the app, and in the argv passed to secondInstance when the app is already running.

openFile#

openFile(event: Event, path: string): void

macOS only. Called when the app is asked to open a file, as when one is dropped on its Dock icon. It's the listener for Electron's open-file event. Call event.preventDefault() to tell macOS that the app is handling the file.

Like openUrl, it's called before ready when opening a file is what starts the app, and the same pattern keeps the path until the app can use it.

On Windows and Linux, the file's path arrives as a command-line argument, as described for openUrl.

secondInstance#

secondInstance(
  event: Event,
  argv: string[],
  workingDirectory: string,
  additionalData: unknown,
): void

Called in the running app when the user starts a second copy of it, with that copy's command-line arguments and working directory. It's the listener for Electron's second-instance event.

Electron only emits the event in an app that has asked for the single-instance lock, so the hook goes together with a call to app.requestSingleInstanceLock() at the top of fsn.ts: see Allow only one instance. additionalData is whatever the second copy passed to that call.

browserWindowCreated#

browserWindowCreated(event: Event, window: BrowserWindow): void

Called for every window as it's created: the ones FSN opens for your hosts, and any you open yourself. It's the listener for Electron's browser-window-created event.

This is how to reach the windows FSN opens, which it creates at 1024 by 768 with Electron's default options:

export const browserWindowCreated: Hooks['browserWindowCreated'] = (
  _event,
  window,
) => {
  window.setMinimumSize(640, 480);
};

The window has only just been constructed, and hasn't loaded anything yet. What Electron fixes when a window is constructed, such as its frame and its webPreferences, can't be changed from here.

webContentsCreated#

webContentsCreated(event: Event, webContents: WebContents): void

Called for every web contents as it's created. That's the page inside each window, FSN's windows included, and for a window it's called just before browserWindowCreated. It's the listener for Electron's web-contents-created event.

Use it for rules that should hold for every page, such as where links are allowed to lead: see Open external links in the browser.