Giter Site home page Giter Site logo

pwa-toolkit's Introduction

pwa-toolkit

功能

  • Mock: 浏览器端的 mock server,本地开发时Chrome可用
  • Cache: 静态资源缓存加速、离线体验
  • Demo: Mock、Cache的使用演示Demo

使用方式

1、Mock

主页面(例如index.js)代码:

import { Mock } from 'pwa-toolkit'
new Mock.MockRegister({ filename: '/service-worker.js' })
// 可以开始请求数据了
fetch('/mock/demo1').then(res => res.json()).then(data => console.log(data))
fetch('/mock/demo2', { method: 'POST' }).then(res => res.json()).then(data => console.log(data))


service-worker.js代码:

// service-worker.js 不建议设置 max-age 等http缓存
import { Mock } from 'pwa-toolkit'
const mock = new Mock.MockServer()

mock.get('/mock/demo1', function(req, res) {
  res.status = 200
  res.body = { data: 'demo1' }
})

mock.post('/mock/demo2', (req, res) => {
  res.body = { data: 'demo2' }
})
2、Cache

主页面(例如index.js)代码:

import { Cache } from 'pwa-toolkit'
new Cache.CacheRegister({ filename: '/service-worker.js' })

service-worker.js代码:

// service-worker.js 不建议设置 max-age 等http缓存
import { Cache } from 'pwa-toolkit'
const cache = new Cache.CacheServer({ cacheName: 'cache-name', assets: ['/'] })

配置说明

1、Mock
Mock.MockRegister: {
  // 必传,service-worker 脚本的访问路径,也是它的作用域
  filename: ''
}

Mock.MockServer: 提供get、post、patch、put、delete等方法

todo:支持响应流数据等
2、Cache
Cache.CacheRegister: {
  filename: '',
  onError: function() {}, // 可选,serviceWorker 注册失败
  onInstalled: function() {}, // 可选,安装完成
  onInstallFailed: function() {}, // 可选,安装失败
  onUpdating: function() {}, // 可选,更新中
  onUpdateReady: function() {}, // 可选,更新完成
  onUpdated: function() { // 可选,新脚本激活成功
    window.location.reload();
  },
  onUpdateFailed: function() {}, // 可选,更新失败
  online: function() {}, // 可选,监听网络连接
  offline: function() {} // 可选,监听网络离线
}

Cache.CacheServer: {
  cacheName: 'cache-name', // 使用的缓存名称
  assets: [], // 要缓存的资源
  skipWaiting: false // 新脚本安装完成后是否跳过 waiting 状态
}

TODO

  • 消息推送Push

相关

pwa-toolkit's People

Watchers

James Cloos avatar CaiHuabin avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.