Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
add a Service Worker to serve /search from cache
For more details about Service Workers, see https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API The new /placeholder.html handler serves the placeholder template with inlined CSS and the %q%/%q_escaped% placeholders which will then be replaced by the service worker javascript code with the current query. instant.js registers the service worker, which will download assets to cache and handle any subsequent requests to /search, getting rid of an entire round trip. fixes #69
- Loading branch information
1 parent
9dbf468
commit 7f31aef
Showing
6 changed files
with
124 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| // vim:ts=4:sw=4:et | ||
| var version = 'v1/'; | ||
|
|
||
| var assets = { | ||
| '/non-critical.min.css': true, | ||
| '/Pics/openlogo-50.svg': true, | ||
| '/Pics/rackspace.svg': true, | ||
| '/jquery.min.js': true, | ||
| '/url-search-params.min.js': true, | ||
| '/loadCSS.min.js': true, | ||
| '/cssrelpreload.min.js': true, | ||
| '/instant.min.js?6': true, | ||
| // Only cache fonts in woff2 format, all browsers which support service | ||
| // workers also support woff2. | ||
| '/Inconsolata.woff2': true, | ||
| '/Roboto-Regular.woff2': true, | ||
| '/Roboto-Bold.woff2': true, | ||
| '/placeholder.html': true | ||
| }; | ||
|
|
||
| self.addEventListener("install", function(event) { | ||
| event.waitUntil( | ||
| caches | ||
| .open(version + 'assets') | ||
| .then(function(cache) { | ||
| return cache.addAll(Object.keys(assets)); | ||
| }) | ||
| ); | ||
| }); | ||
|
|
||
| self.addEventListener("activate", function(event) { | ||
| // The new version of the service worker is activated, superseding any old | ||
| // version. Go through the cache and delete all assets whose key doesn’t | ||
| // start with the current version prefix. | ||
| event.waitUntil( | ||
| caches | ||
| .keys() | ||
| .then(function(keys) { | ||
| return Promise.all( | ||
| keys | ||
| .filter(function(key) { | ||
| return !key.startsWith(version); | ||
| }) | ||
| .map(function(key) { | ||
| return caches['delete'](key); | ||
| }) | ||
| ); | ||
| }) | ||
| ); | ||
| }); | ||
|
|
||
| self.addEventListener("fetch", function(event) { | ||
| if (event.request.method !== 'GET') { | ||
| return; | ||
| } | ||
| var u = new URL(event.request.url); | ||
| if (assets[u.pathname + u.search] === true) { | ||
| event.respondWith(caches.match(event.request).then(function(response) { | ||
| // Defense in depth: in case the cache request fails, fall back to | ||
| // fetching the request. | ||
| return response || fetch(event.request); | ||
| })); | ||
| return; | ||
| } | ||
| if (u.pathname === '/search') { | ||
| event.respondWith(caches.match('/placeholder.html').then(function(response) { | ||
| if (!response) { | ||
| return fetch(event.request); | ||
| } | ||
| // URLSearchParams is not available on all browsers yet. | ||
| var searchParams = u.search.slice(1).split('&'); | ||
| var q; | ||
| var qEscaped; | ||
| for (var i = 0, len = searchParams.length; i < len; i++) { | ||
| if (searchParams[i].indexOf('q=') === 0) { | ||
| qEscaped = searchParams[i].substr('q='.length); | ||
| q = decodeURIComponent(qEscaped.replace(/\+/g, ' ')); | ||
| break; | ||
| } | ||
| } | ||
| if (q === undefined) { | ||
| return response; | ||
| } | ||
|
|
||
| var init = { | ||
| status: response.status, | ||
| statusText: response.statusText, | ||
| headers: {}, | ||
| }; | ||
| response.headers.forEach(function(v, k) { | ||
| init.headers[k] = v; | ||
| }); | ||
| return response.text().then(function(body) { | ||
| var replaced = body.replace(/%q%/g, q); | ||
| replaced = replaced.replace(/%q_escaped%/g, qEscaped); | ||
| return new Response(replaced, init); | ||
| }); | ||
| })); | ||
| return; | ||
| } | ||
| return; | ||
| }); |