Introducing a new way to review your PRs.

Read the announcement

frappe / builder

Craft beautiful websites effortlessly with an intuitive visual builder and publish them instantly

View on GitHub

20 documents · Updated Nov 23, 2026

docs / client-side-code-and-data-blocks

Client-side code and data blocks

Client-side code and data blocks connect authored page structure to runtime behavior. Engineers use page and component scripts for browser behavior and server data scripts for values. Bindings and repeat blocks then place those values into authored content without changing the design tree.

This page is a child of Visual builder workspace. Read Block composition system for the block model, Property and style editing for binding controls, and Builder content documents for the authored document boundary.

Authored content and runtime values

The builder keeps design content separate from values that appear when a page runs. The authored block tree stores structure, styles, props, scripts, and binding rules. Page data, component data, and route values arrive later through the render or preview context.

Authored page and component content

An authored block is the stable design unit. It can contain normal content, a data key for its main value, extra dynamic values for other properties, and client-side code. A component stores the same kind of content as a reusable unit. Each instance can supply its own resolved props without turning runtime values into new authored content.

This separation lets one component definition serve many instances. Each instance keeps its own runtime context while the shared definition remains unchanged.

References: frontend/…/block.ts• L161-200 builder/utils.py• L120-147 frontend/…/doctypes.ts• L115-194

Runtime data sources

Dynamic values can come from three named sources. The resolver reads the selected source and applies its result to the target property. If a value is missing, the authored value remains available as the fallback.

SourceMeaningTypical owner
dataScriptData created for the current pagePage Data Script
componentDataData created for a component instanceComponent Data Script
propsValues passed to a block or componentBlock or component props

Runtime data is therefore an input to the design rather than a replacement for it.

References: frontend/…/blockValueResolver.ts• L29-73 frontend/…/blockValueResolver.ts• L75-97 frontend/…/DynamicValueHandler.vue• L116-172

Client scripts

Client scripts add browser behavior and styles to authored pages and reusable blocks. A named client-script resource stores JavaScript or CSS and can be linked to pages or components. JavaScript runs with a rendered block instance as its target and receives the data and props for that instance.

Page client scripts

Page client scripts are reusable JavaScript or CSS resources attached to a page. A page can create, attach, rename, remove, and reorder these resources. The same resource can serve more than one page, so an edit can affect every page that attaches it.

Attached resources form the page-wide behavior layer. This differs from a block script, which follows a rendered block instance and its local content.

References: frontend/…/PageClientScriptManager.vue• L6-100 frontend/…/PageClientScriptManager.vue• L246-401 frontend/…/pageStore.ts• L81-94 builder/…/builder_client_script.json• L8-45

Component and block client scripts

Component and block client scripts travel with their reusable definition. A component instance uses that script when it renders, so behavior and style stay with the component instead of one page. The JavaScript body receives component_data and props, while this identifies the current rendered instance.

The script can return a cleanup function for temporary work such as event listeners. Repeated items have separate instance identities, so one authored child can run once for each rendered item without sharing one target.

References: frontend/…/BuilderBlock.vue• L145-149 frontend/…/BuilderBlock.vue• L398-438 builder/…/builder_page.py• L1251-1296

Preview sandbox

The editor can execute block scripts in three modes: Don't Execute, Restricted, and Unrestricted. Restricted mode limits access to the canvas area and blocks common paths to the wider editor document. Unrestricted mode gives normal document access for scripts that need wider browser access.

The sandbox applies to editor execution. Published page scripts still run as authored browser code. A script that needs a blocked editor feature can therefore behave differently in preview and on the published page. Scripts should use the supplied instance and stable class hooks instead of depending on parent structure.

References: frontend/…/scriptSandbox.ts• L33-70 frontend/…/scriptSandbox.ts• L73-231 frontend/…/BuilderCanvas.vue• L479-505 frontend/…/doctypes.ts• L40-43

Code editing and completions

The code editor gives authors one editing surface for page, component, and block code. It supports the languages used by scripts and content fields, loads its CodeMirror editor on demand, and keeps editing state separate from the stored value.

Editor contexts

The active editing target determines which code and data surfaces are available.

ContextClient-side codeData code or preview
PagePage client scriptsPage Data Script and read-only page data preview
ComponentComponent client scriptComponent Data Script and read-only component data preview
Block templateBlock JavaScript and CSSBlock props; no page data editor

The shared editor supports JSON, HTML, Python, JavaScript, and CSS. It also supports search, history, bracket handling, optional line numbers, themes, and explicit save actions where the caller requires them.

References: frontend/…/PageScript.vue• L1-143 frontend/…/CodeEditor.vue• L44-80 frontend/…/CodeMirrorEditor.vue• L21-28 frontend/…/CodeMirrorEditor.vue• L128-160

Draft and save behavior

The editor keeps a draft separate from the current model value. Editors with a Save action emit changes only when the author saves. Other editable fields emit on blur. JSON is parsed before it is emitted, so invalid JSON stays in the draft and does not become a model value.

Read-only panes show runtime data without changing its source. A dirty marker shows that the editor contains unsaved source. When a value is updated from the outside, the editor resets unless the update is only the expected echo of the author's own change.

References: frontend/…/CodeEditor.vue• L87-177 frontend/…/CodeMirrorEditor.vue• L56-87

Completion sources

JavaScript completion suggests browser globals and properties reached from this. In block context it also suggests the selected block's merged props. Python completion starts with page and data names, then uses server-provided safe-global metadata for property access.

These suggestions help authors discover the available context. They do not make code valid or grant access beyond the actual browser sandbox or server execution rules.

