Skip to content
This repository has been archived by the owner on Apr 1, 2019. It is now read-only.

Commit

Permalink
Move service worker to src/js/ folder
Browse files Browse the repository at this point in the history
  • Loading branch information
marcoscaceres committed Nov 2, 2015
1 parent b04322c commit 236eb61
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 105 deletions.
7 changes: 5 additions & 2 deletions bin/generate-offline-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@ function processResults(results) {
const paths = results
.reduce((current, next) => next.concat(current), [])
.split("\n")
// filter out empties
.filter(item => item)
.map(item => item.replace(/^src\//, "/"))
//Service workers shouldn't be cached, so filter out
.filter(item => !item.includes("sw.js"))
.map(item => item.replace(/^src\//, "../"))
.sort();
return JSON.stringify(paths.concat(["/"]), null, 2);
return JSON.stringify(paths.concat(["../"]), null, 2);
}

function writeFile(template) {
Expand Down
53 changes: 4 additions & 49 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module.exports = function(config) {

// list of files / patterns to load in the browser
files: [{
pattern: "src/sw.js",
pattern: "src/js/sw.js",
served: true,
watched: true,
included: false,
Expand All @@ -27,7 +27,8 @@ module.exports = function(config) {
"src/js/blockedLinks.js",
"src/js/rectangle.js",
"src/test/fixtures/**/*.html",
"src/test/**/*.js", {
"src/test/**/*.js",
{
pattern: "src/css/**/*.*",
watched: true,
served: true,
Expand All @@ -53,56 +54,10 @@ module.exports = function(config) {
customHeaders: [{
match: ".*",
name: "Service-Worker-Allowed",
value: "/"
value: "/base/src/js/"
},
],

proxies: {
'/sw.js': 'http://localhost:9876/base/src/sw.js',
"/css/contentSearchUI.css": "http://localhost:9876/base/src/css/contentSearchUI.css",
"/css/images/close.png": "http://localhost:9876/base/src/css/images/close.png",
"/css/images/controls.svg": "http://localhost:9876/base/src/css/images/controls.svg",
"/css/images/defaultFavicon.png": "http://localhost:9876/base/src/css/images/defaultFavicon.png",
"/css/images/history-icon.svg": "http://localhost:9876/base/src/css/images/history-icon.svg",
"/css/images/search-engine-placeholder.png": "http://localhost:9876/base/src/css/images/search-engine-placeholder.png",
"/css/images/search-arrow-go.svg": "http://localhost:9876/base/src/css/images/search-arrow-go.svg",
"/css/images/search-indicator-magnifying-glass.svg": "http://localhost:9876/base/src/css/images/search-indicator-magnifying-glass.svg",
"/css/images/shared-menu-check.svg": "http://localhost:9876/base/src/css/images/shared-menu-check.svg",
"/css/images/whimsycorn.png": "http://localhost:9876/base/src/css/images/whimsycorn.png",
"/css/newTab.css": "http://localhost:9876/base/src/css/newTab.css",

"/index.html": "http://localhost:9876/base/src/index.html",
"/js/blockedLinks.js": "http://localhost:9876/base/src/js/blockedLinks.js",
"/js/cells.js": "http://localhost:9876/base/src/js/cells.js",
"/js/contentSearchUI.js": "http://localhost:9876/base/src/js/contentSearchUI.js",
"/js/customize.js": "http://localhost:9876/base/src/js/customize.js",
"/js/drag.js": "http://localhost:9876/base/src/js/drag.js",
"/js/dragDataHelper.js": "http://localhost:9876/base/src/js/dragDataHelper.js",
"/js/drop.js": "http://localhost:9876/base/src/js/drop.js",
"/js/dropPreview.js": "http://localhost:9876/base/src/js/dropPreview.js",
"/js/dropTargetShim.js": "http://localhost:9876/base/src/js/dropTargetShim.js",
"/js/grid.js": "http://localhost:9876/base/src/js/grid.js",
"/js/intro.js": "http://localhost:9876/base/src/js/intro.js",
"/js/lib/async.js": "http://localhost:9876/base/src/js/lib/async.js",
"/js/lib/cachetasks.js": "http://localhost:9876/base/src/js/lib/cachetasks.js",
"/js/mainSiteURLs.json": "http://localhost:9876/base/src/js/mainSiteURLs.json",
"/js/newTab.js": "http://localhost:9876/base/src/js/newTab.js",
"/js/page.js": "http://localhost:9876/base/src/js/page.js",
"/js/pinnedLinks.js": "http://localhost:9876/base/src/js/pinnedLinks.js",
"/js/rectangle.js": "http://localhost:9876/base/src/js/rectangle.js",
"/js/search.js": "http://localhost:9876/base/src/js/search.js",
"/js/sites.js": "http://localhost:9876/base/src/js/sites.js",
"/js/transformations.js": "http://localhost:9876/base/src/js/transformations.js",
"/js/undo.js": "http://localhost:9876/base/src/js/undo.js",
"/js/updater.js": "http://localhost:9876/base/src/js/updater.js",
"/js/userDatabase.js": "http://localhost:9876/base/src/js/userDatabase.js",
"/locale/newTab.js": "http://localhost:9876/base/src/locale/newTab.js",
"/test/rectangle.js": "http://localhost:9876/base/src/test/rectangle.js",
"/test/script_test_example.js": "http://localhost:9876/base/src/test/script_test_example.js",
"/test/sw_spec.js": "http://localhost:9876/base/src/test/sw_spec.js",
"/test/test.js": "http://localhost:9876/base/src/test/test.js",
},

// list of files to exclude
exclude: [
"**/*.swp",
Expand Down
6 changes: 3 additions & 3 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
</div>
<button id="newtab-customize-button" title="Customize your New Tab page"></button>
</div>
<script type="text/javascript;version=1.8" src="js/lib/async.js"></script>
<script src="js/lib/async.js"></script>
<script type="text/javascript;version=1.8" src="js/lib/cachetasks.js"></script>
<script type="text/javascript;version=1.8" src="js/intro.js"></script>
<script type="text/javascript;version=1.8" src="locale/newTab.js"></script>
Expand All @@ -114,10 +114,10 @@
<script type="text/javascript;version=1.8" src="js/newTab.js"></script>
<script type="text/javascript;version=1.8" src="js/updater.js"></script>
<script type="text/javascript;version=1.8" src="js/undo.js"></script>
<script type="text/javascript;version=1.8">
<script>
document.addEventListener("DOMContentLoaded", async(function*() {
try {
yield navigator.serviceWorker.register("sw.js", {scope: "./"});
yield navigator.serviceWorker.register("js/sw.js", {scope: "./"});
} catch (err) {
console.error("ServiceWorker registration failed:", err);
}
Expand Down
74 changes: 37 additions & 37 deletions src/js/mainSiteURLs.json
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
[
"/css/contentSearchUI.css",
"/css/images/close.png",
"/css/images/controls.svg",
"/css/images/defaultFavicon.png",
"/css/images/history-icon.svg",
"/css/images/search-arrow-go.svg",
"/css/images/search-engine-placeholder.png",
"/css/images/search-indicator-magnifying-glass.svg",
"/css/images/shared-menu-check.svg",
"/css/images/whimsycorn.png",
"/css/newTab.css",
"/js/blockedLinks.js",
"/js/cells.js",
"/js/contentSearchUI.js",
"/js/customize.js",
"/js/drag.js",
"/js/dragDataHelper.js",
"/js/drop.js",
"/js/dropPreview.js",
"/js/dropTargetShim.js",
"/js/grid.js",
"/js/intro.js",
"/js/lib/async.js",
"/js/lib/cachetasks.js",
"/js/lib/swMessage.js",
"/js/newTab.js",
"/js/page.js",
"/js/pinnedLinks.js",
"/js/rectangle.js",
"/js/search.js",
"/js/sites.js",
"/js/transformations.js",
"/js/undo.js",
"/js/updater.js",
"/js/userDatabase.js",
"/locale/newTab.js",
"/"
"../css/contentSearchUI.css",
"../css/images/close.png",
"../css/images/controls.svg",
"../css/images/defaultFavicon.png",
"../css/images/history-icon.svg",
"../css/images/search-arrow-go.svg",
"../css/images/search-engine-placeholder.png",
"../css/images/search-indicator-magnifying-glass.svg",
"../css/images/shared-menu-check.svg",
"../css/images/whimsycorn.png",
"../css/newTab.css",
"../js/blockedLinks.js",
"../js/cells.js",
"../js/contentSearchUI.js",
"../js/customize.js",
"../js/drag.js",
"../js/dragDataHelper.js",
"../js/drop.js",
"../js/dropPreview.js",
"../js/dropTargetShim.js",
"../js/grid.js",
"../js/intro.js",
"../js/lib/async.js",
"../js/lib/cachetasks.js",
"../js/lib/swMessage.js",
"../js/newTab.js",
"../js/page.js",
"../js/pinnedLinks.js",
"../js/rectangle.js",
"../js/search.js",
"../js/sites.js",
"../js/transformations.js",
"../js/undo.js",
"../js/updater.js",
"../js/userDatabase.js",
"../locale/newTab.js",
"../"
]
8 changes: 4 additions & 4 deletions src/sw.js → src/js/sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
/*jshint worker:true*/
/*globals async, CacheTasks, Response, fetch */
"use strict";
importScripts("js/lib/async.js"); // imports async()
importScripts("js/lib/cachetasks.js"); // imports CacheTasks
importScripts("lib/async.js"); // imports async()
importScripts("lib/cachetasks.js"); // imports CacheTasks

const SWTasks = {
/**
Expand All @@ -20,9 +20,9 @@ const SWTasks = {
"pagethumbs_cache", // User's history tiles
"ads_cache", // Advertisement tiles
]);
var request = yield fetch("js/mainSiteURLs.json");
var request = yield fetch("mainSiteURLs.json");
var mainSiteURLs = (yield request.json())
.map(path => `${location.origin}${path}`);
.map(path => new URL(path, self.location));
yield CacheTasks.addAll(mainSiteURLs, "skeleton_cache");
}, this);
},
Expand Down
10 changes: 5 additions & 5 deletions src/test/lib/cachetasks_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,25 +53,25 @@ describe("CacheTasks", function() {

describe("addAll() method", () => {
it("should allow populating the cache.", async(function*() {
var result = yield CacheTasks.addAll(["js/lib/async.js"], CACHENAME);
var result = yield CacheTasks.addAll(["base/src/js/lib/async.js"], CACHENAME);
expect(result).to.be.true;
}));
});

describe("match() method", () => {
it("should respond when given a Request object.", async(function*() {
var request = new Request("js/lib/async.js");
var request = new Request("base/src/js/lib/async.js");
var result = yield CacheTasks.match(request, CACHENAME);
expect(result.url).to.equal(request.url);
}));
it("should respond when given a string.", async(function*() {
var request = "js/lib/async.js";
var request = "base/src/js/lib/async.js";
var result = yield CacheTasks.match(request, CACHENAME);
var expectedURL = new URL(request, window.location).href;
expect(result.url).to.equal(expectedURL);
}));
it("should respond when given a URL.", async(function*() {
var request = new URL("js/lib/async.js", window.location);
var request = new URL("base/src/js/lib/async.js", window.location);
var result = yield CacheTasks.match(request, CACHENAME);
expect(result.url).to.equal(request.href);
}));
Expand All @@ -83,7 +83,7 @@ describe("CacheTasks", function() {

describe("has() method", () => {
it("should store entries in the cache.", async(function*() {
var result = yield CacheTasks.has("js/lib/async.js", CACHENAME);
var result = yield CacheTasks.has("base/src/js/lib/async.js", CACHENAME);
expect(result).to.be.true;
result = yield CacheTasks.has("/not/stored", CACHENAME);
expect(result).to.be.false;
Expand Down
11 changes: 6 additions & 5 deletions src/test/sw_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
/* globals expect, async, it*/
"use strict";
describe("Service worker registration", function() {
it("should correctly register and activate the Service Worker.", async(function*() {
// force reload on first load.
navigator.serviceWorker.register("sw.js");
var sw = (yield navigator.serviceWorker.ready).active;
expect(sw).to.be.ok;
it("should install the Service Worker.", async(function*() {
var swr = yield navigator.serviceWorker.register("/base/src/js/sw.js");
expect(swr).to.be.ok;
}));
it("should eventually become ready.", () => {
expect(navigator.serviceWorker.ready).to.eventually.be.fulfilled;
});
});

0 comments on commit 236eb61

Please sign in to comment.