{"record":{"id":"4acf8b8e15df94b7","repo":"grafana/k6","slug":"adding-web-vital-init-script-to-new-browser-contex","errorCode":null,"errorMessage":"adding web vital init script to new browser context: %w","messagePattern":"adding web vital init script to new browser context: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/browser_context.go","lineNumber":154,"sourceCode":"\t\tbrowser:          browser,\n\t\tid:               id,\n\t\topts:             opts,\n\t\tlogger:           logger,\n\t\ttimeoutSettings:  NewTimeoutSettings(nil),\n\t}\n\n\tif len(opts.Permissions) > 0 {\n\t\terr := b.GrantPermissions(opts.Permissions, GrantPermissionsOptions{})\n\t\tif err != nil {\n\t\t\treturn nil, err\n\t\t}\n\t}\n\n\tif err := b.AddInitScript(js.WebVitalIIFEScript); err != nil {\n\t\treturn nil, fmt.Errorf(\"adding web vital script to new browser context: %w\", err)\n\t}\n\tif err := b.AddInitScript(js.WebVitalInitScript); err != nil {\n\t\treturn nil, fmt.Errorf(\"adding web vital init script to new browser context: %w\", err)\n\t}\n\tif err := b.setDownloadsPath(opts.DownloadsPath); err != nil {\n\t\treturn nil, fmt.Errorf(\"setting downloads path: %w\", err)\n\t}\n\n\treturn &b, nil\n}\n\n// AddInitScript adds a script that will be initialized on all new pages.\nfunc (b *BrowserContext) AddInitScript(script string) error {\n\tb.logger.Debugf(\"BrowserContext:AddInitScript\", \"bctxid:%v\", b.id)\n\n\tb.evaluateOnNewDocumentSources = append(b.evaluateOnNewDocumentSources, script)\n\n\tfor _, p := range b.browser.getPages() {\n\t\tif err := p.evaluateOnNewDocument(script); err != nil {\n\t\t\treturn fmt.Errorf(\"adding init script to browser context: %w\", err)\n\t\t}","sourceCodeStart":136,"sourceCodeEnd":172,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/browser/common/browser_context.go#L136-L172","documentation":"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.","triggerScenarios":"The connection drops or a page session detaches exactly between the two addScriptToEvaluateOnNewDocument calls; chromium crashes during context creation while pages are open.","commonSituations":"Flaky chromium under memory pressure; creating contexts concurrently with page teardown; race with browser.close().","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"],"exampleFix":null,"handlingStrategy":"retry","validationCode":"// same guard as the IIFE variant: create contexts early with zero open pages","typeGuard":null,"tryCatchPattern":"try {\n  ctx = browser.newContext()\n} catch (e) {\n  if (/adding web vital init script/.test(String(e))) {\n    // mid-sequence connection loss; a fresh browser launch is needed\n    throw e\n  }\n  throw e\n}","preventionTips":["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"],"tags":["cdp","init-script","web-vitals","browser-context","connection"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}