Skip to content
This repository was archived by the owner on Sep 10, 2022. It is now read-only.

Commit 8b82bcb

Browse files
author
Matt Gaunt
committed
Fixing up sw-precache watch task to update on changes to layouts and views. Adding noscript support and including comment on request animation frame
1 parent 1c5ffa0 commit 8b82bcb

File tree

3 files changed

+57
-48
lines changed

3 files changed

+57
-48
lines changed

gulp-tasks/service-worker.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,32 @@ var packageName = JSON.parse(fs.readFileSync('./package.json', 'utf8')).name;
2525

2626
gulp.task('service-worker:watch', function(cb) {
2727
gulp.watch(GLOBAL.config.src + '/**/*.*', ['service-worker']);
28+
gulp.watch(GLOBAL.config.src + '/../server/layouts/**/*.*',
29+
['service-worker']);
30+
gulp.watch(GLOBAL.config.src + '/../server/views/**/*.*',
31+
['service-worker']);
2832
});
2933

3034
gulp.task('service-worker', function(cb) {
3135
swPrecache.write(path.join(GLOBAL.config.dest, 'sw.js'), {
3236
staticFileGlobs: [
3337
GLOBAL.config.dest + '/**/*.{js,html,css,png,jpg,jpeg,gif,svg}',
34-
GLOBAL.config.dest + '/manifest.json',
38+
GLOBAL.config.dest + '/manifest.json'
3539
],
3640
dynamicUrlToDependencies: {
3741
'/app-shell': ['server/layouts/app-shell.handlebars'],
38-
'/partials/': ['server/layouts/partial.handlebars', 'server/views/index.handlebars'],
39-
'/partials/url-1': ['server/layouts/partial.handlebars', 'server/views/url-1.handlebars'],
40-
'/partials/url-2': ['server/layouts/partial.handlebars', 'server/views/url-2.handlebars'],
42+
'/partials/': [
43+
'server/layouts/partial.handlebars',
44+
'server/views/index.handlebars'
45+
],
46+
'/partials/url-1': [
47+
'server/layouts/partial.handlebars',
48+
'server/views/url-1.handlebars'
49+
],
50+
'/partials/url-2': [
51+
'server/layouts/partial.handlebars',
52+
'server/views/url-2.handlebars'
53+
]
4154
},
4255
stripPrefix: GLOBAL.config.dest,
4356
navigateFallback: '/app-shell',

server/layouts/app-shell.handlebars

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -118,19 +118,10 @@ limitations under the License.
118118
{{~/each}}
119119
];
120120
121-
// <3 to Paul Irish
122-
// http://www.paulirish.com/2011/requestanimationframe-for-smart-animating/
123-
var requestAnimFrame = (function(){
124-
return window.requestAnimationFrame ||
125-
window.webkitRequestAnimationFrame ||
126-
window.mozRequestAnimationFrame ||
127-
function( callback ){
128-
window.setTimeout(callback, 1000 / 60);
129-
};
130-
})();
131-
132121
// Asynchronously load CSS with a Request Animation Frame
133-
requestAnimFrame(function() {
122+
// For older browser support see this for a simple shim
123+
// http://www.paulirish.com/2011/requestanimationframe-for-smart-animating/
124+
window.requestAnimationFrame(function() {
134125
var elementToInsertLinkBefore =
135126
document.getElementsByTagName('script')[0];
136127
for (var i = 0; i < remoteStyles.length; i++) {
@@ -144,8 +135,15 @@ limitations under the License.
144135
}
145136
});
146137
</script>
138+
139+
<!-- In case the browser has JS disabled -->
140+
<noscript>
141+
{{#each remoteStyles}}
142+
<link href="{{this}}" rel="stylesheet" property="stylesheet" media="all">
143+
{{~/each}}
144+
</noscript>
147145
{{~/if}}
148-
146+
149147
{{#each remoteScripts}}
150148
<script src="{{this}}" async></script>
151149
{{~/each}}

server/layouts/default.handlebars

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -63,38 +63,36 @@ limitations under the License.
6363

6464
{{#if remoteStyles}}
6565
<script>
66-
var remoteStyles = [
67-
{{#each remoteStyles}}
68-
'{{this}}',
69-
{{~/each}}
70-
];
71-
72-
// <3 to Paul Irish
73-
// http://www.paulirish.com/2011/requestanimationframe-for-smart-animating/
74-
var requestAnimFrame = (function(){
75-
return window.requestAnimationFrame ||
76-
window.webkitRequestAnimationFrame ||
77-
window.mozRequestAnimationFrame ||
78-
function( callback ){
79-
window.setTimeout(callback, 1000 / 60);
80-
};
81-
})();
82-
83-
// Asynchronously load CSS with a Request Animation Frame
84-
requestAnimFrame(function() {
85-
var elementToInsertLinkBefore =
86-
document.getElementsByTagName('script')[0];
87-
for (var i = 0; i < remoteStyles.length; i++) {
88-
var linkElement = document.createElement('link');
89-
linkElement.rel = 'stylesheet';
90-
linkElement.media = 'all';
91-
linkElement.href = remoteStyles[i];
92-
93-
elementToInsertLinkBefore.parentNode.insertBefore(linkElement,
94-
elementToInsertLinkBefore);
95-
}
96-
});
66+
var remoteStyles = [
67+
{{#each remoteStyles}}
68+
'{{this}}',
69+
{{~/each}}
70+
];
71+
72+
// Asynchronously load CSS with a Request Animation Frame
73+
// For older browser support see this for a simple shim
74+
// http://www.paulirish.com/2011/requestanimationframe-for-smart-animating/
75+
window.requestAnimationFrame(function() {
76+
var elementToInsertLinkBefore =
77+
document.getElementsByTagName('script')[0];
78+
for (var i = 0; i < remoteStyles.length; i++) {
79+
var linkElement = document.createElement('link');
80+
linkElement.rel = 'stylesheet';
81+
linkElement.media = 'all';
82+
linkElement.href = remoteStyles[i];
83+
84+
elementToInsertLinkBefore.parentNode.insertBefore(linkElement,
85+
elementToInsertLinkBefore);
86+
}
87+
});
9788
</script>
89+
90+
<!-- In case the browser has JS disabled -->
91+
<noscript>
92+
{{#each remoteStyles}}
93+
<link href="{{this}}" rel="stylesheet" property="stylesheet" media="all">
94+
{{~/each}}
95+
</noscript>
9896
{{~/if}}
9997

10098
{{#each remoteScripts}}

0 commit comments

Comments
 (0)