morethanwords / tweb

Telegram Web K, GPL v3
https://web.telegram.org/k/
GNU General Public License v3.0
1.77k stars 574 forks source link

[Feature Request] [Mini Apps] Add application launch timestamp #271

Closed heyqbnk closed 10 months ago

heyqbnk commented 10 months ago

Is your feature request related to a problem? Please describe.

It seems like both Web A and Web K versions of Telegram require some parameter which is responsible for determining the unique application launch. We already discussed this problem in this issue in Web A repository, but @zubiden offered to create an issue in Web K to make a discussion: https://github.com/Ajaxy/telegram-tt/issues/282

As long as we are unable to reload iframe we appended using the default way (iframe.currentWindow.reload() or something) due to security policy violations, we have to reload it by re-inserting the iframe with the initial location.

In turn, this makes the Mini App to lose all its routing history and create possible problems related to components (BackButton, MainButton, etc.) state restoration as long as we don't really know if current application was reloaded or re-launched.

Moreover, external developers are unable to solve this problem by themselves as long as there is no possible solution for this problem at the moment. Each application restart is always considered as fresh (even when page was "reloaded"), because session storage would have the same state (because application is launched in the same tab) and init data can be missing in some cases. So, we can't use init data hash to differ one launch from another.

Describe the solution you'd like

We could probably add some launch parameter called tgWebAppTimestamp which defines the timestamp of launch parameters creation. This will allow developers to differ one application launch from another even when init data is missing.

morethanwords commented 10 months ago

Mini app will now handle reload_iframe event and call location.reload on its side (see commit). You just have to update the library and then check whether the issue is fixed.

zubiden commented 10 months ago

What should happen if the mini app declares reload_supported, but then hangs up or gets killed by the browser? On the user side, reload button stops working.

morethanwords commented 10 months ago

What should happen if the mini app declares reload_supported, but then hangs up or gets killed by the browser? On the user side, reload button stops working.

Added fallback with 300ms. Should be enough.

heyqbnk commented 10 months ago

In my mind, the better solution would be adding new method to set the location which can be used on the Mini App's side.

Think of some method called web_app_set_location which notifies the Telegram application about changes in Mini App's location.

This will let us to avoid creation of any fallbacks and application stuttering, this does not break backward compatibility with previous behavior and in this case we can always say developer "you just forgot to notify Telegram application about location changes". But we should add some checks related to the passed location

heyqbnk commented 10 months ago

So, this could work this way:

  1. Mini App notifies Telegram application about its current location.
  2. Telegram application stores the passed value.
  3. User presses the Reload Page button.
  4. Telegram application uses last passed location to recreate the iframe.

This is not really the native iframe reload and has some flaws, but this will probably work more intuitive. I remember this kind of solution in some other popular platform of the same kind

morethanwords commented 10 months ago

This approach would require implementing it in every other Telegram app, seems like overkill to me.

Current workaround is about fixing the issue that exists only on Web. The only thing developer should do here, is to update the library, reloading will be handled by it.

heyqbnk commented 10 months ago

I have just found out, that current solution still does not cover all cases. The problem is in the case when Mini App didn't respond in time, Telegram application will reload the Mini App with force, making it think, it was just launched, but not reloaded. This still does not fully solve the problem

morethanwords commented 10 months ago

Why can't it respond with iframe_will_reload in time?

heyqbnk commented 10 months ago

Only synchronous extensive computations came into my mind.

I don't really like the approach connected with "you have N time to do it. Otherwise something will break" when it can be avoided. It also brings us new not documented Telegram Mini Apps method property, event and method, when it all could be replaced with the only 1 clear method.

Moreover, I don't like global architecture solutions which are focused on working in some specific library implementing platform functionality. This solution becomes really worse than just implementing the single method in case, there are other libraries rather than Telegram SDK

marcosdodo commented 10 months ago

El El lun, 6 nov 2023 a las 16:24, Eduard Kuzmenko @.***> escribió:

Mini app will now handle reload_iframe event and call location.reload on its side (see commit https://github.com/morethanwords/tweb/commit/d77232985ce417be5bb4261f7e0d3abd7bfa85bf). You just have to update the library and then check whether the issue is fixed.

— Reply to this email directly, view it on GitHub https://github.com/morethanwords/tweb/issues/271#issuecomment-1795085924, or unsubscribe https://github.com/notifications/unsubscribe-auth/A56ZJHWW567MDLK3YB47DZTYDD6KRAVCNFSM6AAAAAA67MRXL2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTOOJVGA4DKOJSGQ . You are receiving this because you are subscribed to this thread.Message ID: @.***>