SlideShare a Scribd company logo
1 of 32
Download to read offline
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

Rest API Guidelines by HabileLabs
Rest API Guidelines by HabileLabsRest API Guidelines by HabileLabs
Rest API Guidelines by HabileLabsHabilelabs
 
Don't worry be API with Slim framework and Joomla
Don't worry be API with Slim framework and JoomlaDon't worry be API with Slim framework and Joomla
Don't worry be API with Slim framework and JoomlaPierre-André Vullioud
 
Getting Started With Angular
Getting Started With AngularGetting Started With Angular
Getting Started With AngularStormpath
 
Building an API using Grape
Building an API using GrapeBuilding an API using Grape
Building an API using Grapevisnu priya
 
Introducing Swagger
Introducing SwaggerIntroducing Swagger
Introducing SwaggerTony Tam
 
Mobile APIs: Optimizing APIs for Many Devices
Mobile APIs: Optimizing APIs for Many DevicesMobile APIs: Optimizing APIs for Many Devices
Mobile APIs: Optimizing APIs for Many DevicesApigee | Google Cloud
 
A Debugging Adventure: Journey through Ember.js Glue
A Debugging Adventure: Journey through Ember.js GlueA Debugging Adventure: Journey through Ember.js Glue
A Debugging Adventure: Journey through Ember.js GlueMike North
 
4 basic api design guidelines
4 basic api design guidelines4 basic api design guidelines
4 basic api design guidelinesSurendra kumar
 
Creating an Effective Mobile API
Creating an Effective Mobile API Creating an Effective Mobile API
Creating an Effective Mobile API Nick DeNardis
 
How to audit Website In SEO
How to audit Website In SEOHow to audit Website In SEO
How to audit Website In SEOjigneshbhalu101
 
Do's and Don'ts of APIs
Do's and Don'ts of APIsDo's and Don'ts of APIs
Do's and Don'ts of APIsJason Harmon
 
Rails 5 – most effective features for apps upgradation
Rails 5 – most effective features for apps upgradationRails 5 – most effective features for apps upgradation
Rails 5 – most effective features for apps upgradationAndolasoft Inc
 
Enemy of the state
Enemy of the stateEnemy of the state
Enemy of the stateMike North
 
Phonegap Day 2016: Ember/JS & Hybrid Apps Tips
Phonegap Day 2016: Ember/JS & Hybrid Apps TipsPhonegap Day 2016: Ember/JS & Hybrid Apps Tips
Phonegap Day 2016: Ember/JS & Hybrid Apps TipsAlex Blom
 
Ember,js: Hipster Hamster Framework
Ember,js: Hipster Hamster FrameworkEmber,js: Hipster Hamster Framework
Ember,js: Hipster Hamster FrameworkBilly Shih
 
Designing your API Server for mobile apps
Designing your API Server for mobile appsDesigning your API Server for mobile apps
Designing your API Server for mobile appsMugunth Kumar
 
Akka Presentation Schule@synyx
Akka Presentation Schule@synyxAkka Presentation Schule@synyx
Akka Presentation Schule@synyxFlorian Hopf
 

What's hot (20)

Rest API Guidelines by HabileLabs
Rest API Guidelines by HabileLabsRest API Guidelines by HabileLabs
Rest API Guidelines by HabileLabs
 
Don't worry be API with Slim framework and Joomla
Don't worry be API with Slim framework and JoomlaDon't worry be API with Slim framework and Joomla
Don't worry be API with Slim framework and Joomla
 
Getting Started With Angular
Getting Started With AngularGetting Started With Angular
Getting Started With Angular
 
Building an API using Grape
Building an API using GrapeBuilding an API using Grape
Building an API using Grape
 
Introducing Swagger
Introducing SwaggerIntroducing Swagger
Introducing Swagger
 
Mobile APIs: Optimizing APIs for Many Devices
Mobile APIs: Optimizing APIs for Many DevicesMobile APIs: Optimizing APIs for Many Devices
Mobile APIs: Optimizing APIs for Many Devices
 
