Skip to content

Commit 093993e

Browse files
committed
version 0.1.0
refactor service worker to try to get index.html from network first, then fall back to the cached version
1 parent 748d41f commit 093993e

8 files changed

+63
-200
lines changed

.github/ISSUE_TEMPLATE.md

-35
This file was deleted.

.github/PULL_REQUEST_TEMPLATE.md

-14
This file was deleted.

CODE_OF_CONDUCT.md

-74
This file was deleted.

CONTRIBUTING.md

+17-51
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,26 @@
1-
# How to contribute
1+
# How To Contribute
22

3-
## Improve documentation
4-
5-
We are always looking to improve our documentation. If at some moment you are
6-
reading the documentation and something is not clear, or you can't find what you
7-
are looking for, then please open an issue with the repository. This gives us a
8-
chance to answer your question and to improve the documentation if needed.
9-
10-
Pull requests correcting spelling or grammar mistakes are always welcome.
11-
12-
## Found a bug?
13-
14-
Please try to answer at least the following questions when reporting a bug:
15-
16-
- Which version of the project did you use when you noticed the bug?
17-
- How do you reproduce the error condition?
18-
- What happened that you think is a bug?
19-
- What should it do instead?
20-
21-
It would really help the maintainers if you could provide a reduced test case
22-
that reproduces the error condition.
23-
24-
## Have a feature request?
25-
26-
Please provide some thoughful commentary and code samples on what this feature
27-
should do and why it should be added (your use case). The minimal questions you
28-
should answer when submitting a feature request should be:
29-
30-
- What will it allow you to do that you can't do today?
31-
- Why do you need this feature and how will it benefit other users?
32-
- Are there any drawbacks to this feature?
33-
34-
## Submitting a pull-request?
3+
## Installation
354

