8 ms·
Ask HN: Why does 'View Source' issue a new HTTP request?
I've noticed that both Firefox and Chrome issue a new HTTP request when you view the source for a web page that you've already loaded. It's particularly annoying when the page itself is slow to load or if it won't load at all.
Why is that? Wouldn't they have the existing source for the originally received page cached already? Is it based on Cache-Control headers?
This has been on my mind for a while (usually comes up when looking at what's behind slow web apps) and came up again with Piwik on the front page[1]. Their website was semi-down (HN hug of death) but eventually loaded. I wanted to see what their GA equivalent tracking code looks like but the page failed to load as rather than showing the cached copy, it tried to fetch a new one.
[1]: https://news.ycombinator.com/item?id=13210195
- jmkni 10y agoI usually have Fiddler running constantly in the background. If a page is slow to load and I want to see the HTML, I would find the actual HTTP request in fiddler and grab the HTML that way. Alternatively, the element inspector doesn't re-issue the request, but it will show the compiled HTML, so after DOM augmentation etc
- TheRealWatson 10y agoIs there anything comparable to Fiddler for macOS? I heard you can run it in VM but that's just too much brute force for me.
- unknownUsers 10y agoFrom what I can tell the Telerik version is on mac also
- fastball 10y agoFiddler is available for MacOS.
- TheRealWatson 10y agoInteresting. I had no idea they had a beta version for macOS. I've gotta try it.
- callahad 10y agohttps://mitmproxy.org/ https://mitmproxy.org/ or https://charlesproxy.com/ https://charlesproxy.com/
- spydum 10y agoBurpsuite has interception proxy and runs on Java.
- jutaz 10y agoI'm not exactly sure why browsers do that, but I suspect that's due to caching. However, I would suggest that you use "Inspect element" and open up dev tools - this way you will see DOM exactly as it is rendered.
- aplummer 10y agoOften the dom has been changed, and you want to know what the server actually sent
- jbm 10y agoYou can still do that with the inspect menu; if you look at the network history in Chrome, you can see the headers sent, the headers received and the body sent / received.
- koolba 10y ago> You can still do that with the inspect menu; if you look at the network history in Chrome, you can see the headers sent, the headers received and the body sent / received. That only works if you have it open before the page loaded. It doesn't save information about network requests that occurred before the inspector was opened.
- noir_lord 10y agoIt would be handy if it did though since I continually forget to open inspector before the request. I'd love a "I'm a developer, store all the things!" setting
- radarsat1 10y ago> I suspect that's due to caching. Shouldn't caching have the opposite effect? i.e. it's treated as a new resource but no new http request is needed because it's in the cache..
- nikbackm 10y agoMaybe the HTML source is discarded after the browser has built the DOM object tree?
- mwill 10y agoI think it may just be because in both cases it opens in a new tab with the view-source modifier, it treats it like you just opened a copy of the page in a new tab? I believe if you open the inspector instead it does not issue a new request.
- koolba 10y ago> I think it may just be because in both cases it opens in a new tab with the view-source modifier, it treats it like you just opened a copy of the page in a new tab? Interesting. I never payed attention to the resource prefix. Are those standardized at all? Both Firefox and Chrome use the same "view-source:" prefix. > I believe if you open the inspector instead it does not issue a new request. The inspector shows the current DOM, not the original loaded HTML, which could be different. It's probably "good enough" as in most cases the original HTML would be the simpler one (say a single div for a single page app) and the live DOM would show what look like right now.
- rohansingh 10y agoIt's not the prefix/scheme that's important, it's the fact that it's a new tab.
- spookylukey 10y agoIt's a bug: https://bugzilla.mozilla.org/show_bug.cgi?id=307089 https://bugzilla.mozilla.org/show_bug.cgi?id=307089 Or, it's a memory saving feature. To implement "View source from cache" requires keeping around the raw page HTML, which you might not otherwise need after parsing - except you probably will for all the developer tools to work, so this probably should just be considered bug.
- koolba 10y agoWow first raised in 2005. Nice!
- chx 10y agoHow much memory is saved here? http://www.httparchive.org/interesting.php?a=All&l=Dec%202%202016 http://www.httparchive.org/interesting.php?a=All&l=Dec%202%2... 52 _kilobytes_ ? Really?
- morley 10y agoThe browser would have to save the source text for EVERY tab it opens, regardless of whether you view source or not. That seems more onerous than making a separate request for the source code every once in a while.
- amelius 10y agoYes, but if you have more than enough memory, then that's no problem. So, the browser should load the HTML page into a part of memory that can be discarded by the OS if the OS needs more memory. Actually I think it is strange that no such memory API exists in Unix. EDIT: Anyway, storing it in the browser's file cache would also do the trick, I suppose :)
- Insanity 10y agoIsn't that one of the issues nowadays, that people have the attitude "we have enough memory" and thus don't tend to care about optimizing for memory usage anymore. Your suggested solution, about discarding when memory is needed, solves this problem. But it solves a problem that would be created by needlessly storing a lot more data than needed. So it's a solution to a problem that was not a problem to begin with. But I know, if it's a few kb, it will not make a huge difference.
- junke 10y agoI seem to recall that it wasn't always the case.
- schoen 10y agoPerhaps pre-DOM, per other comments here?
- samet 10y agoSecond that. That wasn't the default behaviour at once.
- INTPenis 10y agoTrue and I was shocked that the referenced mozilla bug[1] is from 2006. Time really flies because it feels like yesterday I could view source without re-loading it. [1]: https://bugzilla.mozilla.org/show_bug.cgi?id=307089 https://bugzilla.mozilla.org/show_bug.cgi?id=307089
- samet 10y agoSweet memories: http://imgur.com/a/p02cQ http://imgur.com/a/p02cQ
- WallWextra 10y agoSame with save page, etc.
- oneeyedpigeon 10y agoThey (well, Chrome at least) even do that for a POST, which is highly questionable.
- oneeyedpigeon 10y agoI wish View Source were more flexible. I would like: * Raw: a view of the actual body as sent in the response. Although I'm not aware of the current situation, at least some browsers used to subtly alter what was sent, even for View Source (possibly related to validity corrections). I want a guarantee that what I'm viewing is what the server sent. * View source as it is today (with a good common understanding of what that means), but a bit more powerful. Give me a cursor so I can copy from the keyboard, for crying out loud! Maybe even let me edit the source so I can work with static pages more easily. * Something in-between View Source and DOM inspector. E.g. the original source, guaranteed to be untouched by javascript, but cleaned-up for easier reading (given that the source returned by many websites nowadays is practically unreadable (take a look at this page, for example). Reformatting (where possible), maybe automatic expansion of any base href, consistent ordering of attributes, highlighting of errors, etc.
- arethuza 10y agoIsn't a debugging proxy (like Fiddler) a good choice for the first of those?
- deleted 10y ago[deleted]
- LostCharacter 10y agoTo prevent a new browser request and to get a formatted version of the raw source in Chrome, you can open up the dev tools, go to sources, find the name of the page, and then click the curly brackets in the bottom left corner of the tools. It's still missing a good cursor implementation, though.
- nommm-nommm 10y agoDev tools in most browsers does the first as does Fiddler as mentioned in a sibling comment.
- db48x 10y agoHit F7 for caret browsing.
- czep 10y agoOnce the DOM is ready, the browser has no need to keep the original source in memory or cache. For how many page views does the user want to view source? One in a thousand? You're asking the browser to waste space storing something that it will only rarely be asked to display.
- phaser 10y agoit could be garbage collected when user closes the tab.
- anonymfus 10y agoIn Opera up to 12.x it does not.
- bbarn 10y agoI suppose in today's web ecosystem, the initial HTML state of the page is useless in almost all cases after the DOM loads. Rarely will you care about the pre-javascript page, and if you do, you're probably debugging a web site, and the extra load shouldn't matter. Where it could get interesting though, is if the content changes before you view the source. In that case, you're out of luck I guess.
- msie 10y agoYeah, that always annoyed me. So far, i dont see any answer in this page. Really curious, tho.
- deleted 10y ago[deleted]
- Bugseverywhere 10y agoBug? Wow
- f0code 10y agoThe html returned from the already performed HTTP action has been consumed, rendering the view in the browser. If you want to view the raw HTML instead, 'View Source' requests another copy of it, but does not render it.
- seanwilson 10y agoI wouldn't think view-source is a highly used feature that benefits from optimising much and your scenario sounds rare. If a "view-source:..." URL is entered into the browser directly you would need to write code to grab the page from scratch anyway so doing this always makes the logic simpler.
- karmakaze 10y agoI once wondered this, but didn't think much of it. Now seeing the question, it seems quite clear. The DOM+CSS+javascript is pretty much like a computer running a (self-modifying) program. If it didn't do a fresh fetch, then what you'd be seeing is the contents of 'memory' after the program has been running some time. This is useful in itself, so we have Inspect. If you want to see the initial state, the program before execution begins, then you want to View its source(s).