Skip to content

Latest commit

 

History

History

html

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

README.md

RolloutCtrl plain HTML Example

A single index.html page that uses @rolloutctrl/js-sdk directly in the browser — no bundler, no build step. The SDK is loaded as an ES module from the esm.sh CDN.

The page evaluates the signup-button feature flag and renders the assigned variant's name directly as the call-to-action button label — whatever variant is configured in the dashboard (Sign Up, Free Trial 14 Days, Free Trial 30 Days, …) shows up on the button unchanged. Toggle the flag (or change its variant) in the RolloutCtrl dashboard and the UI updates automatically on the next poll.

Files

File Purpose
index.html The page itself — imports the SDK and renders the demo UI.
config.js Default SDK configuration (sdk key, API URL, environment, flag key, context).
config.local.example.js Template for local overrides. Copy to config.local.js (gitignored).
.gitignore Ignores config.local.js so secrets stay out of git.

Setup

  1. Open config.js (or copy config.local.example.js to config.local.js) and fill in your values:

    window.ROLLOUTCTRL_CONFIG = {
      sdkKey: 'rctrl-client-********',
      apiUrl: 'http://localhost:3000/api',
      environment: 'production',
      refreshInterval: 15000,
      flagKey: 'signup-button',
      context: { country: 'US' },
    };
  2. Serve the directory with any static file server. Because the page uses <script type="module">, you cannot open index.html directly via file:// — a real HTTP server is required.

    # Pick any of these:
    npx serve .
    # or
    npx http-server -p 8080 .
    # or
    python3 -m http.server 8080
  3. Open http://localhost:8080 in your browser.

How it works

  • The SDK is imported from https://esm.sh/@rolloutctrl/js-sdk@1.0.0 as an ES module.
  • A RolloutCtrlClient is created with the values from config.js (overridden by config.local.js if present).
  • On the ready event the page calls client.evaluate(flagKey, context) and client.getVariant(flagKey, context) and renders the matching button.
  • On the updated event (fired when the polled configuration changes) the page re-renders, so dashboard changes appear without a reload.
  • The Re-evaluate button re-runs the local evaluation immediately.
  • The Flush metrics button pushes aggregated exposure metrics to the server.
  • On beforeunload the client is closed (metrics flushed, polling stopped).

Using a local build instead of the CDN

If you want to use a locally built SDK instead of the CDN (e.g. for offline testing or against unreleased changes), build the package and point the import at the resulting ESM file:

# From the repo root:
yarn workspace @rolloutctrl/js-sdk build
# Copy the bundle next to index.html:
cp packages/sdk/js-sdk/dist/index.mjs ./vendor/rolloutctrl-js-sdk.mjs

Then replace the import in index.html:

<script type="module">
  import { RolloutCtrlClient } from './vendor/rolloutctrl-js-sdk.mjs';
  // ...
</script>

Notes

  • The SDK uses fetch and localStorage, so a real HTTP origin is required — file:// will not work.
  • Pin the CDN URL to a specific version (e.g. @1.0.0) to avoid surprise upgrades.
  • Keep your sdkKey in config.local.js (gitignored) rather than config.js when committing changes.