A Debugging Adventure: Journey through Ember.js Glue
A Debugging Adventure: Journey through Ember.js GlueA Debugging Adventure: Journey through Ember.js Glue
A Debugging Adventure: Journey through Ember.js Glue
 
4 basic api design guidelines
4 basic api design guidelines4 basic api design guidelines
4 basic api design guidelines
 
Creating an Effective Mobile API
Creating an Effective Mobile API Creating an Effective Mobile API
Creating an Effective Mobile API
 
Story ofcorespring infodeck
Story ofcorespring infodeckStory ofcorespring infodeck
Story ofcorespring infodeck
 
HTBYOOFIYRHT RubyConf
HTBYOOFIYRHT RubyConfHTBYOOFIYRHT RubyConf
HTBYOOFIYRHT RubyConf
 
How to audit Website In SEO
How to audit Website In SEOHow to audit Website In SEO
How to audit Website In SEO
 
Do's and Don'ts of APIs
Do's and Don'ts of APIsDo's and Don'ts of APIs
Do's and Don'ts of APIs
 
Rails 5 – most effective features for apps upgradation
Rails 5 – most effective features for apps upgradationRails 5 – most effective features for apps upgradation
Rails 5 – most effective features for apps upgradation
 
Enemy of the state
Enemy of the stateEnemy of the state
Enemy of the state
 
Phonegap Day 2016: Ember/JS & Hybrid Apps Tips
Phonegap Day 2016: Ember/JS & Hybrid Apps TipsPhonegap Day 2016: Ember/JS & Hybrid Apps Tips
Phonegap Day 2016: Ember/JS & Hybrid Apps Tips
 
Cocoa on-rails-3rd
Cocoa on-rails-3rdCocoa on-rails-3rd
Cocoa on-rails-3rd
 
Ember,js: Hipster Hamster Framework
Ember,js: Hipster Hamster FrameworkEmber,js: Hipster Hamster Framework
Ember,js: Hipster Hamster Framework
 
Designing your API Server for mobile apps
Designing your API Server for mobile appsDesigning your API Server for mobile apps
Designing your API Server for mobile apps
 
Akka Presentation Schule@synyx
Akka Presentation Schule@synyxAkka Presentation Schule@synyx
Akka Presentation Schule@synyx
 

Viewers also liked

China Mingyu Holdings Company Profile
China Mingyu Holdings Company ProfileChina Mingyu Holdings Company Profile
China Mingyu Holdings Company Profilemingyugroup
 
დედამიწის წყლისა და ხმელეთის ობიექტები
დედამიწის წყლისა და ხმელეთის ობიექტებიდედამიწის წყლისა და ხმელეთის ობიექტები
დედამიწის წყლისა და ხმელეთის ობიექტებიirmasurmanidze5
 
61557874 volume-i-ericsson-umts-rf-optimization-12 dec2003
61557874 volume-i-ericsson-umts-rf-optimization-12 dec200361557874 volume-i-ericsson-umts-rf-optimization-12 dec2003
61557874 volume-i-ericsson-umts-rf-optimization-12 dec2003Mohammad Khamiseh
 
Archiving Web News (captioned)
Archiving Web News (captioned)Archiving Web News (captioned)
Archiving Web News (captioned)SvenAas
 
08 ls8
08 ls808 ls8
08 ls8Jobs
 
Casino bussiness カジノ 東北復興プラン
Casino bussiness カジノ 東北復興プランCasino bussiness カジノ 東北復興プラン
Casino bussiness カジノ 東北復興プランNguyen Minh Tuan
 
10 consigli per dormire meglio
10 consigli per dormire meglio10 consigli per dormire meglio
10 consigli per dormire meglioAlessandra Cescut
 
презентация Microsoft office power point
презентация Microsoft office power pointпрезентация Microsoft office power point
презентация Microsoft office power pointAlbert Ahmadullin
 
Don't Climb out of the Window Tonight
Don't Climb out of the Window TonightDon't Climb out of the Window Tonight
Don't Climb out of the Window TonightRāwhiti School
 
