Slow load facebook canvas app when click on links (if target top)

跟風遠走 提交于 2019-12-11 04:47:23

问题


Situation: I am developing a facebook canvas app. Facebook is sending my sever a POST request with the signed_request each time that a page is render. Inside my app I have all my links with target="_top" because if I don't, facebook send my server a common GET without the signed request. So I cann't check the user info.

Problem: It is too slow! even if I am testing it in local, each click that I press takes 1 sec to render and my canvas becomes completely white and then the info is shown, It will be a bad user experience.

My tests: If I remove the target=_top and I point all my links' href to my server without the app.facebook.com/whatever, it loads very quickly.

My doubts: Is there any security issue with this? If I point all the links to my server (no apps.facebook.com) I can not check the signed request, I will only check it in the main page..

Any advice? any tutorial? Do I have any misundestanding of this? (It is my first facebook app)


回答1:


Have you read the Server-Side Authentication tutorial? You're doing it wrong.

Once the users lands in your app you should keep all links in the same frame, loading the entire window along with facebook is completely redundant.

What you should do:

When you get the POST with the signed request, decode it and check if the user is authenticated, if he is persist the data (token and such) somewhere (session, db, cache). If he is not authenticated send him to the auth dialog as noted in the tutorial, when he gets back exchange the code you get (in GET) for the token (also shown in the tutorial), then redirect him to http(s)://apps.facebook.com/YOUR_APP and you'll be posted with the authenticated signed request, save it, etc..

Since you persist the data, in every request that is not POST or don't include the signed_request check your persistency choice for the data, and use it.

There should be only two times where facebook sends you the request, once it is POST when your canvas is loaded, the 2nd is when the user returns from the authentication dialog, in which you either get the code parameter or error in case the user declined the authentication. Other requests should be from your app (inside the iframe) into the app servers.




回答2:


JDL,

I believe you are querying the graph API at each request (and that's why you always need the signed_request). Is this right?

The graph API is pretty slow (~ 1 second/query) and you should use only when necessary. For example, when you first receive the access_token you should save it in your session and query the graph API to retrieve the respective facebook user info. But then you should put the info you need about this user in your session and only refresh it (using the graph API) when the signed_request access token is different from the one you have saved in your session.

The behavior of adding _top to the target of your links is ok and a good practice within facebook canvas.



来源:https://stackoverflow.com/questions/9811509/slow-load-facebook-canvas-app-when-click-on-links-if-target-top

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!