Skip to content

@lynx-js/web-core@0.28.0

Choose a tag to compare

@github-actions github-actions released this 05 Dec 13:39
e40bcf9

Minor Changes

  • Add lynx-group-scope to <lynx-view>. With lynx-group-scope="page", views with the same lynx-group-id also 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.requestAnimationFrame then 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 first renderPage. 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-id Each card creates its own dedicated worker Not shared
    2. Document group lynx-group-id is set, and the scope is "document" (default) or falls back to it The 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-id is 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 not credentialless. This includes cards directly in the top-level page The first card creates a dedicated worker with the Worker constructor of the top-level window Cards in the same top-level page with the same group id and worker script URL
    4. Page group, SharedWorker lynx-group-id is 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 is credentialless. The card's frame can be found through window.top.frames, and new SharedWorker() does not throw The first card creates a SharedWorker with a random name. The other cards look up the name and connect to it The 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 no lynx-group-id Mode 1
    The scope is unknown Mode 2, or mode 1 without lynx-group-id
    Mode 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 SharedWorker is missing or its constructor throws Mode 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 its url changes All 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 closed 3 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, SharedWorker The 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-core does not cancel them when it destroys a card
    Data from lynx.setSharedData() Later cards can still read it, which is the purpose of shared data
    Cached code of non-entry chunks (_$factoryCache in @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
    SystemInfo Only 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 and browserConfig, even while the first card is still running

Patch Changes

  • Skip posting timing flags in __FlushElementTree before the page element is mounted so empty startup flushes (e.g. with async external bundles) do not prematurely consume first-paint onSetup, and merge pipeline timings into setupTiming when the first mount carries a pipelineId. (#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-plugin calls, now replaces the style sheets of the card or of a lazy bundle with the CSS of the update. Without enableCSSSelector, it also recomputes the styles of the entry's elements. Before, the main-thread lynx had no getDevtool, 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