III Conferência CMMI Portugal, Presentation 1: CMMI implementation using open...
III Conferência CMMI Portugal, Presentation 1: CMMI implementation using open...III Conferência CMMI Portugal, Presentation 1: CMMI implementation using open...
III Conferência CMMI Portugal, Presentation 1: CMMI implementation using open...isabelmargarido
 
COL_P_MATHUR CV for Regional Manager
COL_P_MATHUR CV for Regional ManagerCOL_P_MATHUR CV for Regional Manager
COL_P_MATHUR CV for Regional ManagerPradeep Mathur
 

Viewers also liked (20)

12X Tutor Training
12X Tutor Training12X Tutor Training
12X Tutor Training
 
China Mingyu Holdings Company Profile
China Mingyu Holdings Company ProfileChina Mingyu Holdings Company Profile
China Mingyu Holdings Company Profile
 
Hizb 44
Hizb 44Hizb 44
Hizb 44
 
დედამიწის წყლისა და ხმელეთის ობიექტები
დედამიწის წყლისა და ხმელეთის ობიექტებიდედამიწის წყლისა და ხმელეთის ობიექტები
დედამიწის წყლისა და ხმელეთის ობიექტები
 
61557874 volume-i-ericsson-umts-rf-optimization-12 dec2003
61557874 volume-i-ericsson-umts-rf-optimization-12 dec200361557874 volume-i-ericsson-umts-rf-optimization-12 dec2003
61557874 volume-i-ericsson-umts-rf-optimization-12 dec2003
 
Archiving Web News (captioned)
Archiving Web News (captioned)Archiving Web News (captioned)
Archiving Web News (captioned)
 
Lecture3
Lecture3Lecture3
Lecture3
 
08 ls8
08 ls808 ls8
08 ls8
 
Casino bussiness カジノ 東北復興プラン
Casino bussiness カジノ 東北復興プランCasino bussiness カジノ 東北復興プラン
Casino bussiness カジノ 東北復興プラン
 
10 consigli per dormire meglio
10 consigli per dormire meglio10 consigli per dormire meglio
10 consigli per dormire meglio
 
презентация Microsoft office power point
презентация Microsoft office power pointпрезентация Microsoft office power point
презентация Microsoft office power point
 
Git basic
Git basicGit basic
Git basic
 
Bryan Sivak 7-24-14
Bryan Sivak 7-24-14Bryan Sivak 7-24-14
Bryan Sivak 7-24-14
 
Researh
ResearhResearh
Researh
 
Don't Climb out of the Window Tonight
Don't Climb out of the Window TonightDon't Climb out of the Window Tonight
Don't Climb out of the Window Tonight
 
Uga 01
Uga 01Uga 01
Uga 01
 
Getting to know you assignment
Getting to know you assignmentGetting to know you assignment
Getting to know you assignment
 
Medicine Cabinet
Medicine CabinetMedicine Cabinet
Medicine Cabinet
 
III Conferência CMMI Portugal, Presentation 1: CMMI implementation using open...
III Conferência CMMI Portugal, Presentation 1: CMMI implementation using open...III Conferência CMMI Portugal, Presentation 1: CMMI implementation using open...
III Conferência CMMI Portugal, Presentation 1: CMMI implementation using open...
 
COL_P_MATHUR CV for Regional Manager
COL_P_MATHUR CV for Regional ManagerCOL_P_MATHUR CV for Regional Manager
COL_P_MATHUR CV for Regional Manager
 

Similar to API Prefetching - HTML5DevConf - Oct. 21, 2014

WebApp / SPA @ AllFacebook Developer Conference
WebApp / SPA @ AllFacebook Developer ConferenceWebApp / SPA @ AllFacebook Developer Conference
WebApp / SPA @ AllFacebook Developer ConferenceAllFacebook.de
 
Firefox OS Workshop @ Serbia & Montenegro - Training
Firefox OS Workshop @ Serbia & Montenegro - TrainingFirefox OS Workshop @ Serbia & Montenegro - Training
Firefox OS Workshop @ Serbia & Montenegro - TrainingJan Jongboom
 
