Skip to content

experimental: embed bootstrap data #145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 13 commits into
base: main
Choose a base branch
from
Draft

Conversation

dferber90
Copy link
Collaborator

@dferber90 dferber90 commented Jun 12, 2025

Goal

The goal is to make it possible to experiment on marketing pages without causing layout shift, while still being able to serve the different variants from a CDN.

Constraints

We do not want to use client-side experimentation as it leads to layout shift. We can not have any information about the user on the cached page itself as it would lead to cache poisoning or very bad cache hit rates.

However, the flags client (eg Statsig) must still be aware of the experiment running on the server and must be able to bootstrap itself so it can track events.

Approach

image
  1. Browser requests page
  2. Edge Middleware bootstraps Statsig from Edge Config and determines the Evaluation Context (eg user)
  3. Using the precompute pattern, Edge Middleware rewrites the request to serve the correct variant
  4. Edge Middleware embeds the Statsig bootstrap file and the Evaluation Context in the response by rewriting the HTML
  5. Browser sees the correct variant of the page immediately, and can bootstrap itself without any further network requests through the data embedded in the HTML

This solves multiple problems

  • the exact evaluation context used by Edge Middleware when determining the state of feature flags is available to the client
  • high cache hit ratios as the variants of the page are now cache-able and can be served from the CDN, as they do not contain any user-specific information
  • browser can bootstrap the Statsig SDK without a network request from the embedded datafile and evaluation context

Added methods

We're adding several methods to the Flags SDK to make this nicer to work with.

  • embedBootstrapData(response, data): Response call this from Edge Middleware to embed data into the response
  • <FlagBootstrapData /> render this in a top-level layout, its contents will be replaced by embedBootstrapData
  • useBootstrapData() a hook which returns the data embedded by embedBootstrapData

Snapshot release

You can try out this logic in the flags@4.1.0-4ebe698e-20250613112000 snapshot release.

@dferber90 dferber90 requested a review from a team June 12, 2025 12:45
Copy link

vercel bot commented Jun 12, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
flags-sdk-snippets ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 22, 2025 0:56am
flags-sdk-sveltekit-snippets ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 22, 2025 0:56am
shirt-shop ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 22, 2025 0:56am

This is a fork of htmlrewriter (no dash) which fixes the node compatibility issues while we wait for remorses/htmlrewriter#6 to land and get fixed upstream
Copy link

socket-security bot commented Jun 13, 2025

All alerts resolved. Learn more about Socket for GitHub.

This PR previously contained dependency changes with security issues that have been resolved, removed, or ignored.

View full report

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant