From ae5f82336b2e800d8b7bb1428156b845e44816eb Mon Sep 17 00:00:00 2001 From: Razzwan Date: Mon, 13 Nov 2023 16:03:15 +0300 Subject: [PATCH] fix(WebAppProvider): loading script --- package-lock.json | 4 ++-- package.json | 2 +- src/WebAppProvider.tsx | 14 +++++++++++--- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/package-lock.json b/package-lock.json index 667fe03..28edd3b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@altiore/twa", - "version": "0.0.1", + "version": "1.2.9", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@altiore/twa", - "version": "0.0.1", + "version": "1.2.9", "license": "MIT", "devDependencies": { "@changesets/cli": "2.25.2", diff --git a/package.json b/package.json index f3de0ee..acb9bb1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@altiore/twa", - "version": "1.2.8", + "version": "1.2.9", "description": "React components for Telegram WebApp", "source": "./src/index.ts", "type": "module", diff --git a/src/WebAppProvider.tsx b/src/WebAppProvider.tsx index 4cecc6a..88f1481 100644 --- a/src/WebAppProvider.tsx +++ b/src/WebAppProvider.tsx @@ -71,7 +71,7 @@ const WebAppProvider = ({ return () => window.removeEventListener('beforeunload', forceHideButtons); }, [options?.smoothButtonsTransition, webApp]); - const [isLoading, setIsLoading] = useState(true); + const [isLoading, setIsLoading] = useState(false); const [isLoaded, setIsLoaded] = useState(false); const subscribeScriptLoading = useCallback( @@ -94,11 +94,17 @@ const WebAppProvider = ({ }; scriptEle.addEventListener('load', successListener); - scriptEle.addEventListener('error', errorListener); + + return () => { + scriptEle.removeEventListener('load', successListener); + scriptEle.removeEventListener('error', errorListener); + }; } catch (err) { console.error(err); } + + return () => {}; }, [setIsLoaded, setIsLoading, setWebApp], ); @@ -110,7 +116,7 @@ const WebAppProvider = ({ } setIsLoading(true); const existingScripts: NodeListOf = - window.document.querySelectorAll(`script[src~='${SCRIPT}']`); + window.document.querySelectorAll(`script[src='${SCRIPT}']`); if (existingScripts[0]) { return subscribeScriptLoading(existingScripts[0]); } @@ -129,6 +135,8 @@ const WebAppProvider = ({ setIsLoaded(false); setIsLoading(false); } + + return () => {}; }, [subscribeScriptLoading, isLoading, isLoaded]); const systemValue = useMemo(createSystemContextValue, []);