Hilfe, wir brauchen ein Frontend
Hilfe, wir brauchen ein FrontendHilfe, wir brauchen ein Frontend
Hilfe, wir brauchen ein FrontendOPEN KNOWLEDGE GmbH
 
Introduction to Ember.js and how we used it at FlowPro.io
Introduction to Ember.js and how we used it at FlowPro.ioIntroduction to Ember.js and how we used it at FlowPro.io
Introduction to Ember.js and how we used it at FlowPro.ioPaul Knittel
 
Isomorphic web application
Isomorphic web applicationIsomorphic web application
Isomorphic web applicationOliver N
 
Web Application Frameworks (WAF)
Web Application Frameworks (WAF)Web Application Frameworks (WAF)
Web Application Frameworks (WAF)Ako Kaman
 
Decoupled Architecture and WordPress
Decoupled Architecture and WordPressDecoupled Architecture and WordPress
Decoupled Architecture and WordPressPantheon
 
Why your next serverless project should use AWS AppSync
Why your next serverless project should use AWS AppSyncWhy your next serverless project should use AWS AppSync
Why your next serverless project should use AWS AppSyncYan Cui
 
Writing a Fullstack Application with Javascript - Remote media player
Writing a Fullstack Application with Javascript - Remote media playerWriting a Fullstack Application with Javascript - Remote media player
Writing a Fullstack Application with Javascript - Remote media playerTikal Knowledge
 
Rapid API Development with LoopBack/StrongLoop
Rapid API Development with LoopBack/StrongLoopRapid API Development with LoopBack/StrongLoop
Rapid API Development with LoopBack/StrongLoopRaymond Camden
 
Building Better Web APIs with Rails
Building Better Web APIs with RailsBuilding Better Web APIs with Rails
Building Better Web APIs with RailsAll Things Open
 
Web Apps: APIs' Nightmare
Web Apps: APIs' NightmareWeb Apps: APIs' Nightmare
Web Apps: APIs' NightmarePaulo Silva
 
How Disney Streaming Services and TrueCar Deliver Web Applications for Scale,...
How Disney Streaming Services and TrueCar Deliver Web Applications for Scale,...How Disney Streaming Services and TrueCar Deliver Web Applications for Scale,...
How Disney Streaming Services and TrueCar Deliver Web Applications for Scale,...Amazon Web Services
 
API Workshop: Deep dive into REST APIs
API Workshop: Deep dive into REST APIsAPI Workshop: Deep dive into REST APIs
API Workshop: Deep dive into REST APIsTom Johnson
 
API Product Opportunity Responsibility Nicolas Sierro 2015.pptx
API Product Opportunity Responsibility Nicolas Sierro 2015.pptxAPI Product Opportunity Responsibility Nicolas Sierro 2015.pptx
API Product Opportunity Responsibility Nicolas Sierro 2015.pptxBlockchainizator
 

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

WebApp / SPA @ AllFacebook Developer Conference
WebApp / SPA @ AllFacebook Developer ConferenceWebApp / SPA @ AllFacebook Developer Conference
WebApp / SPA @ AllFacebook Developer Conference
 
Firefox OS Workshop @ Serbia & Montenegro - Training
Firefox OS Workshop @ Serbia & Montenegro - TrainingFirefox OS Workshop @ Serbia & Montenegro - Training
Firefox OS Workshop @ Serbia & Montenegro - Training
 
Hilfe, wir brauchen ein Frontend
Hilfe, wir brauchen ein FrontendHilfe, wir brauchen ein Frontend
Hilfe, wir brauchen ein Frontend
 
Introduction to Ember.js and how we used it at FlowPro.io
Introduction to Ember.js and how we used it at FlowPro.ioIntroduction to Ember.js and how we used it at FlowPro.io
Introduction to Ember.js and how we used it at FlowPro.io
 
Ng-init
Ng-init Ng-init
Ng-init
 
Ng-init
Ng-init Ng-init
Ng-init
 
Isomorphic web application
Isomorphic web applicationIsomorphic web application
Isomorphic web application
 
In app search 1
In app search 1In app search 1
In app search 1
 
