19

When I press <== back button in the browser, what is going on?

  • Is the query with the same URL made again?
  • Is the current DOM state saved and restored? (HTML only)
  • Is the current page state saved and restored? (HTML + Javascript)
  • Is the server queried but if sends unchanged then local cache is used?
  • In general, can we consider we have up-to-date information on the previous page?

I am unclear because of those situations:

  • Stackoverflows sometimes handles my upvotes very badly (not displaying it, preventing me from undoing because I last voted 5 minutes ago but it was in another tab etc.)
  • When I work on local environment, I don't have the feeling much is queried then
  • I'm always pretty unsure of what's going to come when going back, hence as a developper, avoid using it as much as possible (only to find back a URL in the history actually)

My opinion is that querying again would be the best idea, but it is not the fastest, and browser may want to be fast in that case (to impress the user)... On the other hand, storing page states must cost a lot of memory...

Augustin Riedinger
  • 20,909
  • 29
  • 133
  • 206
  • Nice question, I think it keeps HTML and JS, but I'm not sure, maybe it changes in different browsers – Manjar Oct 01 '14 at 08:33
  • 2
    It depends on the browsers indeed. When you press back, usually nothing will be queried and the cache will be used to load the page. The problem is usually that when you have used JavaScript on a page, and you reload it from cache. – bzeaman Oct 01 '14 at 08:36

2 Answers2

10

It depends.

It depends on HTTP request method. If the page was GET the browser may decide to cache it, and not re-request. POST and other HTTP methods are not cached as they may have side effects server side. This is why you even get a warning dialog if you go back to a page that was loaded by a POST request.

It depends on caching headers. (See here) Pages that are explicitly allowed to be cached as described in their header may be reused when navigated back to.

It depends on the browser. Some have optimized heavily for user experience (more caching, more speed, more staleness). Where others are simpler and simply re-request the page.

It depends on memory usage, especially on mobile devices. The browser may decide not to keep the page content and state if the page is large or there isn't a lot of available memory.

Browsers are complicated pieces of software, and smart people having been working to optimize them for a very long time.


As far as what level of caching is used for back navigation, I think there are three primary levels

(this is probably an oversimplication, but it'll give you the general idea.)

  1. Uncached. Page is re-requested.
  2. Cached assets. The content and assets are cached as they came from the server. This probably doesn't require memory, just disk space, since it's probably not very much data.
  3. Cached state. DOM state, JS heap, form field values, all preserved in memory. This probably requires a lot of memory, and is probably not written to disk since it could be many megabytes depending on the page.
Alex Wayne
  • 178,991
  • 47
  • 309
  • 337
1

The specification of agents (browsers included) in the W3C site, says: http://www.w3.org/TR/2014/WD-UAAG20-Reference-20140925/#sc_315

It is also beneficial for users for whom navigation is time consuming, tiring, or painful, because it allows them to avoid having to re-enter long URLs. The Back feature is a part of the UA user interface instead of the rendered content.

A possible interpretation can be: "Go back, but don't lose data!" each browser define his own back button.

randiel
  • 290
  • 1
  • 16