tma.js: SDK can crash from time to time when using NextJS
Platform
Select platforms where this bug is reproducible:
- Telegram for Android
- Telegram for iOS
- Telegram for macOS
- Telegram Desktop
- Telegram Web A
- Telegram Web K
Describe the bug
When using @tma.js/sdk-react with NextJS an issue with SDK crashing with undefined is not an object (evaluating 'window.TelegramGameProxy.receiveEvent') can appear from time to time
To Reproduce
Steps to reproduce the behavior:
- Mount the SDK provider with the following code
"use client";
import { useState, useEffect, useMemo } from "react";
import { useSDKContext } from "@tma.js/sdk-react";
export const ComponentsProvider = ({
children,
}: {
children: React.ReactNode;
}) => {
const [render, setRender] = useState(false);
const { loading, error } = useSDKContext();
useEffect(() => {
setRender(true);
}, []);
const errorMessage = useMemo<null | string>(() => {
if (!error) {
return null;
}
return error instanceof Error ? error.message : "Unknown error";
}, [error]);
if (!render) return null;
if (error) {
setTimeout(() => {
throw new Error(`SDK error: ${errorMessage}`);
});
return <Alert />;
}
if (loading) {
return <Loading />;
}
return children;
};
- Create several pages linked to each other with
useRouterfromnext/navigation - Navigate between pages and see the issue from time to time
About this issue
- Original URL
- State: closed
- Created 7 months ago
- Comments: 15 (8 by maintainers)
We dig into the problem and could not really find out why the problem sometimes occurs. We were getting it absolutely randomly and extremely rarely. The problem is, for some reason Next server calls function wrapped in
useEffect. To be more accurate, it calls the SDK init which leads to error.There is only 1 assumption - Vercel did not shutdown the broken server and sometimes directs the request to it.
@thecoorum Please, let me know if you find the solution and why this problem occurs. Until then, I am closing the issue assuming, the problem is not in the SDK.
Thanks! I will try this approach and get back to you as soon as possible!