Web Application Frameworks (WAF)
Web Application Frameworks (WAF)Web Application Frameworks (WAF)
Web Application Frameworks (WAF)
 
Decoupled Architecture and WordPress
Decoupled Architecture and WordPressDecoupled Architecture and WordPress
Decoupled Architecture and WordPress
 
Why your next serverless project should use AWS AppSync
Why your next serverless project should use AWS AppSyncWhy your next serverless project should use AWS AppSync
Why your next serverless project should use AWS AppSync
 
Writing a Fullstack Application with Javascript - Remote media player
Writing a Fullstack Application with Javascript - Remote media playerWriting a Fullstack Application with Javascript - Remote media player
Writing a Fullstack Application with Javascript - Remote media player
 
ITT Flisol 2013
ITT Flisol 2013ITT Flisol 2013
ITT Flisol 2013
 
Rapid API Development with LoopBack/StrongLoop
Rapid API Development with LoopBack/StrongLoopRapid API Development with LoopBack/StrongLoop
Rapid API Development with LoopBack/StrongLoop
 
Hybrid mobile apps
Hybrid mobile appsHybrid mobile apps
Hybrid mobile apps
 
Building Better Web APIs with Rails
Building Better Web APIs with RailsBuilding Better Web APIs with Rails
Building Better Web APIs with Rails
 
Web Apps: APIs' Nightmare
Web Apps: APIs' NightmareWeb Apps: APIs' Nightmare
Web Apps: APIs' Nightmare
 
How Disney Streaming Services and TrueCar Deliver Web Applications for Scale,...
How Disney Streaming Services and TrueCar Deliver Web Applications for Scale,...How Disney Streaming Services and TrueCar Deliver Web Applications for Scale,...
How Disney Streaming Services and TrueCar Deliver Web Applications for Scale,...
 
API Workshop: Deep dive into REST APIs
API Workshop: Deep dive into REST APIsAPI Workshop: Deep dive into REST APIs
API Workshop: Deep dive into REST APIs
 
API Product Opportunity Responsibility Nicolas Sierro 2015.pptx
API Product Opportunity Responsibility Nicolas Sierro 2015.pptxAPI Product Opportunity Responsibility Nicolas Sierro 2015.pptx
API Product Opportunity Responsibility Nicolas Sierro 2015.pptx
 

Recently uploaded

Ground Improvement Technique: Earth Reinforcement
Ground Improvement Technique: Earth ReinforcementGround Improvement Technique: Earth Reinforcement
Ground Improvement Technique: Earth ReinforcementDr. Deepak Mudgal
 
Basic Electronics for diploma students as per technical education Kerala Syll...
Basic Electronics for diploma students as per technical education Kerala Syll...Basic Electronics for diploma students as per technical education Kerala Syll...
Basic Electronics for diploma students as per technical education Kerala Syll...ppkakm
 
Max. shear stress theory-Maximum Shear Stress Theory ​ Maximum Distortional ...
Max. shear stress theory-Maximum Shear Stress Theory ​  Maximum Distortional ...Max. shear stress theory-Maximum Shear Stress Theory ​  Maximum Distortional ...
Max. shear stress theory-Maximum Shear Stress Theory ​ Maximum Distortional ...ronahami
 
Computer Graphics Introduction To Curves
Computer Graphics Introduction To CurvesComputer Graphics Introduction To Curves
Computer Graphics Introduction To CurvesChandrakantDivate1
 
Memory Interfacing of 8086 with DMA 8257
Memory Interfacing of 8086 with DMA 8257Memory Interfacing of 8086 with DMA 8257
Memory Interfacing of 8086 with DMA 8257subhasishdas79
 
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...Amil baba
 
Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...
Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...
Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...ssuserdfc773
 
8086 Microprocessor Architecture: 16-bit microprocessor
8086 Microprocessor Architecture: 16-bit microprocessor8086 Microprocessor Architecture: 16-bit microprocessor
8086 Microprocessor Architecture: 16-bit microprocessorAshwiniTodkar4
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsvanyagupta248
 
