{"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/01ffac6f245854c1b8adc6a69857c76a15f90022/internal/js/modules/k6/browser/common/browser_context.go#L136-L172","documentation":"After registering the web-vitals IIFE, each new BrowserContext also registers the web-vitals init script (the polling/reporting half) through the same AddInitScript path, injecting it into every already-open page. This error wraps that second registration failing. Like [617], the script body is a k6 constant, so failures come from the CDP calls against existing pages, not from user code.","triggerScenarios":"browser.newContext() with existing pages whose sessions are closed or targets crashed; context creation racing browser/connection shutdown so Page.addScriptToEvaluateOnNewDocument fails.","commonSituations":"Creating contexts late in a test while other pages are crashing or closing; browser under memory pressure dropping page sessions; teardown races.","solutions":["Create contexts up front, before navigating/crashing workloads","Close unusable pages before creating a new context","Retry the context creation once","Enable K6_BROWSER_LOG=debug and update k6 if it persists"],"exampleFix":null,"handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  ctx = browser.newContext();\n} catch (e) {\n  if (String(e).includes('adding web vital init script')) {\n    await sleep(1000); // let dying page sessions finish detaching\n    ctx = browser.newContext(); // retry once\n  } else throw e;\n}","preventionTips":["Keep context creation away from page-crash windows and shutdown","Stabilize or close failing pages before new contexts","Stay current on k6 releases for web-vital injection fixes"],"tags":["cdp","browser-context","init-script","web-vitals","injection"],"backgroundTag":"init-script-injection-failed","analyzedSha":"01ffac6f245854c1b8adc6a69857c76a15f90022","analyzedAt":"2026-08-18T03:05:52.393Z","contentChangedAt":"2026-08-18T03:05:52.393Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}