SlideShare a Scribd company logo
Front-End or Back-End 
Templating? 
Getting the best of both 
Jon Abrams (@JonathanAbrams)
Server Side Rendering 
e.g. Most PHP, Rails, and Django apps 
Jon Abrams (@JonathanAbrams)
Server Side Rendering 
Client Server 
Goes to URL 
Final HTML rendered 
User interacts 
Final HTML rendered 
Entire web page 
reloaded 
Jon Abrams (@JonathanAbrams)
Client Side Rendering 
Jon Abrams (@JonathanAbrams) 
Loading…
Client Side Rendering 
e.g. AngularJS, Backbone, and EmberJS apps 
Jon Abrams (@JonathanAbrams)
Client Side Rendering 
Client Server 
Goes to URL 
Returns layout HTML 
Browser processes JS 
Initiates API call 
Returns JSON data 
User interacts 
Another API call 
Returns JSON data 
Jon Abrams (@JonathanAbrams)
Benefits: Server Side 
vs Client Side
Benefits: Server Side 
vs Client Side 
Server Side: 
• Returns data + layout 
in one go round-trip. 
• Doesn’t require 
JavaScript support 
from client. 
• Has great frameworks 
like Rails and Django. 
Client: 
• Web app uses same API 
as mobile apps. 
• No refreshing while 
using the app. 
• Has great frameworks 
like Angular and Ember.
API Pre-Fetching 
Jon Abrams (@JonathanAbrams)
API Pre-Fetching 
Client Server 
Goes to URL 
User interacts 
Returns layout HTML 
+ JSON data 
Returns JSON data 
Jon Abrams (@JonathanAbrams)
API Pre-Fetching 
Rendered index.html: 
<script src="/jquery.js"></script> 
<script> 
window.apiPrefetchData = { 
"/api/posts": […], 
"/api/user": {…} 
}; 
</script> 
<script src="/main.js"></script>
Front-end rendering without 
API prefetching 
Final Rendering Step Start Time: 607 ms 
Jon Abrams (@JonathanAbrams)
Front-end rendering with 
API prefetching 
Final Rendering Step Start Time: 519 ms 
17% Faster
apiPrefetch.js 
https://github.com/JonAbrams/apiPrefetch.js 
Jon Abrams (@JonathanAbrams)
apiPrefetch.js 
<script src="/apiPrefetch.js"></script> 
<script src="/jquery.js"></script> 
<script> 
window.apiPrefetchData = { 
"/api/posts": […], 
"/api/user": {…} 
https://github.com/JonAbrams/apiPrefetch.js 
}; 
</script> 
<script src="/main.js"></script>
apiPrefetch.js 
<script> 
$.getJSON('/api/posts') // gets prefetched data 
$.getJSON('/api/posts') // hits server 
$.getJSON('/api/stuff') // uses prefetched data 
$.post('/api/user') // hits server 
$.getJSON('/api/user') // uses prefetched data 
</script> 
https://github.com/JonAbrams/apiPrefetch.js 
Main.js:
Back-end Requirements for 
API Pre-fetching 
Jon Abrams (@JonathanAbrams)
1 
Escape ‘forward-slash’
Escape ‘forward-slash’ 
Or else users can inject code. 
"</script><script>alert(“oh no!”)</script>” 
To make it safe: 
</script><script>Still a harmless string</script> 
Jon Abrams (@JonathanAbrams)
Escape ‘forward-slash’ 
JSON.stringify(obj).replace(///g, “/“); 
Jon Abrams (@JonathanAbrams)
2 
Map your view URLs 
to your API URLs
Map your view URLs 
to your API URLs 
For example: 
mysite.com/tweets -> mysite.com/api/tweets 
Jon Abrams (@JonathanAbrams)
3 
Your front-end app needs 
to run in HTML5 mode
Your front-end app needs 
to run in HTML5 mode 
The server is not sent anything after the # character in a URL. 
e.g. 
mysite.com/stuff is good 
mysite.com/#stuff is bad 
Jon Abrams (@JonathanAbrams)
4 
Your API request handlers 
need to be invokable by 
the server itself.
Your API request handlers 
need to be invokable by 
the server itself. 
The same code used to handle incoming API requests 
need to be invoked by your view request handler too. 
Jon Abrams (@JonathanAbrams)
5 
Use cookies for 
authentication
Use cookies for 
authentication 
Or else the server doesn’t know if the user can access the 
data on initial fetch. 
Jon Abrams (@JonathanAbrams)
Libraries / Frameworks 
• Front-end: apiPrefetch.js 
• Node.js: Synth (synthjs.com) 
• Rails: ? 
• Java: ? 
• Python: ? 
• PHP: ? 
Jon Abrams (@JonathanAbrams)
Why Pre-fetch API calls but 
not static assets?
Why Pre-fetch API calls but 
not static assets? 
• Assets are already cached by the browser. 
• Assets can be served from a CDN. 
• Can be fetched (or even pre-fetched) before the JS 
is executed. 
• Assets can be sent down with the initial response 
when HTTP 2.0 is a thing. 
Jon Abrams (@JonathanAbrams)
Links 
apiPrefetch.js: github.com/JonAbrams/apiPrefetch.js 
Synth: synthjs.com 
Me on Twitter: twitter.com/JonathanAbrams

More Related Content

What's hot (20)

PPTX
Rest API Guidelines by HabileLabs
HabileLabs
 
PDF
Don't worry be API with Slim framework and Joomla
Pierre-André Vullioud
 
PDF
Getting Started With Angular
Stormpath
 
PPTX
Building an API using Grape
visnu priya
 
PPTX
Introducing Swagger
Tony Tam
 
PPTX
Mobile APIs: Optimizing APIs for Many Devices
Apigee | Google Cloud
 
PDF
A Debugging Adventure: Journey through Ember.js Glue
Mike North
 
PPTX
4 basic api design guidelines
Surendra kumar
 
PDF
Creating an Effective Mobile API
Nick DeNardis
 
PPT
Story ofcorespring infodeck
Makarand Bhatambarekar
 
PDF
HTBYOOFIYRHT RubyConf
Sandy Vanderbleek
 
PPTX
How to audit Website In SEO
jigneshbhalu101
 
PPTX
Do's and Don'ts of APIs
Jason Harmon
 
PDF
Rails 5 – most effective features for apps upgradation
Andolasoft Inc
 
PDF
Enemy of the state
Mike North
 
PPTX
Phonegap Day 2016: Ember/JS & Hybrid Apps Tips
Alex Blom
 
KEY
Cocoa on-rails-3rd
Xiaochun Shen
 
PDF
Ember,js: Hipster Hamster Framework
Billy Shih
 
PDF
Designing your API Server for mobile apps
Mugunth Kumar
 
PDF
Akka Presentation Schule@synyx
Florian Hopf
 
Rest API Guidelines by HabileLabs
HabileLabs
 
Don't worry be API with Slim framework and Joomla
Pierre-André Vullioud
 
Getting Started With Angular
Stormpath
 
Building an API using Grape
visnu priya
 
Introducing Swagger
Tony Tam
 
Mobile APIs: Optimizing APIs for Many Devices
Apigee | Google Cloud
 
A Debugging Adventure: Journey through Ember.js Glue
Mike North
 
4 basic api design guidelines
Surendra kumar
 
Creating an Effective Mobile API
Nick DeNardis
 
Story ofcorespring infodeck
Makarand Bhatambarekar
 
HTBYOOFIYRHT RubyConf
Sandy Vanderbleek
 
How to audit Website In SEO
jigneshbhalu101
 
Do's and Don'ts of APIs
Jason Harmon
 
Rails 5 – most effective features for apps upgradation
Andolasoft Inc
 
Enemy of the state
Mike North
 
Phonegap Day 2016: Ember/JS & Hybrid Apps Tips
Alex Blom
 
Cocoa on-rails-3rd
Xiaochun Shen
 
Ember,js: Hipster Hamster Framework
Billy Shih
 
Designing your API Server for mobile apps
Mugunth Kumar
 
Akka Presentation Schule@synyx
Florian Hopf
 

Viewers also liked (20)

PPTX
12X Tutor Training
tutorclearinghouse
 
PPT
China Mingyu Holdings Company Profile
mingyugroup
 
PDF
Hizb 44
steka36
 
PPSX
დედამიწის წყლისა და ხმელეთის ობიექტები
irmasurmanidze5
 
TXT
61557874 volume-i-ericsson-umts-rf-optimization-12 dec2003
Mohammad Khamiseh
 
KEY
Archiving Web News (captioned)
SvenAas
 
PPT
Lecture3
uwagankanabata
 
PPS
08 ls8
Jobs
 
PDF
Casino bussiness カジノ 東北復興プラン
Nguyen Minh Tuan
 
PPTX
10 consigli per dormire meglio
Alessandra Cescut
 
PPTX
презентация Microsoft office power point
Albert Ahmadullin
 
PDF
Git basic
Nguyen Minh Tuan
 
PPTX
Bryan Sivak 7-24-14
Steven Randazzo
 
DOCX
Researh
taissoga2012
 
PDF
Don't Climb out of the Window Tonight
Rāwhiti School
 
PDF
Uga 01
Thangarasu A
 
PPTX
Getting to know you assignment
Arlene Thay 伍愛玲
 
PDF
Medicine Cabinet
Sara Faizpour
 
PDF
III Conferência CMMI Portugal, Presentation 1: CMMI implementation using open...
isabelmargarido
 
DOC
COL_P_MATHUR CV for Regional Manager
Pradeep Mathur
 
12X Tutor Training
tutorclearinghouse
 
China Mingyu Holdings Company Profile
mingyugroup
 
Hizb 44
steka36
 
დედამიწის წყლისა და ხმელეთის ობიექტები
irmasurmanidze5
 
61557874 volume-i-ericsson-umts-rf-optimization-12 dec2003
Mohammad Khamiseh
 
Archiving Web News (captioned)
SvenAas
 
Lecture3
uwagankanabata
 
08 ls8
Jobs
 
Casino bussiness カジノ 東北復興プラン
Nguyen Minh Tuan
 
10 consigli per dormire meglio
Alessandra Cescut
 
презентация Microsoft office power point
Albert Ahmadullin
 
Git basic
Nguyen Minh Tuan
 
Bryan Sivak 7-24-14
Steven Randazzo
 
Researh
taissoga2012
 
Don't Climb out of the Window Tonight
Rāwhiti School
 
Uga 01
Thangarasu A
 
Getting to know you assignment
Arlene Thay 伍愛玲
 
Medicine Cabinet
Sara Faizpour
 
III Conferência CMMI Portugal, Presentation 1: CMMI implementation using open...
isabelmargarido
 
COL_P_MATHUR CV for Regional Manager
Pradeep Mathur
 
Ad

Similar to API Prefetching - HTML5DevConf - Oct. 21, 2014 (20)

PDF
WebApp / SPA @ AllFacebook Developer Conference
AllFacebook.de
 
PPTX
Pros and Cons of developing a Thick Clientside App
Ravi Teja
 
PDF
Client vs Server Templating: Speed up initial load for SPA with Angular as an...
David Amend
 
PPTX
Offline web app - Let's kill the downasaur
Kautilya Bhardwaj
 
ODP
Mobile+API
Miguel Paraz
 
KEY
Optimization of modern web applications
Eugene Lazutkin
 
PPTX
Web Dev 21-01-2024.pptx
PARDHIVANNABATTULA
 
PDF
Your app works slowly. Now what?
Aleksandra (Ola) Kunysz
 
KEY
20120802 timisoara
Richard Rodger
 
PDF
08 ajax
Ynon Perek
 
PPTX
AJAX for Scalability
Tuenti
 
PPTX
Ajax For Scalability
erikschultink
 
KEY
20120306 dublin js
Richard Rodger
 
PDF
Modern Web Applications Utilizing HTML5 APIs
Ido Green
 
PDF
Practical Thin Server Architecture With Dojo Sapo Codebits 2008
codebits
 
PPTX
Service workers - Velocity 2016 Training
Patrick Meenan
 
PDF
API SECURITY
Tubagus Rizky Dharmawan
 
PDF
Practical Thin Server Architecture With Dojo Peter Svensson
rajivmordani
 
PDF
Panoramic view of web APIs
Karen Immanuel
 
PDF
Are Today’s Good Practices… Tomorrow’s Performance Anti-Patterns?
Andy Davies
 
WebApp / SPA @ AllFacebook Developer Conference
AllFacebook.de
 
Pros and Cons of developing a Thick Clientside App
Ravi Teja
 
Client vs Server Templating: Speed up initial load for SPA with Angular as an...
David Amend
 
Offline web app - Let's kill the downasaur
Kautilya Bhardwaj
 
Mobile+API
Miguel Paraz
 
Optimization of modern web applications
Eugene Lazutkin
 
Web Dev 21-01-2024.pptx
PARDHIVANNABATTULA
 
Your app works slowly. Now what?
Aleksandra (Ola) Kunysz
 
20120802 timisoara
Richard Rodger
 
08 ajax
Ynon Perek
 
AJAX for Scalability
Tuenti
 
Ajax For Scalability
erikschultink
 
20120306 dublin js
Richard Rodger
 
Modern Web Applications Utilizing HTML5 APIs
Ido Green
 
Practical Thin Server Architecture With Dojo Sapo Codebits 2008
codebits
 
Service workers - Velocity 2016 Training
Patrick Meenan
 
Practical Thin Server Architecture With Dojo Peter Svensson
rajivmordani
 
Panoramic view of web APIs
Karen Immanuel
 
Are Today’s Good Practices… Tomorrow’s Performance Anti-Patterns?
Andy Davies
 
Ad

Recently uploaded (20)

PDF
Rapid Prototyping for XR: Lecture 3 - Video and Paper Prototyping
Mark Billinghurst
 
PDF
FSE-Journal-First-Automated code editing with search-generate-modify.pdf
cl144
 
PDF
Generative AI & Scientific Research : Catalyst for Innovation, Ethics & Impact
AlqualsaDIResearchGr
 
PPTX
WHO And BIS std- for water quality .pptx
dhanashree78
 
PPTX
Computer network Computer network Computer network Computer network
Shrikant317689
 
PPTX
Bitumen Emulsion by Dr Sangita Ex CRRI Delhi
grilcodes
 
PDF
June 2025 Top 10 Sites -Electrical and Electronics Engineering: An Internatio...
elelijjournal653
 
PPT
دراسة حاله لقرية تقع في جنوب غرب السودان
محمد قصص فتوتة
 
PDF
How to Buy Verified CashApp Accounts IN 2025
Buy Verified CashApp Accounts
 
PPTX
How to Un-Obsolete Your Legacy Keypad Design
Epec Engineered Technologies
 
PPTX
Kel.3_A_Review_on_Internet_of_Things_for_Defense_v3.pptx
Endang Saefullah
 
PDF
PRIZ Academy - Process functional modelling
PRIZ Guru
 
PDF
lesson4-occupationalsafetyandhealthohsstandards-240812020130-1a7246d0.pdf
arvingallosa3
 
PDF
Validating a Citizen Observatories enabling Platform by completing a Citizen ...
Diego López-de-Ipiña González-de-Artaza
 
PPTX
Comparison of Flexible and Rigid Pavements in Bangladesh
Arifur Rahman
 
PPTX
Stability of IBR Dominated Grids - IEEE PEDG 2025 - short.pptx
ssuser307730
 
PPTX
Introduction to Python Programming Language
merlinjohnsy
 
PDF
Designing for Tomorrow – Architecture’s Role in the Sustainability Movement
BIM Services
 
PPT
SF 9_Unit 1.ppt software engineering ppt
AmarrKannthh
 
PPSX
OOPS Concepts in Python and Exception Handling
Dr. A. B. Shinde
 
Rapid Prototyping for XR: Lecture 3 - Video and Paper Prototyping
Mark Billinghurst
 
FSE-Journal-First-Automated code editing with search-generate-modify.pdf
cl144
 
Generative AI & Scientific Research : Catalyst for Innovation, Ethics & Impact
AlqualsaDIResearchGr
 
WHO And BIS std- for water quality .pptx
dhanashree78
 
Computer network Computer network Computer network Computer network
Shrikant317689
 
Bitumen Emulsion by Dr Sangita Ex CRRI Delhi
grilcodes
 
June 2025 Top 10 Sites -Electrical and Electronics Engineering: An Internatio...
elelijjournal653
 
دراسة حاله لقرية تقع في جنوب غرب السودان
محمد قصص فتوتة
 
How to Buy Verified CashApp Accounts IN 2025
Buy Verified CashApp Accounts
 
How to Un-Obsolete Your Legacy Keypad Design
Epec Engineered Technologies
 
Kel.3_A_Review_on_Internet_of_Things_for_Defense_v3.pptx
Endang Saefullah
 
PRIZ Academy - Process functional modelling
PRIZ Guru
 
lesson4-occupationalsafetyandhealthohsstandards-240812020130-1a7246d0.pdf
arvingallosa3
 
Validating a Citizen Observatories enabling Platform by completing a Citizen ...
Diego López-de-Ipiña González-de-Artaza
 
Comparison of Flexible and Rigid Pavements in Bangladesh
Arifur Rahman
 
Stability of IBR Dominated Grids - IEEE PEDG 2025 - short.pptx
ssuser307730
 
Introduction to Python Programming Language
merlinjohnsy
 
Designing for Tomorrow – Architecture’s Role in the Sustainability Movement
BIM Services
 
SF 9_Unit 1.ppt software engineering ppt
AmarrKannthh
 
OOPS Concepts in Python and Exception Handling
Dr. A. B. Shinde
 

API Prefetching - HTML5DevConf - Oct. 21, 2014

  • 1. Front-End or Back-End Templating? Getting the best of both Jon Abrams (@JonathanAbrams)
  • 2. Server Side Rendering e.g. Most PHP, Rails, and Django apps Jon Abrams (@JonathanAbrams)
  • 3. Server Side Rendering Client Server Goes to URL Final HTML rendered User interacts Final HTML rendered Entire web page reloaded Jon Abrams (@JonathanAbrams)
  • 4. Client Side Rendering Jon Abrams (@JonathanAbrams) Loading…
  • 5. Client Side Rendering e.g. AngularJS, Backbone, and EmberJS apps Jon Abrams (@JonathanAbrams)
  • 6. Client Side Rendering Client Server Goes to URL Returns layout HTML Browser processes JS Initiates API call Returns JSON data User interacts Another API call Returns JSON data Jon Abrams (@JonathanAbrams)
  • 7. Benefits: Server Side vs Client Side
  • 8. Benefits: Server Side vs Client Side Server Side: • Returns data + layout in one go round-trip. • Doesn’t require JavaScript support from client. • Has great frameworks like Rails and Django. Client: • Web app uses same API as mobile apps. • No refreshing while using the app. • Has great frameworks like Angular and Ember.
  • 9. API Pre-Fetching Jon Abrams (@JonathanAbrams)
  • 10. API Pre-Fetching Client Server Goes to URL User interacts Returns layout HTML + JSON data Returns JSON data Jon Abrams (@JonathanAbrams)
  • 11. API Pre-Fetching Rendered index.html: <script src="/jquery.js"></script> <script> window.apiPrefetchData = { "/api/posts": […], "/api/user": {…} }; </script> <script src="/main.js"></script>
  • 12. Front-end rendering without API prefetching Final Rendering Step Start Time: 607 ms Jon Abrams (@JonathanAbrams)
  • 13. Front-end rendering with API prefetching Final Rendering Step Start Time: 519 ms 17% Faster
  • 15. apiPrefetch.js <script src="/apiPrefetch.js"></script> <script src="/jquery.js"></script> <script> window.apiPrefetchData = { "/api/posts": […], "/api/user": {…} https://github.com/JonAbrams/apiPrefetch.js }; </script> <script src="/main.js"></script>
  • 16. apiPrefetch.js <script> $.getJSON('/api/posts') // gets prefetched data $.getJSON('/api/posts') // hits server $.getJSON('/api/stuff') // uses prefetched data $.post('/api/user') // hits server $.getJSON('/api/user') // uses prefetched data </script> https://github.com/JonAbrams/apiPrefetch.js Main.js:
  • 17. Back-end Requirements for API Pre-fetching Jon Abrams (@JonathanAbrams)
  • 19. Escape ‘forward-slash’ Or else users can inject code. "</script><script>alert(“oh no!”)</script>” To make it safe: </script><script>Still a harmless string</script> Jon Abrams (@JonathanAbrams)
  • 20. Escape ‘forward-slash’ JSON.stringify(obj).replace(///g, “/“); Jon Abrams (@JonathanAbrams)
  • 21. 2 Map your view URLs to your API URLs
  • 22. Map your view URLs to your API URLs For example: mysite.com/tweets -> mysite.com/api/tweets Jon Abrams (@JonathanAbrams)
  • 23. 3 Your front-end app needs to run in HTML5 mode
  • 24. Your front-end app needs to run in HTML5 mode The server is not sent anything after the # character in a URL. e.g. mysite.com/stuff is good mysite.com/#stuff is bad Jon Abrams (@JonathanAbrams)
  • 25. 4 Your API request handlers need to be invokable by the server itself.
  • 26. Your API request handlers need to be invokable by the server itself. The same code used to handle incoming API requests need to be invoked by your view request handler too. Jon Abrams (@JonathanAbrams)
  • 27. 5 Use cookies for authentication
  • 28. Use cookies for authentication Or else the server doesn’t know if the user can access the data on initial fetch. Jon Abrams (@JonathanAbrams)
  • 29. Libraries / Frameworks • Front-end: apiPrefetch.js • Node.js: Synth (synthjs.com) • Rails: ? • Java: ? • Python: ? • PHP: ? Jon Abrams (@JonathanAbrams)
  • 30. Why Pre-fetch API calls but not static assets?
  • 31. Why Pre-fetch API calls but not static assets? • Assets are already cached by the browser. • Assets can be served from a CDN. • Can be fetched (or even pre-fetched) before the JS is executed. • Assets can be sent down with the initial response when HTTP 2.0 is a thing. Jon Abrams (@JonathanAbrams)
  • 32. Links apiPrefetch.js: github.com/JonAbrams/apiPrefetch.js Synth: synthjs.com Me on Twitter: twitter.com/JonathanAbrams