Theory of Time 2024 (Universal Theory for Everything)
Theory of Time 2024 (Universal Theory for Everything)Theory of Time 2024 (Universal Theory for Everything)
Theory of Time 2024 (Universal Theory for Everything)Ramkumar k
 
Augmented Reality (AR) with Augin Software.pptx
Augmented Reality (AR) with Augin Software.pptxAugmented Reality (AR) with Augin Software.pptx
Augmented Reality (AR) with Augin Software.pptxMustafa Ahmed
 
Introduction to Robotics in Mechanical Engineering.pptx
Introduction to Robotics in Mechanical Engineering.pptxIntroduction to Robotics in Mechanical Engineering.pptx
Introduction to Robotics in Mechanical Engineering.pptxhublikarsn
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdfAldoGarca30
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayEpec Engineered Technologies
 
Introduction to Artificial Intelligence ( AI)
Introduction to Artificial Intelligence ( AI)Introduction to Artificial Intelligence ( AI)
Introduction to Artificial Intelligence ( AI)ChandrakantDivate1
 
PE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and propertiesPE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and propertiessarkmank1
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXssuser89054b
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptxJIT KUMAR GUPTA
 
Post office management system project ..pdf
Post office management system project ..pdfPost office management system project ..pdf
Post office management system project ..pdfKamal Acharya
 

Recently uploaded (20)

Ground Improvement Technique: Earth Reinforcement
Ground Improvement Technique: Earth ReinforcementGround Improvement Technique: Earth Reinforcement
Ground Improvement Technique: Earth Reinforcement
 
Signal Processing and Linear System Analysis
Signal Processing and Linear System AnalysisSignal Processing and Linear System Analysis
Signal Processing and Linear System Analysis
 
Basic Electronics for diploma students as per technical education Kerala Syll...
Basic Electronics for diploma students as per technical education Kerala Syll...Basic Electronics for diploma students as per technical education Kerala Syll...
Basic Electronics for diploma students as per technical education Kerala Syll...
 
Max. shear stress theory-Maximum Shear Stress Theory ​ Maximum Distortional ...
Max. shear stress theory-Maximum Shear Stress Theory ​  Maximum Distortional ...Max. shear stress theory-Maximum Shear Stress Theory ​  Maximum Distortional ...
Max. shear stress theory-Maximum Shear Stress Theory ​ Maximum Distortional ...
 
Computer Graphics Introduction To Curves
Computer Graphics Introduction To CurvesComputer Graphics Introduction To Curves
Computer Graphics Introduction To Curves
 
Memory Interfacing of 8086 with DMA 8257
Memory Interfacing of 8086 with DMA 8257Memory Interfacing of 8086 with DMA 8257
Memory Interfacing of 8086 with DMA 8257
 
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
 
Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...
Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...
Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...
 
8086 Microprocessor Architecture: 16-bit microprocessor
8086 Microprocessor Architecture: 16-bit microprocessor8086 Microprocessor Architecture: 16-bit microprocessor
8086 Microprocessor Architecture: 16-bit microprocessor
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech students
 
Theory of Time 2024 (Universal Theory for Everything)
Theory of Time 2024 (Universal Theory for Everything)Theory of Time 2024 (Universal Theory for Everything)
Theory of Time 2024 (Universal Theory for Everything)
 
Augmented Reality (AR) with Augin Software.pptx
Augmented Reality (AR) with Augin Software.pptxAugmented Reality (AR) with Augin Software.pptx
Augmented Reality (AR) with Augin Software.pptx
 
Introduction to Robotics in Mechanical Engineering.pptx
Introduction to Robotics in Mechanical Engineering.pptxIntroduction to Robotics in Mechanical Engineering.pptx
Introduction to Robotics in Mechanical Engineering.pptx
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
Introduction to Artificial Intelligence ( AI)
Introduction to Artificial Intelligence ( AI)Introduction to Artificial Intelligence ( AI)
Introduction to Artificial Intelligence ( AI)
 
PE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and propertiesPE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and properties
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
 
Post office management system project ..pdf
Post office management system project ..pdfPost office management system project ..pdf
Post office management system project ..pdf
 

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