Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens when you go back with the browser?

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...

like image 711
Augustin Riedinger Avatar asked Oct 01 '14 08:10

Augustin Riedinger


People also ask

What happens when back button is pressed in browser?

In javascript, navigation type 2 means browser's back or forward button clicked and the browser is actually taking content from cache. This doesn't work on a single page application, the result is always 1 (which means reload).

Is the back button on a browser just like undo?

Going back is not the same as "undo". You may be looking at stale information without realizing it. The "Back" concept is fine for static content, but falls apart when dealing with dynamic content, such as data entry, dynamic queries, etc.

What happens when you visit a webpage in your browser?

Browser initiates TCP connection with the server. Browser sends the HTTP request to the server. Server processes request and sends back a response. Browser renders the content.

How do websites prevent you from going back?

Script preventing you from going back If neither of the previous sections resolved your issue, you have encountered deliberate, poor, or malicious code. Some websites add code to their pages that prevent users from using the back button to leave their pages or site.


1 Answers

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.
like image 122
Alex Wayne Avatar answered Sep 30 '22 03:09

Alex Wayne