Recently the W3C Technical Architecture Group published a new finding on the role of polyfills on the web. I was the primary author of the finding and in this talk I present the findings recommendations for a developer audience.
Who is this guy? 1. Helped build the original HTML5 web app for the FT (app.ft.com) 2. Created our Origami component system 3. Ran FT Labs for 3 years 4. Now working with Nikkei to rebuild nikkei.com 5. W3C Technical Architecture Group 6. Polyfill.io 7. A bit obsessed with mascots 7
8 Travis Leithead Microsoft Alex Russell Google Mark Nottingham Akamai David Baron Mozilla Tim Berners-Lee W3C/MIT Dan Appelquist Samsung Hadley Beeman Invited expert Peter Linss HP Andrew Betts Financial Times W3C Technical Architecture Group (TAG) Yves Lafron W3C
Tipping point changes 17 Before tipping point After tipping point Use native implementation if it exists const myFoo = nativeFoo || polyfillFoo Use the proposed name window.nativeFoo = polyfillFoo
Tipping point changes 18 const myIsNaN = polyfillNaN; Number.isNaN = Number.isNaN || polyfillNaN; Before: Don't defer to native, don't use the proposed name After: OK to defer to native and use the proposed name
1. Understand stability Look for signs of quality in polyfills, such as: ● Comprehensive test suite, especially if it includes relevant Web Platform Tests ● Effects on the performance profile of your site, measured using in-browser profiling tools or other auditing or monitoring tools or services 21 https://github.com/w3c/web-platform-tests
2. Don't serve unnecessary polyfills If possible, avoid wasting the user's time, money and data allowance ● Feature detect + conditional loading can delay the loading of necessary polyfills ● But better to optimize for modern browsers anyway ● If the worst-case set of polyfills is small, maybe serve them anyway 22 https://html5test.com
3. Keep polyfills up to date Make it possible to update the polyfill ● Consider automation ● Use a service like polyfill.io or a module registry like NPM ● Subscribe to release notifications 24 https://www.npmjs.com/package/npm-check
5. Use caching Polyfills are static, so cache them aggressively. ● Serve polyfill in a static form ● Use a CDN if possible ● Use the immutable directive in the Cache-Control response header. 26 https://www.webpagetest.org
Evergreen as standard ● Great browsers ship modern standards and update automatically ○ Firefox, Chrome, MS Edge, Safari (ish), Opera ● Others… don't. ○ Non-Google Android phones (especially China) ○ Very cheap phones (especially India) ○ Niche browsers (especially China, Russia, Africa) ○ Internet of things devices with embedded browsers 29
Evergreen web recommendations ● Bad / outdated web browsers are a risk to evolution of the web ● Fine for private content / apps ○ programme guide on TV ○ in-flight entertainment UI on aircraft seat ● NOT OK if the device can browse the public internet ● For polyfilling: "least capable" category includes these 39
Polyfill.io ● Analyses each request, identifies the browser ● Packages only the polyfills you need ● Keeps them up to date ● Served via Fastly's global CDN ○ Nodes in Tokyo, Singapore and Hong Kong 40