The Fetch API is a modern replacement of the XMLHTTPRequest object. It is based on promises and makes making AJAX/API calls easier to manage and code.
This slide deck is a quick introduction to the API.
5. FETCH
Replaces XMLHttpRequest
Uses Promises
Polyfil Available for Legacy Browsers
Full Support for Modern Browsers
IE 11 & Old Android Need Polyfil
Headers, Request, Response Objects
Build Web Sites Better Than The
Facebook App
5
6. FETCH
It also provides a global fetch() method that provides an easy, logical
way to fetch resources asynchronously across the network.
Build Web Sites Better Than The
Facebook App
6
Fetch API provides a JavaScript interface for accessing and manipulating
parts of the HTTP pipeline, such as requests and responses.
7. if (typeof fetch === "undefined" ||
fetch.toString().indexOf("[native code]") === -1) {
scripts.unshift("js/libs/fetch.js");
}
Build Web Sites Better Than The
Facebook App
7
8. var myImage = document.querySelector('img');
fetch('flowers.jpg')
.then(function(response) {
return response.blob();
})
.then(function(myBlob) {
var objectURL = URL.createObjectURL(myBlob);
myImage.src = objectURL;
});
Build Web Sites Better Than The
Facebook App
8
9. SUPPLY REQUEST OPTIONS
method
headers
body
mode
credentials
cache
redirect
referrer
referrerPolicy
integrity
Build Web Sites Better Than The
Facebook App
9
10. var myHeaders = new Headers();
var myInit = { method: 'GET',
headers: myHeaders,
mode: 'cors',
cache: 'default' };
fetch('flowers.jpg',myInit)
.then(function(response) {
return response.blob();
})
.then(function(myBlob) {
var objectURL = URL.createObjectURL(myBlob);
myImage.src = objectURL;
});
Build Web Sites Better Than The
Facebook App
10
11. VERIFY SUCCESSFUL RESPONSE
Will Reject With a TypeError For Network Error
404 is Not an Error
Check Response.OK
Build Web Sites Better Than The
Facebook App
11
16. BODY OBJECT EXTRACTION METHODS
arrayBuffer()
blob()
json()
text()
formData()
Build Web Sites Better Than The
Facebook App
16
17. var form = new
FormData(document.getElementById('login-form'));
fetch("/login", {
method: "POST",
body: form
});
Build Web Sites Better Than The
Facebook App
17
21. LIFE CYCLE
Lives Separate From Page
Must Register Service Worker
The Service Worker is Installed
It is not Immediately Active
Can be Forced Active Upon Install
Activated After All Current References Terminated
Now Controls All New Instances of Site
Presentation Title Can Be Placed Here 21
22. LIFE CYCLE
Presentation Title Can Be Placed Here 22
Client
Register
SW
Wait For Instance Close
SW
Install
SW
Activated
26. REGISTRATION
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/sw.js')
.then(function(registration) {
// Registration was successful
})
.catch(function(err) {
// registration failed :(
});
}
Presentation Title Can Be Placed Here 26
35. SERVICE WORKER
Presentation Title Can Be Placed Here 35
Service worker is a programmable
network proxy, allowing you to control
how network requests from your page
are handled.
It's terminated when not in use, and
restarted when it's next needed
Persistence Done With IndexDB
Can’t Communicate with DOM Directly
36. WHAT IS A SERVICE WORKER
A Proxy Server
Between the Browser
and Network
44. SERVICE WORKER RESOURCES
Presentation Title Can Be Placed Here 44
Is Service Worker Ready?
https://jakearchibald.github.io/isserviceworkerready/
Service Worker Cookbook
https://serviceworke.rs/
50. SERVICE WORKER CACHE
Presentation Title Can Be Placed Here 50
Persist Files with Response Headers
Limited by Device Resources
Available Online & Offline
51. self.addEventListener('install', function (e) {
e.waitUntil(
caches.open(cacheName).then(function (cache) {
return cache.addAll(filesToCache)
.catch(function (error) {
console.log(error);
});
})
);
});
Presentation Title Can Be Placed Here 51
52. Presentation Title Can Be Placed Here 52
self.addEventListener('fetch', function (event) {
//intercept fetch request (any request from the UI thread for a file or API) and return from cache or get from
server & cache it
event.respondWith(
caches.match(event.request).then(function (resp) {
return resp || fetchAsset(event);
})
);
});
67. self.addEventListener('activate', function(event) {
event.waitUntil( caches.keys()
.then(function(cacheNames) {
return Promise.all(
cacheNames.filter(function(cacheName) {
// Return true if you want to remove this cache,
// but remember that caches are shared across
// the whole origin })
.map(function(cacheName) {
return caches.delete(cacheName);
})
);
})
);
});
94. SERVICE WORKER TOOLS
Presentation Title Can Be Placed Here 94
sw_precache
A node module to generate service worker code that will precache
specific resources so they work offline.
https://github.com/googlechrome/sw-precache
sw_toolbox
A collection of service worker tools for offlining runtime requests
https://github.com/GoogleChrome/sw-toolbox
98. INCREASED ENGAGEMENT
72% increase in time spent for users visiting via a push
notification
26% increase in average spend per visit by members
arriving via a push notification
+50% repeat visits within 3 month
Note:
To Change the Background Picture Follow the steps below
Mouse Right button click>Format Background>Select Picture or Texture File>Click “File” button>Browse and select the image from your computer>Click Insert
That’s it. You are Done !!!
Note:
To Change the Background Picture Follow the steps below
Mouse Right button click>Format Background>Select Picture or Texture File>Click “File” button>Browse and select the image from your computer>Click Insert
That’s it. You are Done !!!
Note:
To Change the Background Picture Follow the steps below
Mouse Right button click>Format Background>Select Picture or Texture File>Click “File” button>Browse and select the image from your computer>Click Insert
That’s it. You are Done !!!
Note:
To Change the Background Picture Follow the steps below
Mouse Right button click>Format Background>Select Picture or Texture File>Click “File” button>Browse and select the image from your computer>Click Insert
That’s it. You are Done !!!
Note:
To Change the Background Picture Follow the steps below
Mouse Right button click>Format Background>Select Picture or Texture File>Click “File” button>Browse and select the image from your computer>Click Insert
That’s it. You are Done !!!
Note:
Picture size will be 3” X 2.5”
If picture is not exist, To insert the picture
click on the icon of Picture
you will automatically get the option to browse and select picture
If any picture is exist,
Take the mouse over the picture
Right button click of the mouse
Select Change picture
Now browse and select the right picture you want