grafana/k6 · error
adding web vital init script to new browser context: %w
Error message
adding web vital init script to new browser context: %w
What it means
Identical mechanism to the previous error but for the second injection: NewBrowserContext adds js.WebVitalInitScript (browser_context.go:154), the script that actually wires up web-vitals metric collection, on every existing page. Seeing this (rather than the IIFE one) means the first CDP injection succeeded and the sequence failed mid-way — most likely the connection or a page session died between the two calls.
Source
Thrown at internal/js/modules/k6/browser/common/browser_context.go:154
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() {
if err := p.evaluateOnNewDocument(script); err != nil {
return fmt.Errorf("adding init script to browser context: %w", err)
}View on GitHub (pinned to 93accf6570)
Solutions
- Retry browser.newContext() after confirming the browser is still connected
- Create contexts before any pages/popups exist so the per-page loop is trivial
- Investigate chromium stability (OOM/shm) with K6_BROWSER_LOG=debug
- Upgrade k6 for current web-vitals injection behavior
Defensive patterns
Strategy: retry
Validate before calling
// same guard as the IIFE variant: create contexts early with zero open pages
Try / catch
try {
ctx = browser.newContext()
} catch (e) {
if (/adding web vital init script/.test(String(e))) {
// mid-sequence connection loss; a fresh browser launch is needed
throw e
}
throw e
} Prevention
- Create contexts before opening pages
- Treat any web-vital injection failure as a symptom of a dead/dying chromium
- Upgrade k6 to stay current with web-vitals injection code
When it happens
Trigger: The connection drops or a page session detaches exactly between the two addScriptToEvaluateOnNewDocument calls; chromium crashes during context creation while pages are open.
Common situations: Flaky chromium under memory pressure; creating contexts concurrently with page teardown; race with browser.close().
Related errors
- adding web vital script to new browser context: %w
- adding init script to browser context: %w
- adding k6 object to new browser context: %w
- connecting to Chromium over CDP: %w
- browser connect: %w
AI-assisted analysis of grafana/k6@93accf6570 (2026-08-15).
Data as JSON: /api/errors/4acf8b8e15df94b7.
Report an issue: GitHub.