Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Does Stack Overflow Replace State? [closed]

Tags:

javascript

I'm a mac user and never use IE. But yesterday at work I went onto stack overflow and typed this into the browser using IE 9...

http://stackoverflow.com/questions/824349

And they replaced the URL to this without refreshing the page...

http://stackoverflow.com/questions/824349/modify-the-url-without-reloading-the-page

I could not believe what I saw. Any ideas how stack overflow is able to utilize a functionality that mimics the history API's replace state on a browser that does not support it?

like image 669
gmustudent Avatar asked Dec 08 '22 15:12

gmustudent


2 Answers

They actually redirect the user with a 301 redirection. Look at the headers:

GET /questions/824349 HTTP/1.1
Host: stackoverflow.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:21.0) Gecko/20100101 Firefox/21.0
[...]

HTTP/1.1 301 Moved Permanently
Cache-Control: public, max-age=60
Content-Type: text/html
Expires: Sat, 08 Jun 2013 19:00:05 GMT
Last-Modified: Sat, 08 Jun 2013 18:59:05 GMT
Location: /questions/824349/modify-the-url-without-reloading-the-page
Vary: *
X-Frame-Options: SAMEORIGIN
Date: Sat, 08 Jun 2013 18:59:05 GMT
Content-Length: 0
like image 159
Marcelo Pascual Avatar answered Dec 11 '22 09:12

Marcelo Pascual


It's a 301 Moved Permanently redirect, i.e. it's done server side. You don't see a refresh because the browser doesn't open the first URL, it redirects right away to the second.

Here's the result on chrome's console.

enter image description here

like image 25
Telmo Marques Avatar answered Dec 11 '22 09:12

Telmo Marques