Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -197,18 +197,29 @@ class FirebaseCoreWeb extends FirebasePlatform {
web.document.head!.appendChild(script);
}

Completer completer = Completer();

globalContext.callMethod(
// `ff_trigger_*` is an async function, so it returns a promise that
// rejects when `import()` fails (network error, blocked request, CDN
// outage). Awaiting that promise, instead of only waiting for the
// callback, turns a failed load into an error rather than a future that
// never completes, which left `initializeApp` hanging forever.
final JSPromise<JSAny?> trigger = globalContext.callMethod(
'ff_trigger_$windowVar'.toJS,
(JSAny module) {
globalContext[windowVar] = module;
globalContext.delete('ff_trigger_$windowVar'.toJS);
completer.complete();
}.toJS,
);

await completer.future;
try {
await trigger.toDart;
} catch (e) {
throw FirebaseException(
plugin: 'core',
code: 'script-load-failed',
message: 'Failed to load the Firebase JS SDK script $src: $e',
);
} finally {
globalContext.delete('ff_trigger_$windowVar'.toJS);
}
}

/// Initializes the Firebase JS SDKs by injecting them into the `head` of the
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2026 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

@TestOn('browser')
library;

import 'dart:js_interop';
import 'dart:js_interop_unsafe';

import 'package:firebase_core_platform_interface/firebase_core_platform_interface.dart';
import 'package:firebase_core_web/firebase_core_web.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:web/web.dart' as web;

void main() {
group('injectSrcScript', () {
test('fails instead of hanging when the script cannot be loaded', () async {
final coreWeb = FirebaseCoreWeb();
final src = '${web.window.location.origin}/does-not-exist.js';

await expectLater(
coreWeb
.injectSrcScript(src, 'firebase_missing')
.timeout(const Duration(seconds: 10)),
throwsA(
isA<FirebaseException>()
.having((e) => e.plugin, 'plugin', 'core')
.having((e) => e.code, 'code', 'script-load-failed'),
),
);

expect(globalContext.has('firebase_missing'), isFalse);
expect(globalContext.has('ff_trigger_firebase_missing'), isFalse);
Comment thread
LasseLegarth marked this conversation as resolved.
Resolved
});
});
}
Loading