grafana/k6 · error
adding web vital script to new browser context
Error message
adding web vital script to new browser context: %w
What it means
Every new BrowserContext registers a web-vitals IIFE script via AddInitScript, which also injects it into every already-open page of the browser with Page.addScriptToEvaluateOnNewDocument. This error wraps a failure of that registration when creating the context. It cannot come from the script content (which is a constant) — it comes from the CDP calls against existing pages failing.
Solutions
- Create all browser contexts before heavy page churn, and avoid newContext() during teardown
- Retry creating the context once after closing or stabilizing broken pages
- Use K6_BROWSER_LOG=debug to see which existing page rejected the injection
- Upgrade k6 — web-vital script registration has seen fixes across releases
Example fix
// before const ctx2 = browser.newContext(); // while a page in ctx1 is crashing/closing // after await brokenPage.close(); // stabilize first const ctx2 = browser.newContext();
Defensive patterns
Strategy: retry
Validate before calling
// Create contexts before page churn begins
export function setup() {
// contexts created once, up front — not mid-run while pages crash
} Try / catch
try {
ctx = browser.newContext();
} catch (e) {
if (String(e).includes('adding web vital script')) {
// an existing page's session rejected injection; close broken pages and retry
for (const p of browser.contexts().pages()) { /* close unusable pages */ }
ctx = browser.newContext();
} else throw e;
} Prevention
- Create all browser contexts early, before heavy navigation workloads
- Avoid newContext() during teardown
- Close crashed pages instead of leaving them attached
When it happens
Trigger: Calling browser.newContext() while existing pages are in a bad state (crashed target, closed session) so evaluateOnNewDocument fails; creating a context as the browser/connection is shutting down.
Common situations: Opening new contexts after some pages crashed mid-run; creating contexts during teardown while k6 closes the browser; heavily loaded browsers whose page targets die concurrently.
Related errors
- adding web vital init script to new browser context
- adding init script to browser context
- clearing permissions
- disposing browser context ID
- disposing browser context
AI-assisted analysis of grafana/k6@01ffac6f24 (2026-08-18).
Data as JSON: /api/errors/239291e39047a66b.
Report an issue: GitHub.
Appendix: source
Thrown at internal/js/modules/k6/browser/common/browser_context.go:151
b := BrowserContext{
BaseEventEmitter: NewBaseEventEmitter(ctx),
ctx: ctx,
browser: browser,
id: id,
opts: opts,
logger: logger,
timeoutSettings: NewTimeoutSettings(nil),
}
if len(opts.Permissions) > 0 {
err := b.GrantPermissions(opts.Permissions, GrantPermissionsOptions{})
if err != nil {
return nil, err
}
}
if err := b.AddInitScript(js.WebVitalIIFEScript); err != nil {
return nil, fmt.Errorf("adding web vital script to new browser context: %w", err)
}
if err := b.AddInitScript(js.WebVitalInitScript); err != nil {
return nil, fmt.Errorf("adding web vital init script to new browser context: %w", err)
}
if err := b.setDownloadsPath(opts.DownloadsPath); err != nil {
return nil, fmt.Errorf("setting downloads path: %w", err)
}
return &b, nil
}
// AddInitScript adds a script that will be initialized on all new pages.
func (b *BrowserContext) AddInitScript(script string) error {
b.logger.Debugf("BrowserContext:AddInitScript", "bctxid:%v", b.id)
b.evaluateOnNewDocumentSources = append(b.evaluateOnNewDocumentSources, script)
for _, p := range b.browser.getPages() {View on GitHub (pinned to 01ffac6f24)