References: frontend/…/CodeMirrorEditor.vue• L10-13 frontend/…/CodeMirrorEditor.vue• L112-125 frontend/…/createCodeMirrorState.ts• L166-187 frontend/…/jsGlobalCompletion.ts• L34-105 frontend/…/pythonCustomCompletion.ts• L14-127 builder/api.py• L698-745

Data-loader blocks

A data-loader block is a repeat block with one child design. It takes a collection from page data, component data, or a prop and renders that child for each item. The child receives the current item as its local value, so its bindings describe one item while the loader supplies the list.

Repeated content

The loader supports these collection paths:

  • Page Data Script output through dataScript.
  • Component Data Script output through componentData.
  • An array or object prop through props.

Arrays repeat in item order. Object props can provide key and value entries. The editor limits previewed collections to 100 entries so large data does not overwhelm the canvas. The authored repeat rule remains one child block and does not become one authored block per item.

This makes a loader the bridge between reusable content and runtime data. Each rendered copy receives its own data context and instance identity.

References: frontend/…/DataLoaderBlock.vue• L1-19 frontend/…/DataLoaderBlock.vue• L54-115 builder/…/builder_page.py• L1086-1198

Value binding and visibility

The main data key maps a source path to a block property such as text or an image attribute. Additional dynamic values map source paths to attributes or styles. A visibility condition uses the same source model and hides the block when its resolved value is false.

Bindings remain metadata on the authored block. Preview and page rendering resolve them with current page data, component data, props, and repeater context. The server applies the same source choice when it produces rendered output, which keeps preview and published behavior aligned.

References: frontend/…/DataKeySection.ts• L6-55 frontend/…/blockValueResolver.ts• L64-107 builder/…/builder_page.py• L1342-1418

Data script lifecycle

Data scripts run on the server and return values for client-side rendering. This keeps database reads and other server work out of browser scripts. Client scripts then focus on behavior after the page or component renders.

The flow below shows how authored bindings combine with server data and props before client behavior runs.

flowchart LR
    A[Authored blocks and bindings] --> B{Data source}
    B --> C[Page Data Script]
    B --> D[Component Data Script]
    B --> E[Props]
    C --> F[Resolved runtime values]
    D --> F
    E --> F
    F --> G[Text attributes styles and repeats]
    G --> H[Client scripts and browser behavior]

Page data lifecycle

The Page Data Script receives a data object and can add values for the page. It runs when page data is requested and during page rendering. Route variables are available to that request, so one page can resolve different values for different routes. The returned data feeds bindings, repeat blocks, visibility conditions, and the page data preview.

References: frontend/…/PageScript.vue• L59-83 builder/…/builder_page.py• L590-612

Component data lifecycle

The Component Data Script receives the component's resolved props and writes to a component object. The builder evaluates it for each component instance, then passes the result to that instance's blocks and client scripts. Two instances can therefore show different data while sharing one authored component definition.

References: builder/…/builder_component.py• L211-247 frontend/…/componentController.ts• L105-139 frontend/…/componentStore.ts• L456-494

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep props, dataScript, and componentData aligned across binding controls, the client resolver, server rendering, and repeater context.
  • When changing the shared code editor, keep draft updates, explicit saves, read-only state, JSON parsing, search behavior, language mode, and completion context aligned with every caller.
  • When changing editor sandbox behavior, check restricted preview separately from published-page execution.
  • When changing component props or component data, check preview evaluation and per-instance evaluation with the same inputs.
  • When changing repeater bindings, check both the one-child authored model and the per-instance identity used by scripts.
  • When changing dynamic binding fallbacks, keep editor resolution and server output consistent for empty, false, and missing values.

Important failure modes

TriggerConsequenceGuard
A client script needs unrestricted browser access while the editor uses Restricted modePreview behavior differs from published behaviorTest with the configured editor mode and use the supplied instance root where possible
JSON is invalid when the editor tries to emit itThe draft stays visible but the model keeps its previous valueFix the JSON before saving or relying on the emitted value
A completion source is treated as an execution permissionSuggested code fails in the actual sandbox or server contextTreat completions as authoring guidance and validate code at runtime
A component data script returns no usable dataComponent bindings and repeats resolve to empty valuesCheck the component data preview and server script error
A repeater path resolves to a non-collectionThe loader produces no useful repeated contentVerify the source path and collection shape in the data preview
A dynamic value uses the wrong comesFrom sourceThe block shows a blank or unrelated valueMatch the binding source with the script or props that create the data
A script selects authored structure instead of stable hooksHandlers miss rendered text or repeated instancesAdd explicit class hooks and select within the supplied instance root

Key files

FileWhy to read it
frontend/…/PageScript.vueShows how page, component, and block-template code contexts are presented and saved.
frontend/…/PageClientScriptManager.vueShows page-wide script attachment, reuse, and ordering.
frontend/…/CodeEditor.vueShows the shared draft, save, read-only, JSON, and external-value contract.
frontend/…/CodeMirrorEditor.vueShows editor lifecycle, language context, block props, search, theme, and cleanup behavior.
frontend/…/DataLoaderBlock.vueShows the runtime contract for repeat blocks and their data sources.
frontend/…/blockValueResolver.tsShows how source paths become block values and visibility results.
frontend/…/scriptSandbox.tsShows the editor's restricted and unrestricted client-script boundaries.
frontend/…/createCodeMirrorState.tsShows shared editor features and language-specific completion setup.
builder/…/builder_page.pyShows server page data evaluation, dynamic binding output, repeat rendering, and published block scripts.
builder/…/builder_component.pyShows the component data contract and server evaluation boundary.