Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Page instantly closed when typed directly in browser's address bar #428

Open
hiendaovinh opened this issue Jul 30, 2024 · 12 comments
Assignees
Labels
bug Something isn't working

Comments

@hiendaovinh
Copy link

hiendaovinh commented Jul 30, 2024

Telegram Application

Other

Describe the Bug

With the template of Typescript-tma.js-React.js, there is a bug with the navigator which closes the page instantly when opened by typing in the address bar instead of clicking on the link.
With the navigator.attach(); commented out in the App.tsx, the problem never occurred.

To Reproduce

Steps to reproduce the behavior:

  1. npx @telegram-apps/create-mini-app@latest
  2. Typescript-tma.js-React.js
  3. Enter this in the address bar http://localhost:5173/reactjs-template
  4. The page closes
Peek.2024-07-30.19-23.mp4

Expected Behavior

It behaves normally.

@hiendaovinh hiendaovinh added the bug Something isn't working label Jul 30, 2024
@incpo
Copy link

incpo commented Aug 1, 2024

Same, it's redirects back. Maybe it's because of react hash router.

It's also a problem for telegram web, because there are the same issue.

@heyqbnk
Copy link
Member

heyqbnk commented Aug 1, 2024

Yeah, I know about this issue. Will take a look this month.

I am not sure, but you could move the navigator initialization higher, maybe in the index.ts file, wait for it attach and then render the React tree. A possible reason is the navigator conflicting with the React Router. Could you try this solution?

@hiendaovinh
Copy link
Author

Yeah, I know about this issue. Will take a look this month.

I am not sure, but you could move the navigator initialization higher, maybe in the index.ts file, wait for it attach and then render the React tree. A possible reason is the navigator conflicting with the React Router. Could you try this solution?

For now, the snippet of initiating the navigator and Router rendering is in App component. You mean moving this to the Root component right?

// Create a new application navigator and attach it to the browser history, so it could modify
// it and listen to its changes.
const navigator = useMemo(() => initNavigator("app-navigation-state"), []);
const [location, reactNavigator] = useIntegration(navigator);

// Don't forget to attach the navigator to allow it to control the BackButton state as well
// as browser history.
useEffect(() => {
	navigator.attach();
	return () => navigator.detach();
}, [navigator]);

@heyqbnk
Copy link
Member

heyqbnk commented Aug 1, 2024

No, do something like this:

// index.ts
import { initNavigator } from '@telegram-apps/sdk';

const navigator = initNavigator("app-navigation-state");
await navigator.attach();

ReactDOM.createRoot(document.getElementById('root')!).render(<Root navigator={navigator} />);

Then, use the integration using passed navigator inside App.tsx. This solution is far from the perfect, but may serve as a temporary solution

@incpo
Copy link

incpo commented Aug 1, 2024

Nah, still doesn't work for me, even with that statement. The attach method is messing up something, breaking the logic or whatever. Tried pushing some values into history state, but still has the same problem

// index.tsx
const navigator = initNavigator('app-navigation-state');
await navigator.attach();

ReactDOM.createRoot(document.getElementById('root')!).render(
  <QueryClientProvider client={queryClient}>
    {navigator.attached ? <Root navigator={navigator} /> : null}
  </QueryClientProvider>,
);

@hiendaovinh
Copy link
Author

It doesn't work for me.

@heyqbnk
Copy link
Member

heyqbnk commented Aug 6, 2024

Currently working on a new major version of the package. Will take a look and solve the problem there

@heyqbnk
Copy link
Member

heyqbnk commented Aug 7, 2024

Hey! I have looked into the problem and found out what was the reason.

As long as I am using a bit different Chromium-based browser (Yandex Browser), its behavior differs a bit from Google Chrome, so I couldn't see the bug.

It seems like Google Chrome determines the initial new tab page as a complete history entry, so visiting the mini app from this tab makes history.length equal to 2. The navigator inside the mini app is rather greedy and clears the history, so it could have a complete control over it. By clearing we understand a complete history reset and make the history cursor equal to 1. Cursor with value 1 refers to the initial history item, which is a Google tab.

As a temporary solution, you could try using a different browser for development (I know, this is awful solution). I am working on solving the problem.

I am not sure, but I assume that this problem is only reproducible in development, right? I mean, you can't reproduce it deploying the app to something like GitHub Pages and opening the app inside Telegram.

@incpo
Copy link

incpo commented Aug 13, 2024

Hey! I have looked into the problem and found out what was the reason.

As long as I am using a bit different Chromium-based browser (Yandex Browser), its behavior differs a bit from Google Chrome, so I couldn't see the bug.

It seems like Google Chrome determines the initial new tab page as a complete history entry, so visiting the mini app from this tab makes history.length equal to 2. The navigator inside the mini app is rather greedy and clears the history, so it could have a complete control over it. By clearing we understand a complete history reset and make the history cursor equal to 1. Cursor with value 1 refers to the initial history item, which is a Google tab.

As a temporary solution, you could try using a different browser for development (I know, this is awful solution). I am working on solving the problem.

I am not sure, but I assume that this problem is only reproducible in development, right? I mean, you can't reproduce it deploying the app to something like GitHub Pages and opening the app inside Telegram.

It's the same for production - try 'build & preview', same for Mozilla firefox.
Like to see that you find the problem

@GrayYoung
Copy link

popstate event not be received in iFrame on Firefox and Safari, therefore go function https://github.com/Telegram-Mini-Apps/telegram-apps/blob/bb789377bc81609583c7ee7cff105e0152cc1742/packages/sdk/src/navigation/go.ts return a promise with a false value, then go(-1) be called, finally return about:blank page.

@zakthedev
Copy link

any solution to this so far?

@heyqbnk
Copy link
Member

heyqbnk commented Oct 4, 2024

Hey guys. I postponed the development of the SDK's navigation section, so it will take a bit more time to solve the problem. I would recommend temporarily removing the tapps Navigator in favor of the main library (React, Solid, etc.) own router

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants