Frontend Instrumentation (static)
Instrument a built web application with slnodejs for browser coverage: commands, output, multiple environments, the agent configuration file, Collector and token options.
Applies to: web applications that run in a browser: React, Angular, Vue, Svelte and other bundled JavaScript.
Front-end code is instrumented before deployment. slnodejs writes an instrumented copy of your built files. Each instrumented file loads the SeaLights Browser Agent, which collects coverage in the browser while tests run and sends it for the build's lab ID. You deploy the instrumented copy to the test environment instead of the original build.
This page covers the slnodejs side: which command to run and what it writes. Framework-specific setups (Angular, React, Vue, Vite, webpack, esbuild, Rsbuild, Rollup, micro-frontends) are in Browser Agent → Framework and Bundler Setups, and every flag is explained in the Flags and Settings Guide. How the agent behaves in the browser (lab ID resolution, runtime overrides, context propagation, content security policy, troubleshooting) is in the Browser Agent section.
Choose a command
Scan the build and instrument it in one step (recommended)
slnodejs scan --instrumentForBrowsers --outputPath <dir>
Instrument again without reporting a new build
slnodejs scan --instrumentForBrowsers --instrumentationOnly --outputPath <dir>
Instrument separately from the scan, for example once per environment
Scan with --useRelativeSlMapping, then slnodejs instrument --workspacePath <dir> --outputPath <dir>
Build with source maps
Your build must produce source maps with column information next to the bundles. See Scanning → Front-end builds.
Scan and instrument
printf '%s' "$SEALIGHTS_AGENT_TOKEN" > sltoken.txt
npx slnodejs config --tokenFile sltoken.txt --appName my-web-app --branchName main --buildName "1.0.$BUILD_NUMBER"
npx slnodejs scan --tokenFile sltoken.txt --buildSessionIdFile buildSessionId \
--scanDir dist --scmType git --es6Modules \
--instrumentForBrowsers --outputPath sl_dist \
--labId qa-env \
--excludeSourceMapFiles skipCopy--instrumentForBrowsers --outputPath sl_dist
Writes the instrumented copy to sl_dist.
--es6Modules
Most modern bundles are ES modules.
--labId qa-env
The lab ID the browser agent reports with. Coverage goes to the test stage open on that lab. Without it, the build session ID is used.
--excludeSourceMapFiles skipCopy
Keeps the source maps out of the deployed output.
Deploy the instrumented output
Deploy the contents of sl_dist to the test environment in place of dist. For example, swap the folders before your deployment step:
mv dist dist_original
mv sl_dist dist
# deploy dist/ to the test environmentBuild production artifacts from a clean build, not from this folder.
Check that it works
Open a page of the test environment, then the browser developer tools console. A working agent logs Sealights browser version <version> has been loaded, and window.$SealightsAgent is defined. See Browser Agent → Check that it works.
Run your tests
Run the tests with a test stage open on the same lab ID. See Capturing Tests → Functional tests.
What slnodejs writes
All files of the scanned directory are copied to
--outputPath, and the JavaScript files in the copy are instrumented. The scanned directory is not changed, except with--excludeSourceMapFiles skipAndDelete.--outputPathmust differ from the input directory (--scanDirforscan,--workspacePathforinstrument).Files matched by
.slignoreare not instrumented. Files matched by.slignore.instrumentare scanned but not instrumented. By default, common framework packages (Angular, React, Vue,core-js,tsliband others) inside the bundles are not instrumented. See Configuration → Ignore files.Vue single-file components (
.vue) built with webpack (vue-loader) or Vite are reported on the.vuefile itself. The.vuesource files must be present whereslnodejs scanruns.
Multiple test environments
Each environment needs its own lab ID, so coverage from one environment does not land in another environment's test stage.
Option A: instrument once per environment. Scan once with --useRelativeSlMapping, then run instrument for each environment with its own --labId and --outputPath:
Option B: instrument once, set the lab ID at runtime. Each environment sets window["__sl.labId__"] in a script that runs before the instrumented files, for example from the environment's HTML template:
It takes precedence over the lab ID the build was instrumented with. See Browser Agent → Lab ID.
Agent configuration file
By default, the browser agent configuration is embedded in each instrumented file. With --splitPreambleIntoFile, it is written to one separate file in the output directory (sl-preamble-config.js, or --preambleFileName), which the browser loads from {preambleFileUrl}/{preambleFileName}.
Deploy the configuration file with the instrumented files, and serve it non-cacheable. When the browser cannot load it, the agent does not load.
Without
--preambleFileUrl, or with a relative value, the browser resolves the file relative to the page URL (or<base href>). On a deep route of a single-page application (for example/orders/42) it then requests the file below that route and gets a 404. Pass a full URL, or set<base href>.--preambleFileUrlaccepts a full URL, or a relative path without a leading or trailing/.
Toggle-based instrumentation. With --toggleBasedBrowserInstrumentation (which requires --splitPreambleIntoFile), slnodejs writes <name>_original.js and <name>_instrumented.js and replaces each file with a loader. The loader checks for /sl-preamble-config.js at the web server root (whatever --preambleFileName and --preambleFileUrl say) and loads the instrumented or the original file as a classic script. This lets one deployment run with or without SeaLights, for classic-script bundles; it does not fit ES module bundles. Without --splitPreambleIntoFile, slnodejs scan prints toggleBasedBrowserInstrumentation can only be used when splitPreambleIntoFile is true. and neither scans nor instruments. See the limits.
Collector and token exposure
By default, the agent token is written into the browser configuration of every instrumented build, so anyone who can open the deployed application can read it.
To keep it out, send browser coverage through a SeaLights Collector:
The preamble then loads the agent from the Collector, and the agent sends coverage to the Collector. Without --collectorUrl (or --overrideServer), --excludeTokenFromBrowserConfig only prints a warning, and the browser agent calls SeaLights without a token. See also Frontend - Prevent Token Exposure in Instrumented Applications.
Restrict where the agent loads
--allowedDomains app.qa.example.com,app.staging.example.com loads the browser agent only when the page's host name is in the list. Use it when the same instrumented files can be served on other domains.
Large applications and page load time
Tests time out on page load after instrumentation
SL_reduceInstrumentedFileSize=true on the scan --instrumentForBrowsers step (instruments without line-level counters; much smaller files). instrument does this by default.
The sl-mapping download is large
--footprintsMapping server (SeaLights resolves the coverage; the browser does not download the sl-mapping).
Code-split or lazy-loaded chunks
No extra setup: every chunk of an instrumented build reports to the same build session.
Vendor bundles without source maps are instrumented
--skipInstrumentingFilesWithoutSourceMap, or list them in .slignore.instrument.
Context propagation to back-end services
To attribute back-end coverage to the same browser test, instrument with --enableOpenTelemetry and --allowCORS. See Browser Agent → Context Propagation.
Settings without a flag
scan --instrumentForBrowsers and instrument also read these environment variables:
SL_reduceInstrumentedFileSize
true instruments without line-level counters.
SL_minifyInstrumentedOutput
true minifies the instrumented output.
SL_inProcessInstrumentation
true instruments in the slnodejs process instead of a pool of child processes.
SL_fileExtensions
Replaces the list of scanned and instrumented extensions, for example .js,.mjs.
Settings that are written into the build for the browser agent (for example SL_blockBrowserHttpTraffic) are in Browser Agent → Configuration.
Troubleshooting
Agent not loading, no footprints, sl-mapping not found, missing context headers: Browser Agent → Troubleshooting.
instrumentfails withBuild was performed without 'useRelativeSlMapping' flag: scan the build with--useRelativeSlMapping.instrumentrejects--scanDir: use--workspacePath.
Last updated
Was this helpful?

