From 553e91aa20cec20e701b523ca79f8ed2a116b8e1 Mon Sep 17 00:00:00 2001 From: Tunghsiao Liu Date: Fri, 13 Jan 2017 18:28:01 +0800 Subject: [PATCH] feat: init Service Worker support --- Gruntfile.coffee | 38 +++++++++++++++++++ .../themes/curtana/includes/footer.html | 19 ++++++++-- _config.init.yml | 5 +++ _config.yml | 5 +++ package.json | 1 + s3_website.example.yml | 4 +- 6 files changed, 68 insertions(+), 4 deletions(-) diff --git a/Gruntfile.coffee b/Gruntfile.coffee index dbfea4730..9f6d016b4 100644 --- a/Gruntfile.coffee +++ b/Gruntfile.coffee @@ -1,4 +1,8 @@ "use strict" + +path = require("path") +swPrecache = require("sw-precache") + module.exports = (grunt) -> # Load all grunt tasks @@ -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: @@ -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 @@ -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' @@ -690,6 +727,7 @@ module.exports = (grunt) -> "uncss_inline" "cacheBust" "concurrent:dist" + "swPrecache" "cleanempty" ] diff --git a/_app/_includes/themes/curtana/includes/footer.html b/_app/_includes/themes/curtana/includes/footer.html index 95ad53819..4fe1e465e 100644 --- a/_app/_includes/themes/curtana/includes/footer.html +++ b/_app/_includes/themes/curtana/includes/footer.html @@ -19,7 +19,10 @@ {% endif %} - + + + + {% if page.plugin == 'lightense' %} {% endif %} - - + +{% if site.sw == true and site.dev == false %} + +{% endif %} diff --git a/_config.init.yml b/_config.init.yml index e11effeec..e8cfee3ef 100644 --- a/_config.init.yml +++ b/_config.init.yml @@ -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 diff --git a/_config.yml b/_config.yml index 2b2a320c3..f7d5e6540 100644 --- a/_config.yml +++ b/_config.yml @@ -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 diff --git a/package.json b/package.json index b238c888a..1705cc47f 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/s3_website.example.yml b/s3_website.example.yml index 8e9b41c22..be7d8910a 100644 --- a/s3_website.example.yml +++ b/s3_website.example.yml @@ -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