Skip to content

The Vite plugin ​

The worker needs the fetcher module's default export. Vite's ?worker&url bundles a module as a worker entry, without its exports, so it does not fit the fetcher. fetcherUrl() from @anishbenji/tscache/vite gives a ?fetcher import instead: the module's URL with its exports kept.

ts
// vite.config.ts
import { fetcherUrl } from "@anishbenji/tscache/vite";
import { defineConfig } from "vite";

export default defineConfig({
  plugins: [fetcherUrl()],
  // An ES worker keeps the persistence code a chunk of its own, downloaded
  // only by a worker that persists (Vite's default 'iife' inlines it).
  worker: { format: "es" },
});
ts
import { createClient } from "@anishbenji/tscache";
// Vite: `?worker&url` bundles the module worker and gives its served URL.
import workerUrl from "@anishbenji/tscache/worker?worker&url";
// @anishbenji/tscache/vite's plugin: the fetcher module's URL, its exports kept.
import fetcherModule from "./fetcher.ts?fetcher";

const client = await createClient({
  workerUrl,
  fetcher: { module: fetcherModule },
});
console.log(client.mode); // "shared", or "dedicated" where SharedWorker is missing

worker: { format: "es" } is not the plugin's setting but the worker's: with Vite's default, 'iife', the worker's persistence code is inlined into the worker file, and with 'es' it is a chunk the worker loads only when a cache persists (Workers and fallback). It applies to every worker the app builds: a worker of your own that you create from a ?worker&url import must then be a module worker ({ type: 'module' }); a ?worker constructor import sets that itself. The plugin does not set the format for you, since it would change your own workers too; instead, vite build with the format left at 'iife' prints one warning that names this setting. The dev server never warns. An app that keeps 'iife' on purpose can silence the warning.

For TypeScript, declare the import once:

ts
// What an app's bundler declares for these imports. Vite's client types
// declare `?worker&url`; `?fetcher` is @anishbenji/tscache/vite's (the worker setup
// guide).
declare module "*?worker&url" {
  const url: string;
  export default url;
}
declare module "*?fetcher" {
  const url: string;
  export default url;
}

It needs Vite 8; vite is an optional peer dependency of tscache, and the plugin imports nothing from Vite at runtime.

Keeping IIFE workers ​

An app that keeps worker: { format: 'iife' } on purpose, and accepts the persistence code in its worker file, can silence that one warning with a filter on Vite's logger. The plugin has no option for it. The filter replaces warn on the logger it is given, not on a copy, since Vite records on that object (hasWarned) that a warning was logged. It drops a message that starts with [tscache] Vite builds workers as IIFE and passes every other message, with its options, to the original warn:

ts
// vite-logger.ts
import type { Logger } from "vite";

/**
 * Silences tscache's warning about IIFE workers, for an app that keeps
 * `worker: { format: 'iife' }` on purpose. The warning's opening words are a
 * stable contract of tscache; every other message is logged as before.
 */
export function withoutTscacheIifeWarning(logger: Logger): Logger {
  const warn = logger.warn;
  // Replace warn on this logger, not on a copy: Vite keeps hasWarned on it.
  logger.warn = (message, options) => {
    if (message.startsWith("[tscache] Vite builds workers as IIFE")) return;
    warn.call(logger, message, options);
  };
  return logger;
}

Give Vite a logger of its own, createLogger(), with the filter applied:

ts
// vite.config.ts of an app that keeps IIFE workers on purpose: the imports
// of the config above, these two, and this config as the default export
import { createLogger } from "vite";
import { withoutTscacheIifeWarning } from "./vite-logger";

export const iifeConfig = defineConfig({
  plugins: [fetcherUrl()],
  // Logs every message except tscache's IIFE warning. A customLogger replaces
  // Vite's own logger, so logLevel and clearScreen (and their CLI flags) no
  // longer apply: pass them to createLogger instead, for example
  // createLogger("warn", { allowClearScreen: false }).
  customLogger: withoutTscacheIifeWarning(createLogger()),
});

A customLogger replaces the logger Vite would build itself, so logLevel and clearScreen in the config (and --logLevel, --clearScreen on the command line) no longer reach it. Set them on the logger you create instead, for example createLogger("warn", { allowClearScreen: false }).

The warning's opening words, [tscache] Vite builds workers as IIFE, are a stable contract: changing them would break such filters, so tscache treats any change to them as a breaking change. The filter matches the start of the message, not the words anywhere in it, so a message that merely quotes them is still logged.

What it does ​

Where?fetcher gives
a client buildthe URL of a chunk of its own, with its signature preserved
the dev server, in the browserthe URL of the module, served from source
a server builda file: URL of the chunk, which server code can import()
the dev server, in server codethe file's absolute path, which Vite's module runner resolves

It also adds @anishbenji/tscache to ssr.noExternal, so in dev the library's import() of the fetcher goes through Vite's module runner rather than plain Node. Nothing to configure for server rendering.

The fetcher rule: import statically ​

Vite wraps every bundled import() in its module-preload helper, which reads document and, when the import fails, calls window.dispatchEvent. A worker has neither, so the fetch would throw a ReferenceError instead of its own error, an expired-token error included, which would then never reach the other tabs. fetcherUrl() fails the build when the fetcher, or any code it reaches, uses import(), naming the chunk. build.modulePreload: false does not remove the helper.

A fetcher that needs import() ​

Build it on its own, as a static file, with a bundler that keeps the entry's exports and adds no DOM code (Bun, or Vite's library mode), and pass its URL without ?fetcher:

sh
bun build src/fetcher.ts --splitting --format esm --outdir public/fetcher
ts
const client = await createClient({
  workerUrl,
  // Vite serves public/ under `base`; here `base` is "/" or "/app/".
  fetcher: { module: `${import.meta.env.BASE_URL}fetcher/fetcher.js` },
});

This works when base is an absolute path; with a relative base, give the deployed path yourself. The step runs outside your app's build: rebuild the fetcher when it changes, repeat any aliases or define values it needs, and give its URL a version that changes with its contents (a query such as ?v=3, or a versioned path), since its name carries no hash: tscache starts a new worker for a new fetcher URL, while a running worker keeps the module it loaded from an unchanged one (Stale scripts after a deploy). The worker setup guide has the Vite library-mode config.