2

There is a Rails web-application. Each time I open a browser with kinda the 'restore previous session on startup' option (Chrome, Firefox, Edge) I can see the page with outdated data (old numbers, text, and so on). I have to refresh the page to see the actual data.

Is there any way to get refreshed pages on browser opening? Maybe there is a Rails setting... or there is a way to do something using JS.

So, the basic scenario is looking like this:

  • open a page with some data
  • close the browser (do not close the tab with our page)
  • change the data for this page (e.g. in DB)
  • open the browser and look at the state of our page

Expected result: the page contains relevant ('fresh') data. Actual result: the page contains outdated data.

Can anyone help me with that?

Ivan
  • 85
  • 9

3 Answers3

1

I've found an answer here.

class ApplicationController < ActionController::Base

  before_action :set_cache_headers

  private

  def set_cache_headers
    response.headers["Cache-Control"] = "no-cache, no-store"
    response.headers["Pragma"] = "no-cache"
    response.headers["Expires"] = "Mon, 01 Jan 1990 00:00:00 GMT"
  end
end

The solution is 12 years old but it's still workable.

Ivan
  • 85
  • 9
1

In config/initializers/default_headers.rb (or similar), put the following:

Rails.application.config.action_dispatch.default_headers['Cache-Control'] = 'no-cache, no-store'
Anthony Wang
  • 1,285
  • 1
  • 13
  • 14
0

Haven't came across this issue, but I would probably use some JS to handle it in some way or another.

This could be as simple as storing the page_generated_at time (as unix timestamp probably to avoid need for datetime format handling) somewhere on the page (when the page is rendered from the server), and then adding JS to reload the page if the page_generated_at time is more than X seconds older than the current time, which would be a few lines of JS.

Nuclearman
  • 5,029
  • 1
  • 19
  • 35
  • Hmm... that's actually an interesting idea, but it doesn't solve the problem of browser reopening. As a user, I expect to see an absolutely relevant page after browser opening (with previous tabs in it). And I don't even get if it's a back-end issue, a front-end one, or just browser configuration. – Ivan Apr 24 '21 at 05:13
  • Yea, it's more of a test you could run. Does the JS run on browser load or not? If it does, it should give you the relevant page (as it'll load it fresh when the browser opens). Though, there might also be a JS event you could hook into that might work better (but would still need JS to run). As to the source, I would lean strongly towards browser configuration, though it could also be a cache heading issue (or the lack of them, perhaps having a "never cache" type heading would help), but don't use cache headings too much as most of the stuff I work on isn't well suited to be browser cached. – Nuclearman Apr 25 '21 at 16:45
  • Well, I can't ask my customers to configure their browsers in a specific way. It should kinda work everywhere by default. So, if there is a browser opening event, it can help me. – Ivan Apr 26 '21 at 18:50