What's the recommended way to rebuild a page from the browser history? (History.js+Rails 3)

安稳与你 提交于 2019-12-23 03:38:22

问题


Many of the pages in my Rails 3 application are composed by widgets. For example there is a user page with url /users/12 that invokes the users controller show action. This page loads the feed widget that makes an ajax call to the feed controller in order to get all the feed items for the user.

So lets' suppose the user does the following:

1) enters in the browser http://mydomain.com/users/12

2) clicks on a button "Today's posts" to see just today's post (this triggers an ajax call to the feed controller that returns the posts

3) enter http://google.com

What is the recommended way to handle the browser's history and back button?

I guess what I need to do is to store enough state information so that when the user clicks on the back button the browser loads the users/12 page and then clicks on the today's filter button again. What is the best way to implement this?

I could store a pointer to a function to be invoked:

History.pushState({"function_name": "restoreUserHomePage"}, "", /users/12/feed?time=today);

Bad idea? Good idea?

Also what is the "right" way to register for the stateChanged (popState in HTML5) event. Potentially each of my page has a different set of widgets.

Thanks for any help.


回答1:


The problem actually is that your browser is caching the xhr request. So when you press the back button, it loads the .js format, instead of the full url that you have if your browser.

Here is how I fixed it :

  #in application_controller
  before_filter :set_cache_buster  
  def set_cache_buster
    if request.xhr?
      response.headers["Cache-Control"] = "no-cache, no-store, max-age=0, must-revalidate"
      response.headers["Pragma"] = "no-cache"
      response.headers["Expires"] = "Fri, 01 Jan 1990 00:00:00 GMT"
    end
  end


来源:https://stackoverflow.com/questions/7340906/whats-the-recommended-way-to-rebuild-a-page-from-the-browser-history-history

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