Developing JavaScript widgets to be consumed by external websites has many challenges.
This talk addresses most of them, and gives options and strategies to overcome those challenges.
• Challenges Involved • Versioning • You are not the host, you are the thief. • Shared Environment • Bumping the Cross-‐‑Domain Wall • Not Your Grandma’s Cookies • Security • Performance • Questions Thursday, October 3, 13
Site ( widget provider ) • Consumer Sites ( publishers ) • Can have a GUI ( weather forecast ) • May not have GUI too ( analytics, statistics ) • Can be Stateful • Can be Stateless Thursday, October 3, 13
Version Number as an Init Parameter • If it ain’t broke, they won’t fix it. • When’s the last time you updated that Wordpress theme? • Nobody will change that darn version number! Thursday, October 3, 13
Scripts: • A Bootloader Script • A JavaScript Beacon: • Returns 204 No Content if versions match, • Returns an auto-‐‑loader if versions do not match. • Iframe Refresh • window.location.reload(true) Thursday, October 3, 13
or Function.prototype. • Show love to the Module Pa?ern. • Do not slow down publisher: • Async initialization, • Lazy Load. • Do not slow down yourself: • Native is faster, • Use IDs everywhere. Thursday, October 3, 13
window.postMessage API • Hacks • Flash Proxy • Hash Fragment Transport • window.name Transport • Iframe inside an Iframe (klein boJle) • Use Publisher’s Server as a Proxy • JSON with Padding Thursday, October 3, 13
First • Don’t jump straight into hacks. • External Windows as a Rescue • A pop-‐‑up is considered “first party” • What about Opera & Firefox ? • Store session ID as a variable. • Pass to the server at each request. • Do not store on publisher’s page! • Use an IFRAME on API domain for security. Thursday, October 3, 13
• First deny everything, then whitelist known good. • Check referrers, have a list of trusted domains. • Do not trust anyone. function Anyone(){} function Publisher(){} Publisher.prototype = new Anyone(); Thursday, October 3, 13
> ; , ‘ “ into HTML entities. • CSRF • Use a CSRF token. • Denial of Service • Subdomains per publisher ( publisher1.api.example.com ). • ThroJle suspicious requests per subdomain. • Best handled on network / hardware layer. • Session Hijacking • … is a reality. • The only reasonable protection is HTTPS. • Use Multi-‐‑Level Authentication. Thursday, October 3, 13
a UI element. Remedy: • Framekiller scripts • X-‐‑Frame-‐‑Options header • Request confirmation for sensitive actions • Register all your publishers Thursday, October 3, 13
then load dependencies. • Lazy load when possible. • Combine and Minify Assets. • Use CSS Sprites. • Defer images (use a default image, then load original). • Minimize # of HTTP Requests. Thursday, October 3, 13
Requests (thro?le, debounce). • Yield with setTimeout(fn, 0). • Chunk large arrays of instructions. • Improve Perceived Performance: • Be an optimist: act, then verify. Thursday, October 3, 13
optimize prematurely, • Optimizing without measurement is misleading, • It’s hard to measure a third party widget’s performance. • A lot of moving parts involved. • Tools like jsperf.com will not be of much use. • Do not use your 8GB Ram + SSD MacBook for profiling. • Test on an low-‐‑grade machine. • Do not forget mobile! Thursday, October 3, 13