Skip to content

Commit

Permalink
Add serviceworker (ampproject#1371)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbenz authored Feb 6, 2019
1 parent c930ccf commit 0d51d4e
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 39 deletions.
4 changes: 2 additions & 2 deletions boilerplate/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,8 @@ <h3>Bookend Sample</h3>
</script>
</amp-analytics>
<amp-install-serviceworker
src="/sw.js"
data-iframe-src="https://amp.dev/sw.html"
src="/serviceworker.js"
data-iframe-src="https://amp.dev"
layout="nodisplay">
</amp-install-serviceworker>
</body>
Expand Down
1 change: 1 addition & 0 deletions frontend/templates/layouts/default.j2
Original file line number Diff line number Diff line change
Expand Up @@ -146,5 +146,6 @@
}
</script>
</amp-analytics>
<amp-install-serviceworker src="/serviceworker.js" data-iframe-src="{{ podspec.base_urls.platform }}" layout="nodisplay"></amp-install-serviceworker>
</body>
</html>
7 changes: 7 additions & 0 deletions pages/content/amp-dev/offline.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
$title: Offline
---

<main class="ap--main ap-t-home">
<h1>You're offline</h1>
</main>
11 changes: 11 additions & 0 deletions pages/static/serviceworker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
importScripts('https://cdn.ampproject.org/sw/amp-sw.js');
AMP_SW.init({
assetCachingOptions: [{
regexp: /\.(png|jpg|woff2|woff|css|js)/,
cachingStrategy: 'CACHE_FIRST',
}],
offlinePageOptions: {
url: '/offline.html',
assets: [],
},
});
6 changes: 4 additions & 2 deletions platform/lib/platform.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const routers = {
'whoAmI': require('./routers/whoAmI.js'),
'pages': require('./routers/pages.js'),
'examples': require('./routers/examples.js'),
'static': require('./routers/static.js'),
'playground': require('../../playground/backend/'),
'boilerplate': require('../../boilerplate/backend/'),
};
Expand Down Expand Up @@ -93,8 +94,9 @@ class Platform {
_registerRouters() {
this.server.use('/who-am-i', routers.whoAmI);
this.server.use(routers.examples);
this.server.use('/playground', routers.playground);
this.server.use('/boilerplate', routers.boilerplate);
this.server.use(routers.static);
this.server.use('/playground', routers.playground);
this.server.use('/boilerplate', routers.boilerplate);
// Register the following router at last as it works as a catch-all
this.server.use(routers.pages);
}
Expand Down
32 changes: 32 additions & 0 deletions platform/lib/routers/static.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Copyright 2018 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

const express = require('express');
const path = require('path');
const {setNoCache} = require('../utils/cacheHelpers.js');

// eslint-disable-next-line new-cap
const staticRouter = express.Router();

staticRouter.get('/serviceworker.js', (request, response) => {
setNoCache(response);
response.status(200)
.sendFile(path.join(__dirname, '../../../pages/static/serviceworker.js'));
});

module.exports = staticRouter;
2 changes: 1 addition & 1 deletion playground/src/service-worker/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ if ('requestIdleCallback' in window && !window.parent && navigator.serviceWorker
window.onload = () => {
window.requestIdleCallback(() => {
console.log('registering service worker');
navigator.serviceWorker.register('/sw.js')
navigator.serviceWorker.register('/serviceworker.js')
.catch((err) => {
console.error('Unable to register service worker.', err);
});
Expand Down
34 changes: 0 additions & 34 deletions playground/src/sw.js

This file was deleted.

0 comments on commit 0d51d4e

Please sign in to comment.