We're happy to announce the availability of a new feature: HTML Diffs. When you run a test now and you check "Save Response Bodies", a new option shows up in the drop down of results pages called HTML Diff. Example result.
It shows what changed between the page's HTML as sent by the server over the network and the HTML that the browser finally used to render the page when the test was complete. The most common reasons for there being a difference is
- the browser "correcting" your HTML
- You have the option to prettify the HTML before the diff is run, which often helps readability, for example for minified content or otherwise long lines.
- The common content before/after shows in black
- The "before" content (server response) that is removed from the final rendered page shows up in red
- The new content that differs from the "before" is shown in green
- The line numbers are links you can share to point to a specific piece of code
anchorHow it works technically
For the "before" HTML we get the response from the browser. For the "after" it's a bit more complicated:
- We start with
- Prepend the doctype (which is not in the
- Loop over all inline styles to guess if something is changed.
The reason for the last one is to capture changes made using CSSOM (CSS Object Model), for example using insertRule(). These don't show up in
outerHTML either. So a primitive CSS parsing happens to find out these. "Primitive" because we don't want to slow down the testing with a proper parsing and because it just works in most instances. If we detect that an inline CSS has changed (by counting the number of "}") we reconstruct the CSS text as a string from the CSSOM and replace the contents of the
style tag, so it shows up in
outerHTML and the diff. If CSS is replaced you see an extra comment at the top of the
style tag saying
/* inner styles set by WPT to match CSSOM */.
- You need to check "Save response bodies" in the Advanced tab when starting a new test
- "Save response bodies" is not working in Firefox at the moment
- Some CSSOM changes might not be captured and some inline CSS may be rewritten (see above)
The browsers are extremely good at dealing with malformed HTML and making sense of it. The Prettier library we use for prettifying - not so much. As a result when you attempt to prettify before the diff, you might get an error. This uncovers malformed HTML server response and an opportunity to improve it.
anchorMore educational opportunities
It's somewhat fascinating to see how the browsers sometimes "rewrite" the HTML and where we have an opportunity to make their job easier. For example extra white space...
... or trailing slashes:
We hope you enjoy the new insights that HTML Diff can provide and find more opportunities to make your pages faster. As this is a first release of the new feature we appreciate any comments, use cases, bug reports, feedback.
Stoyan Stefanov has recently joined WebPageTest by Catchpoint as an Software Engineer (formerly at Facebook and Yahoo!). He has a storied history as a very accomplished author, contributor, and custodian of the well known and respected Perf Planet calendar. He habitually speaks about web development topics at conferences and on his blog at phpied.com. Stoyan also created the smush.it image optimization tool and was the architect of Yahoo's performance optimization tool YSlow 2.0.@stoyanstefanov