Web Viewer commands and React hooks
While most of this reference covers your app requesting data from FileMaker, commands handle the reverse direction: a FileMaker script reaching into an active Web Viewer application to call a pre-registered JavaScript function (for example, refreshing a screen, opening a record, displaying a notification, or navigating routes).
For app-to-FileMaker requests, see Script calls.
How FileMaker triggers a command
FileMaker executes commands using the Perform JavaScript in Web Viewer script step, specifying the Web Viewer object name, function name, and optional parameters. Parameters passed from FileMaker arrive as strings.
Registered commands reside on a global object on window, keyed by namespace (window.adk by default). If a script step calls adk.openCustomer with parameter "123", it invokes the registered openCustomer handler with "123". If a handler requires numeric or object values, parse them (e.g. via JSON.parse) inside the handler.
Initialization
Invoke initWebViewerCommands() during application startup (e.g. in main.tsx) before FileMaker scripts trigger commands:
initWebViewerCommands();
The module is safe to import in non-browser environments: initWebViewerCommands() returns undefined when window is unavailable, making it safe during SSR or build steps.
Type declarations
Declare command signatures in a .d.ts file by extending WebViewerCommandRegistry in @adk/fmdapi/commands. DefineWebViewerCommandRegistry validates at compile time that every command accepts only string arguments:
export {};
declare module "@adk/fmdapi/commands" {
interface WebViewerCommandRegistry
extends DefineWebViewerCommandRegistry<{
openCustomer: (recordId: string) => void;
refreshDashboard: () => void;
}> {}
}
You can also wrap individual handlers with WebViewerCommandHandler:
declare module "@adk/fmdapi/commands" {
interface WebViewerCommandRegistry {
openCustomer: WebViewerCommandHandler<(recordId: string) => void>;
}
}
Both registerWebViewerCommand and useWebViewerCommand reference this registry to validate command names and parameter types at compile time.
Static command registration
Use registerWebViewerCommand for handlers independent of React component state:
registerWebViewerCommand("loadCustomerSummary", async (recordId) => {
const summary = await fmFetch("Load Customer Summary", { recordId });
window.dispatchEvent(
new CustomEvent("customer-summary-loaded", {
detail: summary,
}),
);
});
registerWebViewerCommand initializes commands automatically if not already invoked. It returns a cleanup function to unregister the handler:
const unregister = registerWebViewerCommand("loadCustomerSummary", handler);
// Cleanup:
unregister();
React hook integration
Use useWebViewerCommand from @adk/fmdapi/react when a handler interacts with component state, routing, or hooks:
export function CustomerScreen() {
useWebViewerCommand("openCustomer", (recordId) => {
console.log(recordId);
});
return null;
}
The hook binds the command on mount and unbinds on unmount, always invoking the latest render callback without requiring useCallback.
Missing-command buffering
If FileMaker executes a command before your application finishes registering it during startup, ADK buffers calls by default and replays them upon handler registration:
initWebViewerCommands({
missingCommand: "buffer",
maxBufferedCallsPerCommand: 100,
});
missingCommand also accepts "drop" (silently ignore), "warn" (log console warning), and "throw" (raise error). maxBufferedCallsPerCommand limits buffer size per command name; full buffers drop the oldest entry with a warning.
Namespaces and registry inspection
Commands register under window.adk by default. Specify namespace to configure custom global objects:
initWebViewerCommands({ namespace: "reportsApp" });
getWebViewerCommandRegistry() returns the active namespace registry to inspect registered or pending calls during debugging:
const registry = getWebViewerCommandRegistry();
registry?.listRegisteredCommands(); // e.g., ["openCustomer", "refreshDashboard"]
registry?.listPendingCommands(); // Commands called by FileMaker prior to registration
unregisterWebViewerCommand(name) removes a command explicitly outside component lifecycles.
Note Direct window assignment: Assigning handlers directly to window.adk.openCustomer = ... bypasses buffering, error handling, and type safety. allowDirectAssignment defaults to false. Use registerWebViewerCommand or useWebViewerCommand.