Skip to content

Commit

Permalink
version 0.1.0
Browse files Browse the repository at this point in the history
refactor service worker to try to get index.html from network first, then fall back to the cached version
  • Loading branch information
st-h committed May 19, 2019
1 parent 748d41f commit 093993e
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 200 deletions.
35 changes: 0 additions & 35 deletions .github/ISSUE_TEMPLATE.md

This file was deleted.

14 changes: 0 additions & 14 deletions .github/PULL_REQUEST_TEMPLATE.md

This file was deleted.

74 changes: 0 additions & 74 deletions CODE_OF_CONDUCT.md

This file was deleted.

68 changes: 17 additions & 51 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,60 +1,26 @@
# How to contribute
# How To Contribute

## Improve documentation

We are always looking to improve our documentation. If at some moment you are
reading the documentation and something is not clear, or you can't find what you
are looking for, then please open an issue with the repository. This gives us a
chance to answer your question and to improve the documentation if needed.

Pull requests correcting spelling or grammar mistakes are always welcome.

## Found a bug?

Please try to answer at least the following questions when reporting a bug:

- Which version of the project did you use when you noticed the bug?
- How do you reproduce the error condition?
- What happened that you think is a bug?
- What should it do instead?

It would really help the maintainers if you could provide a reduced test case
that reproduces the error condition.

## Have a feature request?

Please provide some thoughful commentary and code samples on what this feature
should do and why it should be added (your use case). The minimal questions you
should answer when submitting a feature request should be:

- What will it allow you to do that you can't do today?
- Why do you need this feature and how will it benefit other users?
- Are there any drawbacks to this feature?

## Submitting a pull-request?
## Installation

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

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

# How to work with this project locally
* `npm run lint:hbs`
* `npm run lint:js`
* `npm run lint:js -- --fix`

## Installation

First clone this repository:
## Running tests

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

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

## Running tests
* `ember serve`
* Visit the dummy application at [http://localhost:4200](http://localhost:4200).

<!-- Tell the user how to run the tests of your project -->
For more information on using ember-cli, visit [https://ember-cli.com/](https://ember-cli.com/).
37 changes: 31 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
# Ember Service Worker Index
ember-service-worker-index-fallback
==============================================================================
[![Greenkeeper badge](https://badges.greenkeeper.io/st-h/ember-service-worker-index-fallback.svg)](https://greenkeeper.io/)
[![Latest NPM release][npm-badge]][npm-badge-url]
[![Code Climate][codeclimate-badge]][codeclimate-badge-url]
[![Ember Observer Score][ember-observer-badge]][ember-observer-badge-url]
[![Dependencies][dependencies-badge]][dependencies-badge-url]
[![Dev Dependencies][devDependencies-badge]][devDependencies-badge-url]

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

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

Turn on the "Update on reload" setting in the `Application > Service Workers`
menu in the Chrome devtools.
Turn on the "Update on reload" setting in the `Application > Service Workers` menu in the Chrome devtools.

## Installation

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

// time in milliseconds that fetching index.html from network may take before the cached version is served
requestTimeout: 500,
requestTimeoutCached: 500,

// time in milliseconds that fetching index.html from network may take when no cached version is available
requestTimeoutUncached: 60000

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

## Authors

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

## Versioning

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

License
------------------------------------------------------------------------------

This project is licensed under the [MIT License](LICENSE.md).

[npm-badge]: https://img.shields.io/npm/v/ember-service-worker-index-fallback.svg
[npm-badge-url]: https://www.npmjs.com/package/ember-service-worker-index-fallback
[codeclimate-badge]: https://api.codeclimate.com/v1/badges/b62c466a81d28a69abd1/maintainability
[codeclimate-badge-url]: https://codeclimate.com/github/st-h/ember-service-worker-index-fallback/maintainability
[ember-observer-badge]: http://emberobserver.com/badges/ember-service-worker-index-fallback.svg
[ember-observer-badge-url]: http://emberobserver.com/addons/ember-service-worker-index-fallback
[dependencies-badge]: https://img.shields.io/david/st-h/ember-service-worker-index-fallback.svg
[dependencies-badge-url]: https://david-dm.org/st-h/ember-service-worker-index-fallback
[devDependencies-badge]: https://img.shields.io/david/dev/st-h/ember-service-worker-index-fallback.svg
[devDependencies-badge-url]: https://david-dm.org/st-h/ember-service-worker-index-fallback#info=devDependencies
6 changes: 4 additions & 2 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ module.exports = class Config extends Plugin {
let location = options.location || "index.html";
let excludeScope = options.excludeScope || [];
let includeScope = options.includeScope || [];
let requestTimeout = options.requestTimeout || 500;
let requestTimeoutCached = options.requestTimeoutCached || 500;
let requestTimeoutUncached = options.requestTimeoutUncached || 60000;

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

fs.writeFileSync(path.join(this.outputPath, "config.js"), module);
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "ember-service-worker-index-fallback",
"version": "0.8.0",
"description": "An Ember Service Worker plugin that caches the index.html file",
"version": "0.1.0",
"description": "An Ember Service Worker plugin that serves an Ember app's index file and falls back to a cached version when loading fails",
"directories": {
"doc": "doc",
"test": "tests"
Expand Down
25 changes: 9 additions & 16 deletions service-worker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {
VERSION,
INDEX_EXCLUDE_SCOPE,
INDEX_INCLUDE_SCOPE,
REQUEST_TIMEOUT
REQUEST_TIMEOUT_CACHED,
REQUEST_TIMEOUT_UNCACHED

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

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

event.respondWith(new Promise(function (fulfill, reject) {
fromNetwork(request, REQUEST_TIMEOUT).then((response) => {
fulfill(response);
}, (error) => {
fromCache(request).then((response) => {
if (response) {
fulfill(response);
} else {
reject(response);
}
caches.match(INDEX_HTML_URL, { cacheName: CACHE_NAME }).then((cachedResponse) => {
const timeout = cachedResponse ? REQUEST_TIMEOUT_CACHED : REQUEST_TIMEOUT_UNCACHED;

fromNetwork(request, timeout).then((response) => {
fulfill(response);
}, (error) => {
reject(error);
})
fulfill(cachedResponse);
});
})
}));
}
});

function fromCache() {
return caches.match(INDEX_HTML_URL, { cacheName: CACHE_NAME });
}

function fromNetwork(request, timeout) {
return new Promise(function (fulfill, reject) {

Expand Down

0 comments on commit 093993e

Please sign in to comment.