Skip to content

[Proposal] Provide a first-frame hook for the web entrypoint initializer #133454

Closed

Description

@navaronbracke

Is there an existing issue for this?

Use case

Flutter developers that target the web have the ability to customise the setup of their Flutter entrypoint.

The documentation even provides an excellent example here: https://docs.flutter.dev/platform-integration/web/initialization#example-display-a-progress-indicator

However, the initialization process has one problem: It does not provide a hook for the moment when Flutter draws its first frame.

This leads to a gap between appRunner.runApp(); in the index.html, and when runApp() in the Dart entrypoint actually requests the first frame to be drawn.

An example would be an application where the Dart entrypoint looks like this (using the example from the documentation above)

void main() async {
  // Imagine some expensive work, like fetching a user token for a logged-in user
  await Future.delayed(const Duration(seconds: 2));

  runApp(const MyApp());
}

However, there is an existing mechanism for detecting the first frame, the flutter-first-frame event. This event is not documented on the aforementioned documentation, though. As of Flutter 3.13.1 this event is still fired by the engine so the workaround still works. (I have been using the workaround for over two years, iirc)

Proposal

Add a new hook in _flutter.loader.loadEntrypoint for the first frame of the Flutter app. This can replace the undocumented behavior of the flutter-first-frame event.

        _flutter.loader.loadEntrypoint({
          serviceWorker: {
            serviceWorkerVersion: serviceWorkerVersion,
          },
          onEntrypointLoaded: async function(engineInitializer) {
            loading.textContent = "Initializing engine...";
            let appRunner = await engineInitializer.initializeEngine();

            loading.textContent = "Running app...";
            await appRunner.runApp();
          },
          onFirstFrame: function() {}, // New
        });

Activity

  1. changed the title [-][Proposal] Provide a first-frame hook for the web entrypoint[/-] [+][Proposal] Provide a first-frame hook for the web entrypoint initializer[/+] on Aug 28, 2023
  2. added
    in triagePresently being triaged by the triage team
    c: new featureNothing broken; request for a new capability
    platform-webWeb applications specifically
    c: proposalA detailed proposal for a change to Flutter
    team-webOwned by Web platform team
    and removed
    in triagePresently being triaged by the triage team
    on Aug 28, 2023
  3. ditman commented on Aug 29, 2023

    @ditman
    Member

    flutter-first-frame predates the flutter JS loading API by a fair amount of time, and originally was meant for benchmarking. This proposal sounds good to me (although I'm not sure that we'd want to add more parameters to loadEntrypoint; this might be better suited as something that's passed to the runApp method, for example.)

  4. yjbanov commented on Aug 31, 2023

    @yjbanov
    Contributor

    @ditman Makes sense. The framework is aware of when it's done rendering the first frame, so a framework-side Dart API makes sense here, and it can be available for all platforms. Reassigning to the framework team for evaluation.

  5. added and removed
    platform-webWeb applications specifically
    team-webOwned by Web platform team
    on Aug 31, 2023
  6. goderbauer commented on Sep 12, 2023

    @goderbauer
    Member

    We already have addPostFrameCallback, which fires at the end of a frame. If registered before the first frame, it will inform you when the first frame is done. Can you just use that?

    https://master-api.flutter.dev/flutter/scheduler/SchedulerBinding/addPostFrameCallback.html

  7. added
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    P3Issues that are less important to the Flutter project
    on Sep 12, 2023
  8. navaronbracke commented on Sep 13, 2023

    @navaronbracke
    ContributorAuthor

    That would work.

  9. removed
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Sep 13, 2023
  10. github-actions commented on Sep 27, 2023

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  11. locked as resolved and limited conversation to collaborators on Sep 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3Issues that are less important to the Flutter projectc: new featureNothing broken; request for a new capabilityc: proposalA detailed proposal for a change to Flutterteam-frameworkOwned by Framework teamtriaged-frameworkTriaged by Framework team

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions