Minor Changes
-
Add
lynx-group-scopeto<lynx-view>. Withlynx-group-scope="page", views with the samelynx-group-idalso share a background worker between the same-origin frames of a top-level page, but never with other tabs. The default,"document", still only shares it within one document. (#4469)<lynx-view lynx-group-id="1" lynx-group-scope="page" url="..."></lynx-view>
If all frames up to the page are same-origin, the page owns a dedicated worker. Otherwise, such as when another site embeds several frames of your site, the frames use a
SharedWorker.lynx.requestAnimationFramethen uses the frames of the card's page, and the worker's console is not part of the page's console. If the frame cannot share a worker, the view logs a warning and uses"document".The following tables describe how background workers are created and stopped. Modes 1 and 2 already existed, and modes 3 and 4 are new.
Creation
A card, which is the content of one
<lynx-view>, creates or joins its worker after its main-thread script has run and before its firstrenderPage. The "first card" is the one that starts while its group has no worker. Cards in different modes never share a worker, even with the same group id.Mode Conditions How the worker is created Shared between 1. Exclusive No lynx-group-idEach card creates its own dedicated worker Not shared 2. Document group lynx-group-idis set, and the scope is"document"(default) or falls back to itThe first card creates a dedicated worker Cards in the same document with the same group id 3. Page group, owned by the page lynx-group-idis set and the scope is"page". Every frame from the card's frame up to the top-level page is same-origin, and the card's frame is notcredentialless. This includes cards directly in the top-level pageThe first card creates a dedicated worker with the Workerconstructor of the top-level windowCards in the same top-level page with the same group id and worker script URL 4. Page group, SharedWorkerlynx-group-idis set and the scope is"page", but mode 3 is not possible, because the page or a frame in between is cross-origin, or the card's frame iscredentialless. The card's frame can be found throughwindow.top.frames, andnew SharedWorker()does not throwThe first card creates a SharedWorkerwith a random name. The other cards look up the name and connect to itThe same cards as in mode 3, if their frames are also same-origin with each other, can access each other synchronously, and are in the same storage partition Fallback
Every fallback logs a warning. Errors that happen after the worker is created, such as a script that fails to load, do not cause a fallback.
Condition Falls back to The scope is "page", but there is nolynx-group-idMode 1 The scope is unknown Mode 2, or mode 1 without lynx-group-idMode 3 is not possible, and the card's frame or a frame above it is inside a shadow root Mode 2 Mode 3 is not possible, and SharedWorkeris missing or its constructor throwsMode 2 Destruction
These events make a card leave its worker or stop the worker directly:
Trigger Modes What happens The card is destroyed, because its view is removed, reload()is called or itsurlchangesAll If its background script was started, the card waits until the script is ready and the worker has destroyed the card, and then leaves. Otherwise, it leaves immediately. A card that has not joined a worker does not change the number of cards in the group pagehide, because the frame is removed or navigates, or the page is closed3 and 4 If its background script was started, the card asks the worker to destroy it but does not wait. It then leaves synchronously. Nothing happens if the page enters the back/forward cache The browser stops the worker All Modes 1 and 2: when the document of the cards is gone. Mode 3: when the top-level page is gone. Mode 4: when all documents connected to the worker are gone After a card leaves, the worker stops under these conditions:
Mode The worker stops when How it stops 1 Its card leaves terminate()2 The last card of the group in the document leaves terminate()3 The last card of the group in all frames leaves terminate()4 A card leaves, and no frame of the page stores the name of the group anymore That card posts a close message, and the worker calls close()Reloading the only card of a group creates a new worker. If the group has other cards, the reloaded card joins their worker again. When the last card leaves on
pagehide, the worker stops immediately, so the card's destroy callbacks may not finish.When the first card is destroyed before the others
The first card creates the worker but does not own it. While the worker keeps running, the other cards are not affected, and new cards join the same worker.
Mode Only the view of the first card is removed Its frame is also removed Reason 2. Document group The worker keeps running All cards of the group and the worker stop The cards and the worker belong to the same document 3. Page group, owned by the page The worker keeps running The worker keeps running The top-level page owns the worker and the number of cards 4. Page group, SharedWorkerThe worker keeps running The worker keeps running A shared worker runs while a document is connected to it. Every frame with a card of the group stores the name, so new cards can still find it What stays in the worker
The following stays until the worker stops. The first three rows apply to every destroyed card, and the last row only to the first card. Document groups already worked this way, but
"page"can share a worker between more cards, so this affects more of them.What stays Effect Timers and shared data observers that the card did not remove Timers keep running and observers are still called, which uses CPU and memory. @lynx-js/lynx-coredoes not cancel them when it destroys a cardData from lynx.setSharedData()Later cards can still read it, which is the purpose of shared data Cached code of non-entry chunks ( _$factoryCachein@lynx-js/lynx-core)The cache only uses the path as its key, so another card that loads a chunk with the same path may run the code of the destroyed card SystemInfoOnly the first card to start in the worker sets it. The background scripts of all later cards read its values, such as pixelRatio, the screen size andbrowserConfig, even while the first card is still running
Patch Changes
-
Skip posting timing flags in
__FlushElementTreebefore the page element is mounted so empty startup flushes (e.g. with async external bundles) do not prematurely consume first-paintonSetup, and merge pipeline timings intosetupTimingwhen the first mount carries apipelineId. (#4451) -
Apply CSS hot updates on web. The main-thread
lynx.getDevtool().replaceStyleSheetByIdWithBase64, which the CSS hot update runtime of@lynx-js/css-extract-webpack-plugincalls, now replaces the style sheets of the card or of a lazy bundle with the CSS of the update. WithoutenableCSSSelector, it also recomputes the styles of the entry's elements. Before, the main-threadlynxhad nogetDevtool, so a CSS change on web needed a reload. (#4433) -
Fix two problems with background workers shared through
lynx-group-id. Removing a card no longer leaves its NAPI loader, and through it the card's state, in memory until the worker stops. Removing a card before it has started no longer stops the worker that other cards in the group still use. (#4469) -
Fix a memory leak in asynchronous calls between threads. Every call with a return value, such as a native module call, kept its return handler in memory after the reply arrived. (#4469)
-
Updated dependencies [
681e6b7,36ffc0a]:- @lynx-js/web-worker-rpc@0.28.0
- @lynx-js/web-elements@0.12.21