Skip to content

Commit

Permalink
Breaking: use workbox-build (#138)
Browse files Browse the repository at this point in the history
* Use yarn 2, bump node requirements

* bump caniuse-lite, built via terser

* loosen node support as we are not publishing modules :(

* breaking: use workbox-build

* use GitHub CI

* fix typo

* fix build

* add tests for deprecation message

* run coverage

* tune docs
  • Loading branch information
JLHwung committed Mar 6, 2021
1 parent bc026bc commit 8208bef
Show file tree
Hide file tree
Showing 24 changed files with 8,063 additions and 6,320 deletions.
72 changes: 72 additions & 0 deletions .github/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
name: CI

on: [push, pull_request]

env:
YARN_NODE_LINKER: pnp # use pnp linker for better linking performance
YARN_ENABLE_SCRIPTS: false # disable post-install scripts

jobs:
prepare-yarn-cache:
name: Prepare Cache
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Use Node.js latest
uses: actions/setup-node@v2-beta
with:
node-version: "*"
- name: Get yarn cache directory path
id: yarn-cache-dir-path
run: echo "::set-output name=dir::$(yarn config get cacheFolder)"
- uses: actions/cache@v2
id: yarn-cache
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
key: yarn-${{ hashFiles('yarn.lock') }}
restore-keys: |
yarn-
- name: Check Yarn dedupe
if: steps.yarn-cache.outputs.cache-hit != 'true'
run: |
yarn dedupe --check
- name: Check or update Yarn cache
env:
run: |
yarn install --immutable --skip-builds
test:
name: Test on Node.js # GitHub will add ${{ matrix.node-version }} to this title
needs: prepare-yarn-cache
runs-on: ${{ matrix.os }}
strategy:
matrix:
node-version: ["*", 14, 12]
os: ["ubuntu-latest", "windows-latest"]
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Use Node.js latest # Run yarn on latest node
uses: actions/setup-node@v2-beta
with:
node-version: "*" # Build Babel on latest node LTS versions
- name: Get yarn cache directory path
id: yarn-cache-dir-path
run: echo "::set-output name=dir::$(yarn config get cacheFolder)"
- uses: actions/cache@v2
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
key: yarn-${{ hashFiles('yarn.lock') }}
- name: Install
run: |
yarn install
- name: Use Node.js ${{ matrix.node-version }} # Checkout node version for test executor
uses: actions/setup-node@v2-beta
with:
node-version: ${{ matrix.node-version }}
- name: Test
run: |
yarn test --coverage
- name: Coveralls GitHub Action
if: matrix.node-version == '*'
uses: coverallsapp/github-action@v1.1.2
74 changes: 74 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
name: CI

on: [push, pull_request]

env:
YARN_NODE_LINKER: pnp # use pnp linker for better linking performance
YARN_ENABLE_SCRIPTS: false # disable post-install scripts

jobs:
prepare-yarn-cache:
name: Prepare Cache
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Use Node.js latest
uses: actions/setup-node@v2-beta
with:
node-version: "*"
- name: Get yarn cache directory path
id: yarn-cache-dir-path
run: echo "::set-output name=dir::$(yarn config get cacheFolder)"
- uses: actions/cache@v2
id: yarn-cache
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
key: yarn-${{ hashFiles('yarn.lock') }}
restore-keys: |
yarn-
- name: Check Yarn dedupe
if: steps.yarn-cache.outputs.cache-hit != 'true'
run: |
yarn dedupe --check
- name: Check or update Yarn cache
run: |
yarn install --immutable --skip-builds
test:
name: Test on Node.js # GitHub will add ${{ matrix.node-version }} to this title
needs: prepare-yarn-cache
runs-on: ${{ matrix.os }}
strategy:
matrix:
node-version: ["*", 14, 12]
os: ["ubuntu-latest", "windows-latest"]
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Use Node.js latest # Run yarn on latest node
uses: actions/setup-node@v2-beta
with:
node-version: "*" # Build Babel on latest node LTS versions
- name: Get yarn cache directory path
id: yarn-cache-dir-path
run: echo "::set-output name=dir::$(yarn config get cacheFolder)"
- uses: actions/cache@v2
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
key: yarn-${{ hashFiles('yarn.lock') }}
- name: Install
run: |
yarn install
- name: Use Node.js ${{ matrix.node-version }} # Checkout node version for test executor
uses: actions/setup-node@v2-beta
with:
node-version: ${{ matrix.node-version }}
- name: Test
run: |
yarn test --coverage
yarn spellcheck
- name: Coveralls GitHub Action
if: matrix.node-version == '*'
uses: coverallsapp/github-action@v1.1.2
with:
github-token: ${{ secrets.github_token }}
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,10 @@ jspm_packages

# Built lib
/lib/

# Yarn 2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.js
7 changes: 0 additions & 7 deletions .nycrc

This file was deleted.

12 changes: 0 additions & 12 deletions .travis.yml

This file was deleted.

55 changes: 55 additions & 0 deletions .yarn/releases/yarn-berry.cjs

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
yarnPath: ".yarn/releases/yarn-berry.cjs"
nodeLinker: node-modules
3 changes: 2 additions & 1 deletion .yaspellerrc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"ServiceWorker",
"sw",
"travis",
"vuejs",
"v1",
"workbox",
"yaspeller"
],
"excludeFiles": [
Expand Down
61 changes: 33 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
# hexo-offline

[![npm version](https://img.shields.io/npm/v/hexo-offline.svg?style=flat-square)](https://www.npmjs.com/package/hexo-offline)
[![Build Status](https://img.shields.io/travis/JLHwung/hexo-offline.svg?style=flat-square)](https://travis-ci.org/JLHwung/hexo-offline)
[![AppVeyor build status](https://img.shields.io/appveyor/ci/JLHwung/hexo-offline.svg?style=flat-square)](https://ci.appveyor.com/project/JLHwung/hexo-offline)
![Build Status](https://img.shields.io/github/workflow/status/JLHwung/hexo-offline/ci.svg?style=flat-square)
[![Coverage Status](https://img.shields.io/coveralls/JLHwung/hexo-offline.svg?style=flat-square)](https://coveralls.io/github/JLHwung/hexo-offline)
[![Dependencies Status](https://img.shields.io/david/JLHwung/hexo-offline.svg?style=flat-square)](https://david-dm.org/JLHwung/hexo-offline)
[![Dev Dependencies Status](https://img.shields.io/david/dev/JLHwung/hexo-offline.svg?style=flat-square)](https://david-dm.org/JLHwung/hexo-offline?type=dev)

hexo-offline is intended to provide offline experience for [hexo](https://hexo.io) built static website. It uses _ServiceWorker_ under the hood. Simply install this plugin to your website and it should be offline ready by caching most of static assets.
hexo-offline is intended to provide [offline experience](https://web.dev/progressive-web-apps/) for [hexo](https://hexo.io) built static website. It uses _ServiceWorker_ under the hood. Simply install this plugin to your website and it should be offline ready by caching most of static assets.

See [here](https://github.com/JLHwung/hexo-offline/tree/v1) for v1 docs.

## Demo

- [vuejs.org](https://vuejs.org): Read Vuejs documentation in the flight.
- [徑庭](https://jhuang.me)

## Install

Expand All @@ -25,21 +26,20 @@ Once installed, run `hexo clean && hexo generate` to activate offline experience

If the website serves all content from the origin server, you don't have to add any config. Simply install and run `hexo clean && hexo generate`.

While hexo-offline aims to provide zero-config offline enhancement to your hexo project, it does offer full list of options control from [sw-precache](https://github.com/GoogleChrome/sw-precache#options-parameter). Simply add your configuration to the root `_config.yml`.
While hexo-offline aims to provide zero-config offline enhancement to your hexo project, it does offer full list of options control from [workbox-build](https://developers.google.com/web/tools/workbox/reference-docs/latest/module-workbox-build?hl=en#.generateSW). Create a `hexo-offline.config.cjs` in the hexo root directory

```yaml
# offline config passed to sw-precache.
offline:
maximumFileSizeToCacheInBytes: 5242880
staticFileGlobs:
- public/**/*.{js,html,css,png,jpg,gif,svg,eot,ttf,woff,woff2}
stripPrefix: public
verbose: true
```js
// offline config passed to workbox-build.
module.exports = {
globPatterns: ["**/*.{js,html,css,png,jpg,gif,svg,eot,ttf,woff}"],
globDirectory: "/path/to/hexo/public",
swDest: "/path/to/hexo/service-worker.js",
},
```

Again, the config is demonstration only and you don't have to copy and paste if you serves all contents from the origin server.
Again, the config is demo only and you don't have to copy and paste if you serves all contents from the origin server.

### How if content is served via CDN?
### What if content is served via CDN?

Suppose that you have used two CDN scripts:

Expand All @@ -48,21 +48,26 @@ Suppose that you have used two CDN scripts:
- http://cdn.another-example.org/script-name/script-version.css
```

Add this config to root `_config.yml`

```yaml
offline:
runtimeCaching:
- urlPattern: /*
handler: cacheFirst
options:
origin: cdn.example.com
- urlPattern: /*
handler: cacheFirst
options:
origin: cdn.another-example.org
Add this config to root `hexo-offline.config.cjs`

```js
// offline config passed to workbox-build.
module.exports = {
runtimeCaching: [
{
urlPattern: /^https:\/\/cdn\.example\.com\/.*/,
handler: "CacheFirst"
},
{
urlPattern: /^https:\/\/cdn\.another-example\.org\/.*/,
handler: "CacheFirst"
}
]
},
```

For more information, see [Workbox Runtime Caching Entry](https://developers.google.com/web/tools/workbox/reference-docs/latest/module-workbox-build?hl=en#.RuntimeCachingEntry).

Note:

1. As the CDN resources is runtime cached, it means that the resource will be cached only after a user-agent visit the page where the resource is referenced. Therefore, if you have included a CDN resource `example.com/script.js` in `some-page.html` only, the user who visit `index.html` only would not have `example.com/script.js` in cache.
Expand Down
33 changes: 0 additions & 33 deletions appveyor.yml

This file was deleted.

11 changes: 0 additions & 11 deletions babel.config.js

This file was deleted.

23 changes: 23 additions & 0 deletions babel.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"presets": [
[
"@babel/preset-env",
{
"targets": { "node": "12.17" }
}
]
],
"overrides": [
{
"test": ["./src/lib/template.js"],
"presets": [
[
"@babel/preset-env",
{
"forceAllTransforms": true
}
]
]
}
]
}
4 changes: 2 additions & 2 deletions husky.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = {
hooks: {
"pre-commit": "lint-staged"
}
"pre-commit": "lint-staged",
},
};
5 changes: 0 additions & 5 deletions jest.config.js

This file was deleted.

2 changes: 1 addition & 1 deletion lint-staged.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports = {
"*.{js,md,yml}": ["prettier --write", "git add"]
"*.{js,md,yml}": ["prettier --write"],
};

0 comments on commit 8208bef

Please sign in to comment.