Skip to content

Commit

Permalink
feat: init Service Worker support
Browse files Browse the repository at this point in the history
  • Loading branch information
sparanoid committed Jan 13, 2017
1 parent 0b5759b commit 553e91a
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 4 deletions.
38 changes: 38 additions & 0 deletions Gruntfile.coffee
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
"use strict"

path = require("path")
swPrecache = require("sw-precache")

module.exports = (grunt) ->

# Load all grunt tasks
Expand All @@ -15,6 +19,21 @@ module.exports = (grunt) ->
# Get deploy target, see `_deploy.yml` for more info
deploy_env = grunt.option("env") or "default"

writeServiceWorkerFile = (cacheId, rootDir, destDir, handleFetch, callback) ->
config =
cacheId: cacheId
handleFetch: handleFetch
logger: grunt.log.writeln
staticFileGlobs: [
rootDir + "/**/css/**.css"
rootDir + "/**/**.html"
rootDir + "/**/img/**.*"
rootDir + "/**/js/**.js"
]
stripPrefix: rootDir
verbose: true
swPrecache.write path.join(destDir, "service-worker.js"), config, callback

# Project configurations
grunt.initConfig
config:
Expand All @@ -41,6 +60,13 @@ module.exports = (grunt) ->
new_name: grunt.option("theme") or "<%= amsf.theme.current %>"
new_url: grunt.option("url") or "<%= amsf.theme.current_url %>"

swPrecache:
dev:
handleFetch: true
cacheId: "<%= config.pkg.name %>"
rootDir: "<%= config.dist %>"
destDir: "<%= config.dist %><%= config.base %>"

coffeelint:
options:
indentation: 2
Expand Down Expand Up @@ -583,6 +609,17 @@ module.exports = (grunt) ->
push: false

# Custom tasks
grunt.registerMultiTask "swPrecache", ->
done = @async()
cacheId = @data.cacheId
rootDir = @data.rootDir
destDir = @data.destDir
handleFetch = @data.handleFetch
writeServiceWorkerFile cacheId, rootDir, destDir, handleFetch, (error) ->
if error
grunt.fail.warn error
done()

grunt.registerTask "amsf-func-mkdir", "Initialize AMSF working directory", ->
grunt.file.mkdir '.amsf-cache'

Expand Down Expand Up @@ -690,6 +727,7 @@ module.exports = (grunt) ->
"uncss_inline"
"cacheBust"
"concurrent:dist"
"swPrecache"
"cleanempty"
]

Expand Down
19 changes: 16 additions & 3 deletions _app/_includes/themes/curtana/includes/footer.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
</footer>
{% endif %}

<!-- The following code is an example use of including external JavaScript libraries -->
<!-- Theme scripts -->
<script src="{{ '/js/app.js?assets-inline' | prepend: amsf_theme_assets }}"></script>

<!-- Lightense Images -->
{% if page.plugin == 'lightense' %}
<script src="{{ '/js/lightense.js?assets-inline' | prepend: amsf_theme_assets }}"></script>
<script>
Expand All @@ -30,8 +33,18 @@
</script>
{% endif %}

<!-- Theme scripts -->
<script src="{{ '/js/app.js?assets-inline' | prepend: amsf_theme_assets }}"></script>
<!-- Service Worker -->
{% if site.sw == true and site.dev == false %}
<script>
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('{{ "/service-worker.js" | prepend: site.base }}').then(function (registration) {
console.log('ServiceWorker registration successful with scope: ', registration.scope);
}).catch(function(err) {
console.log('ServiceWorker registration failed: ', err);
});
}
</script>
{% endif %}

<!-- User custom scripts -->
<script src="{{ '/js/user.js?assets-inline' | prepend: amsf_user_assets }}"></script>
Expand Down
5 changes: 5 additions & 0 deletions _config.init.yml
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ download: https://d1bmx7s6749k4k.cloudfront.net
# Default #ff00b4
primary_color: '#ff00b4'

# Service Worker Precache support
# Service Worker does not work on HTTP
# Default true
sw: true

# Google Analytics tracking code
google_analytics:
enabled: false
Expand Down
5 changes: 5 additions & 0 deletions _config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ download: https://d1bmx7s6749k4k.cloudfront.net
# Default #ff00b4
primary_color: '#ff00b4'

# Service Worker Precache support
# Service Worker does not work on HTTP
# Default true
sw: true

# Google Analytics tracking code
google_analytics:
enabled: false
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"grunt-uncss-inline": "^1.0.5",
"grunt-xmlmin": "^0.1.8",
"jit-grunt": "^0.10.0",
"sw-precache": "^4.3.0",
"time-grunt": "^1.4.0"
}
}
4 changes: 3 additions & 1 deletion s3_website.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ site: .
cache_control:
"*.json": no-cache, no-store
max_age:
"*.xml": 15
"*/service-worker.js": 60
"*.html": 1800
"*.xml": 1800
"*.css": 2592000
"*.js": 2592000
"*": 86400
Expand Down

0 comments on commit 553e91a

Please sign in to comment.