36-
Here are some things that will increase the chance that your pull-request will
37-
get accepted:
38-
- Did you confirm this fix/feature is something that is needed?
39-
- Did you write tests, preferably in a test driven style?
40-
- Did you add documentation for the changes you made?
41-
- Did you follow our [styleguide](https://github.com/dockyard/styleguides)?
5+
* `git clone <repository-url>`
6+
* `cd ember-service-worker-index-fallback`
7+
* `npm install`
428

43-
If your pull-request addresses an issue then please add the corresponding
44-
issue's number to the description of your pull-request.
9+
## Linting
4510

46-
# How to work with this project locally
11+
* `npm run lint:hbs`
12+
* `npm run lint:js`
13+
* `npm run lint:js -- --fix`
4714

48-
## Installation
49-
50-
First clone this repository:
15+
## Running tests
5116

52-
```sh
53-
git clone https://github.com/DockYard/ember-service-worker-index.git
54-
```
17+
* `ember test` – Runs the test suite on the current Ember version
18+
* `ember test --server` – Runs the test suite in "watch mode"
19+
* `ember try:each` – Runs the test suite against multiple Ember versions
5520

56-
<!-- Add further details on how to install the project here -->
21+
## Running the dummy application
5722

58-
## Running tests
23+
* `ember serve`
24+
* Visit the dummy application at [http://localhost:4200](http://localhost:4200).
5925

60-
<!-- Tell the user how to run the tests of your project -->
26+
For more information on using ember-cli, visit [https://ember-cli.com/](https://ember-cli.com/).

README.md

+31-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1-
# Ember Service Worker Index
1+
ember-service-worker-index-fallback
2+
==============================================================================
3+
[![Greenkeeper badge](https://badges.greenkeeper.io/st-h/ember-service-worker-index-fallback.svg)](https://greenkeeper.io/)
4+
[![Latest NPM release][npm-badge]][npm-badge-url]
5+
[![Code Climate][codeclimate-badge]][codeclimate-badge-url]
6+
[![Ember Observer Score][ember-observer-badge]][ember-observer-badge-url]
7+
[![Dependencies][dependencies-badge]][dependencies-badge-url]
8+
[![Dev Dependencies][devDependencies-badge]][devDependencies-badge-url]
29

3-
_An Ember Service Worker plugin that caches an Ember app's index file_
10+
An Ember Service Worker plugin that serves an Ember app's index file and falls back to a cached version when loading fails
411

512
## F#$& my assets aren't updating in development mode
613

7-
Turn on the "Update on reload" setting in the `Application > Service Workers`
8-
menu in the Chrome devtools.
14+
Turn on the "Update on reload" setting in the `Application > Service Workers` menu in the Chrome devtools.
915

1016
## Installation
1117

@@ -27,7 +33,10 @@ module.exports = function(defaults) {
2733
location: 'app-shell.html',
2834

2935
// time in milliseconds that fetching index.html from network may take before the cached version is served
30-
requestTimeout: 500,
36+
requestTimeoutCached: 500,
37+
38+
// time in milliseconds that fetching index.html from network may take when no cached version is available
39+
requestTimeoutUncached: 60000
3140

3241
// Bypass esw-index and don't serve cached index file for matching URLs
3342
excludeScope: [/\/non-ember-app(\/.*)?$/, /\/another-app(\/.*)?$/],
@@ -50,10 +59,26 @@ module.exports = function(defaults) {
5059

5160
## Authors
5261

53-
* [Marten Schilstra](http://twitter.com/martndemus)
62+
This addon has been forked from DockYards [ember service worker index](https://github.com/DockYard/ember-service-worker-index)
5463
* [st-h](https://github.com/st-h)
64+
* [Marten Schilstra](http://twitter.com/martndemus)
5565

5666
## Versioning
5767

5868
This library follows [Semantic Versioning](http://semver.org)
5969

70+
License
71+
------------------------------------------------------------------------------
72+
73+
This project is licensed under the [MIT License](LICENSE.md).
74+
75+
[npm-badge]: https://img.shields.io/npm/v/ember-service-worker-index-fallback.svg
76+
[npm-badge-url]: https://www.npmjs.com/package/ember-service-worker-index-fallback
77+
[codeclimate-badge]: https://api.codeclimate.com/v1/badges/b62c466a81d28a69abd1/maintainability
78+
[codeclimate-badge-url]: https://codeclimate.com/github/st-h/ember-service-worker-index-fallback/maintainability
79+
[ember-observer-badge]: http://emberobserver.com/badges/ember-service-worker-index-fallback.svg
80+
[ember-observer-badge-url]: http://emberobserver.com/addons/ember-service-worker-index-fallback
81+
[dependencies-badge]: https://img.shields.io/david/st-h/ember-service-worker-index-fallback.svg
82+
[dependencies-badge-url]: https://david-dm.org/st-h/ember-service-worker-index-fallback
83+
[devDependencies-badge]: https://img.shields.io/david/dev/st-h/ember-service-worker-index-fallback.svg
84+
[devDependencies-badge-url]: https://david-dm.org/st-h/ember-service-worker-index-fallback#info=devDependencies

lib/config.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ module.exports = class Config extends Plugin {
2727
let location = options.location || "index.html";
2828
let excludeScope = options.excludeScope || [];
2929
let includeScope = options.includeScope || [];
30-
let requestTimeout = options.requestTimeout || 500;
30+
let requestTimeoutCached = options.requestTimeoutCached || 500;
31+
let requestTimeoutUncached = options.requestTimeoutUncached || 60000;
3132

3233
let fileLocation = location;
3334
if (fileLocation[fileLocation.length - 1] === "/") {
@@ -43,7 +44,8 @@ module.exports = class Config extends Plugin {
4344
module += `export const INDEX_HTML_PATH = '${location}';\n`;
4445
module += `export const INDEX_EXCLUDE_SCOPE = [${excludeScope}];\n`;
4546
module += `export const INDEX_INCLUDE_SCOPE = [${includeScope}];\n`;
46-
module += `export const REQUEST_TIMEOUT = [${requestTimeout}];\n`;
47+
module += `export const REQUEST_TIMEOUT_CACHED = [${requestTimeoutCached}];\n`;
48+
module += `export const REQUEST_TIMEOUT_UNCACHED = [${requestTimeoutUncached}];\n`;
4749
module += `self.INDEX_FILE_HASH = '${hash}';\n`;
4850

4951
fs.writeFileSync(path.join(this.outputPath, "config.js"), module);

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "ember-service-worker-index-fallback",
3-
"version": "0.8.0",
4-
"description": "An Ember Service Worker plugin that caches the index.html file",
3+
"version": "0.1.0",
4+
"description": "An Ember Service Worker plugin that serves an Ember app's index file and falls back to a cached version when loading fails",
55
"directories": {
66
"doc": "doc",
77
"test": "tests"

service-worker/index.js

+9-16
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import {
44
VERSION,
55
INDEX_EXCLUDE_SCOPE,
66
INDEX_INCLUDE_SCOPE,
7-
REQUEST_TIMEOUT
7+
REQUEST_TIMEOUT_CACHED,
8+
REQUEST_TIMEOUT_UNCACHED
89

910
} from 'ember-service-worker-index-fallback/service-worker/config';
1011

@@ -43,27 +44,19 @@ self.addEventListener('fetch', (event) => {
4344
if (!isTests && isGETRequest && isHTMLRequest && isLocal && scopeIncluded && !scopeExcluded) {
4445

4546
event.respondWith(new Promise(function (fulfill, reject) {
46-
fromNetwork(request, REQUEST_TIMEOUT).then((response) => {
47-
fulfill(response);
48-
}, (error) => {
49-
fromCache(request).then((response) => {
50-
if (response) {
51-
fulfill(response);
52-
} else {
53-
reject(response);
54-
}
47+
caches.match(INDEX_HTML_URL, { cacheName: CACHE_NAME }).then((cachedResponse) => {
48+
const timeout = cachedResponse ? REQUEST_TIMEOUT_CACHED : REQUEST_TIMEOUT_UNCACHED;
49+
50+
fromNetwork(request, timeout).then((response) => {
51+
fulfill(response);
5552
}, (error) => {
56-
reject(error);
57-
})
53+
fulfill(cachedResponse);
54+
});
5855
})
5956
}));
6057
}
6158
});
6259

63-
function fromCache() {
64-
return caches.match(INDEX_HTML_URL, { cacheName: CACHE_NAME });
65-
}
66-
6760
function fromNetwork(request, timeout) {
6861
return new Promise(function (fulfill, reject) {
6962

0 commit comments

Comments
 (0)