feat(ui): record the SDK load outcome in the launch diagnostic
CI / changes (pull_request) Successful in 2s
CI / unit (pull_request) Has been skipped
CI / integration (pull_request) Has been skipped
CI / ui (pull_request) Successful in 55s
CI / gate (pull_request) Successful in 0s
CI / deploy (pull_request) Successful in 1m16s
CI / changes (pull_request) Successful in 2s
CI / unit (pull_request) Has been skipped
CI / integration (pull_request) Has been skipped
CI / ui (pull_request) Successful in 55s
CI / gate (pull_request) Successful in 0s
CI / deploy (pull_request) Successful in 1m16s
The diagnostic showed only sdk: yes/no (window.Telegram presence), not why the SDK was absent. Capture how the dynamic telegram-web-app.js load resolved — present / loaded / no-webapp / error / timeout — and surface it as "sdk-load: <outcome>". error/timeout pinpoint a blocked or hanging telegram.org (the prime suspect for an empty launch); no-webapp a loaded-but-broken script. loadTelegramSDK records the outcome (telegramSdkOutcome); collectTelegramDiag carries it into the screen. Unit tests cover each outcome; the blocked-script e2e now asserts sdk-load: error.
This commit is contained in:
+35
-6
@@ -60,6 +60,27 @@ export function insideTelegram(): boolean {
|
||||
// safe-area insets, vertical-swipe guard, …) are available. Bump it when Telegram bumps theirs.
|
||||
const sdkScriptSrc = 'https://telegram.org/js/telegram-web-app.js?62';
|
||||
|
||||
/**
|
||||
* TelegramSdkOutcome records how the dynamic telegram-web-app.js load resolved, surfaced on the
|
||||
* launch-error screen to tell the failure modes apart — notably 'error' / 'timeout', which mean the
|
||||
* network could not reach telegram.org (the script blocked or hung):
|
||||
*
|
||||
* not-attempted — loadTelegramSDK was never called (a plain web / native entry)
|
||||
* present — window.Telegram.WebApp was already there (a cached load or native injection)
|
||||
* loaded — the script loaded and defined window.Telegram.WebApp
|
||||
* no-webapp — the script loaded (HTTP 200) but did not define window.Telegram.WebApp
|
||||
* error — the script failed to load (telegram.org unreachable / blocked, a fast failure)
|
||||
* timeout — the script neither loaded nor failed within the timeout (a blocked, hanging fetch)
|
||||
*/
|
||||
export type TelegramSdkOutcome = 'not-attempted' | 'present' | 'loaded' | 'no-webapp' | 'error' | 'timeout';
|
||||
|
||||
let sdkLoadOutcome: TelegramSdkOutcome = 'not-attempted';
|
||||
|
||||
/** telegramSdkOutcome returns how the last loadTelegramSDK attempt resolved (see TelegramSdkOutcome). */
|
||||
export function telegramSdkOutcome(): TelegramSdkOutcome {
|
||||
return sdkLoadOutcome;
|
||||
}
|
||||
|
||||
/**
|
||||
* loadTelegramSDK injects the official telegram-web-app.js and resolves true once
|
||||
* window.Telegram.WebApp is available, or false if the script errors or does not load within
|
||||
@@ -72,25 +93,29 @@ const sdkScriptSrc = 'https://telegram.org/js/telegram-web-app.js?62';
|
||||
*/
|
||||
export function loadTelegramSDK(timeoutMs: number): Promise<boolean> {
|
||||
if (typeof document === 'undefined') return Promise.resolve(false);
|
||||
if (webApp()) return Promise.resolve(true);
|
||||
if (webApp()) {
|
||||
sdkLoadOutcome = 'present';
|
||||
return Promise.resolve(true);
|
||||
}
|
||||
return new Promise<boolean>((resolve) => {
|
||||
let done = false;
|
||||
const finish = (ok: boolean): void => {
|
||||
const finish = (outcome: TelegramSdkOutcome): void => {
|
||||
if (done) return;
|
||||
done = true;
|
||||
resolve(ok);
|
||||
sdkLoadOutcome = outcome;
|
||||
resolve(outcome === 'loaded');
|
||||
};
|
||||
const timer = setTimeout(() => finish(!!webApp()), timeoutMs);
|
||||
const timer = setTimeout(() => finish(webApp() ? 'loaded' : 'timeout'), timeoutMs);
|
||||
const s = document.createElement('script');
|
||||
s.src = sdkScriptSrc;
|
||||
s.async = true;
|
||||
s.onload = () => {
|
||||
clearTimeout(timer);
|
||||
finish(!!webApp());
|
||||
finish(webApp() ? 'loaded' : 'no-webapp');
|
||||
};
|
||||
s.onerror = () => {
|
||||
clearTimeout(timer);
|
||||
finish(false);
|
||||
finish('error');
|
||||
};
|
||||
document.head.appendChild(s);
|
||||
});
|
||||
@@ -413,6 +438,9 @@ export interface TelegramDiag {
|
||||
hasSDK: boolean;
|
||||
/** Whether window.Telegram.WebApp is present. */
|
||||
hasWebApp: boolean;
|
||||
/** How the dynamic telegram-web-app.js load resolved (see TelegramSdkOutcome) — 'error' /
|
||||
* 'timeout' mean telegram.org was unreachable, the prime suspect for an empty launch. */
|
||||
sdkLoad: TelegramSdkOutcome;
|
||||
/** Telegram's own platform string (ios | android | android_x | tdesktop | web | …), or ''. */
|
||||
platform: string;
|
||||
/** The Bot API version the client reports, or ''. */
|
||||
@@ -454,6 +482,7 @@ export function collectTelegramDiag(): TelegramDiag {
|
||||
return {
|
||||
hasSDK: sdk,
|
||||
hasWebApp: !!w,
|
||||
sdkLoad: sdkLoadOutcome,
|
||||
platform: w?.platform ?? '',
|
||||
version: w?.version ?? '',
|
||||
initDataLen: initData.length,
|
||||
|
||||
Reference in New Issue
Block a user