Skip to content

Commit

Permalink
[web] dispatch browser event on flutter/service_worker channel (flutt…
Browse files Browse the repository at this point in the history
…er#21284)

Registering the service worker immediately after the documented has loaded may cause SW initialization to compete with framework initialization. It was recommended to us that we defer the service worker setup until after the framework is done with setup, which should be sometime after the first frame.

To implement this, the binding will dispatch a platform message on startup. This can be listened for in the html document
flutter#66066
  • Loading branch information
jonahwilliams committed Sep 22, 2020
1 parent 98cdb7d commit 83c9d10
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/web_ui/lib/src/engine/window.dart
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,11 @@ class EngineWindow extends ui.Window {
}
break;

// Dispatched by the bindings to delay service worker initialization.
case 'flutter/service_worker':
html.window.dispatchEvent(html.Event('flutter-first-frame'));
return;

case 'flutter/textinput':
textEditing.channel.handleTextInput(data, callback);
return;
Expand Down
16 changes: 16 additions & 0 deletions lib/web_ui/test/engine/window_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -255,4 +255,20 @@ void testMain() {
expect(window.locales, isNotEmpty);
expect(localeChangedCount, 1);
});

test('dispatches browser event on flutter/service_worker channel', () async {
final Completer<void> completer = Completer<void>();
html.window.addEventListener('flutter-first-frame', completer.complete);
final Zone innerZone = Zone.current.fork();

innerZone.runGuarded(() {
window.sendPlatformMessage(
'flutter/service_worker',
ByteData(0),
(outputData) { },
);
});

await expectLater(completer.future, completes);
});
}

0 comments on commit 83c9d10

Please sign in to comment.