MicrosoftEdge / MicrosoftEdge/WebView2Feedback
AddScriptToExecuteOnDocumentCreatedAsync takes unusually long to complete on some machines
Nobody has claimed this yet.
- Dominant language
- PowerShell
- Stars
- 526
- Forks
- 67
- PR merge metrics
- No merged PRs in 30d
Description
Description
On some Windows machines, CoreWebView2.AddScriptToExecuteOnDocumentCreatedAsync can take an unexpectedly long time to complete after the WebView2 controller has been created. In our observation, the registration callback returned S_OK approximately 1.5 seconds after the API was called.
During this interval, the first navigation may already have started and the first document may begin executing page code. As a result, a script that is expected to run for the document can be missing from that first document, even though the registration API eventually completes successfully.
Observed timing
T+0 ms CoreWebView2 controller creation completes
T+1 ms AddScriptToExecuteOnDocumentCreatedAsync is called
T+210 ms first navigation starts
T+1,498 ms the target document starts / source changes
T+1,514 ms AddScriptToExecuteOnDocumentCreatedAsync callback completes with S_OK
T+1,? ms page code checks for the injected value and sees it as undefined
The delay is not consistently reproducible on every machine. On affected machines, the registration callback takes about 1.5 seconds; on other machines it completes quickly enough that the problem is not observed.
Reproduction outline
- Create a WebView2 controller asynchronously.
- When the controller is created, call
AddScriptToExecuteOnDocumentCreatedAsyncwith a small script that sets a unique global value. - Start the first navigation immediately after issuing the registration call.
- In the first page, record the time at which the document starts, the value of the injected global, and the registration callback completion time.
- Repeat on different machines or under different system load conditions.
Expected behavior
The document-created script registration should complete with low and predictable latency after the controller is ready, or WebView2 should provide a way to determine that the registration has become effective before the first document begins executing.
Questions
- Are there known conditions that can cause
AddScriptToExecuteOnDocumentCreatedAsyncto take around 1.5 seconds or longer? - Does the registration operation involve browser-process initialization or other work that can be delayed by machine-specific conditions?
- Is there a recommended diagnostic or mitigation for this registration latency?
- Does WebView2 guarantee that a document-created script is applied to a navigation started while the registration operation is still pending?
Environment
- Windows
- WebView2
- The issue is observed when the controller is created asynchronously and the first navigation is to a remote HTTPS page.
- The registration API eventually completes successfully with
S_OK; the concern is the unusually long completion latency.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the asynchronous controller-creation flow and the AddScriptToExecuteOnDocumentCreatedAsync call described in the reproduction; capture callback, first-navigation, document-start, and script-observation timestamps on affected and unaffected Windows machines. Done means identifying a reproducible cause or documenting the WebView2 guarantee or mitigation when registration remains pending during the first navigation.
Written by the indexing model from the issue text.
Assessment
- Domain
- desktop
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Active
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100