Application Client Runtime
Relevant source files
The following files were used as context for generating this wiki page:
The Budibase Application Client Runtime is a Svelte-based single-page application (SPA) that renders and executes published Budibase applications. It is a dynamic system that fetches an application's definition as a JSON object from the server and uses it to construct a fully interactive web application in the user's browser. This runtime is responsible for rendering components, managing application state, handling user interactions, and communicating with the Budibase backend.
The core of the runtime resides in the @budibase/client package. It is loaded and initialized by a server-rendered Svelte template, which acts as the main entry point for any deployed Budibase application. The runtime dynamically loads component libraries and plugins, providing an extensible and flexible user experience.
Architecture Overview
The client runtime architecture is composed of several key stages: server-side rendering of the initial HTML, client-side bootstrapping, state initialization, and dynamic component rendering.
The following diagram illustrates the high-level architecture and the relationship between the main modules.
Bootstrapping Process
The application bootstrap process begins when a user navigates to a published app's URL. The server responds with an HTML document that loads the necessary scripts to initialize the client-side application.
1. Initial HTML Load
The server-side controller serveApp in packages/server/src/api/controllers/static/index.ts handles the initial request. It gathers application metadata, theme information, and feature flags, then uses BudibaseApp.svelte to server-side render the initial HTML page. This template includes meta tags, favicons, and crucially, the script tags to load the client library and its dependencies.
A key part of this template is the bootloader script, which ensures that the main client library and any required plugins are loaded before the application is initialized.
Sources: packages/server/src/api/controllers/static/index.ts:133-241, packages/server/src/api/controllers/static/templates/BudibaseeApp.svelte:144-196
2. Client-Side Initialization
The following sequence diagram shows the client-side bootstrapping flow, which starts after the initial HTML is loaded in the browser.
This diagram shows the sequence from the initial HTML load to the mounting of the main Svelte application.
Sources: packages/server/src/api/controllers/static/templates/BudibaseApp.svelte:168-196, packages/client/src/sdk.js:10-43
The loadBudibase function in sdk.js is the main entry point for the client-side application. It performs these critical steps:
- Fetches the complete application definition (
app package) from the/api/applications/:appId/packageendpoint. - Initializes all Svelte stores, including the central
appstore, with the data from the app package. - Instantiates and mounts the root
ClientApp.sveltecomponent to the<body>of the document.
Core Components
ClientApp.svelte
This is the root component of the application. It establishes the main structure of the UI, including:
- Routing: It uses
svelte-spa-routerto manage navigation between different screens. - Layout: It renders the main application layout, which can include a header, side navigation, and footer.
- Screen Rendering: It acts as the parent for the currently active screen, rendering its component tree.
Sources: packages/client/src/components/ClientApp.svelte
Component.svelte
This is a recursive component that serves as the rendering engine for the entire application UI. For each component defined in a screen's JSON structure, Component.svelte is responsible for:
- Dynamically importing the corresponding Svelte component from a component library (e.g.,
@budibase/bbui,@budibase/standard-components, or a plugin). - Passing the component's properties (
props) to the imported component. - Recursively rendering any child components by creating new instances of
Component.sveltefor each child.
The following flowchart illustrates this recursive rendering logic:
This recursive process allows the runtime to build complex UIs from a declarative JSON structure.
Sources: packages/client/src/components/Component.svelte
State Management
Application state is managed using Svelte stores. The primary store is defined in packages/client/src/stores/app.ts.
The app Store
This store is a writable Svelte store that holds the global state of the application. It is initialized once during the bootstrap process with data from the fetched app package.
Key properties managed by the app store include:
| Property | Type | Description |
|---|---|---|
appId | string | The unique identifier for the application. |
appName | string | The display name of the application. |
url | string | The base URL of the application. |
version | string | The current version of the client library. |
screens | object | An object containing the definitions for all screens in the app. |
pwa | object | Progressive Web App configuration. |
theme | string | The name of the current theme (e.g., "light", "dark"). |
currentUser | object | Information about the currently logged-in user. |
features | object | A map of enabled application-level feature flags. |
componentLibraries | string[] | A list of component libraries used by the app. |
usedPlugins | object[] | An array of plugin definitions used in the app. |
Sources: packages/client/src/stores/app.ts:13-39, packages/client/src/sdk.js:31-33
Summary
The Budibase Application Client Runtime is a sophisticated system designed to render dynamic, data-driven applications from a JSON definition. It leverages server-side rendering for a fast initial load and then takes over on the client side to bootstrap a full-featured Svelte SPA. Through a combination of dynamic component imports, recursive rendering, and centralized state management, it provides a powerful and extensible platform for running low-code applications. The separation of the application definition (JSON) from the rendering engine (@budibase/client) is a core architectural principle that enables the flexibility of the Budibase platform.