前面介绍了PWA技术,那通过示例看看PWA如何让一个站点变成APP吧
准备工作,下载SpreadJS在线表格编辑器示例,在页面点击立即试用即可下载:https://www.grapecity.com.cn/developer/spreadjs
让SpreadJS在线表格编辑器支持PWA只需要实现App Manifest 和 Service Worker
1. 添加 manifest.json 文件
新建manifest.json,并在index.html中引用
- {
- "name": "SpreadJSDesigner",
- "short_name": "SJSD",
- "descriptions": "SpreadJS在线表格编辑器",
- "start_url": "./",
- "background_color": "#fff",
- "display": "minimal-ui",
- "scope": "./",
- "theme_color": "#fff",
- "icons": [
- {
- "src": "./welcome.png",
- "type": "image/png",
- "sizes": "200x200",
- "purpose": "any"
- }
- ]
- }
复制代码- <link rel="manifest" href="./manifest.json">
复制代码
2. 实现Service Worker
新建sw.js, 通过Service Worker缓存设计器所需要的spreadjs资源
- var cacheName = 'v14.2.2';
- var cacheFiles = [
- '/',
- './index.html',
- './lib/css/gc.spread.sheets.excel2013white.14.2.2.css',
- './lib/css/gc.spread.sheets.designer.14.2.2.css',
- './custom.css',
- './lib/scripts/gc.spread.sheets.all.14.2.2.js',
- './lib/scripts/plugins/gc.spread.sheets.charts.14.2.2.js',
- './lib/scripts/plugins/gc.spread.sheets.shapes.14.2.2.js',
- './lib/scripts/plugins/gc.spread.sheets.print.14.2.2.js',
- './lib/scripts/plugins/gc.spread.sheets.barcode.14.2.2.js',
- './lib/scripts/plugins/gc.spread.sheets.pdf.14.2.2.js',
- './lib/scripts/plugins/gc.spread.pivot.pivottables.14.2.2.js',
- './lib/scripts/interop/gc.spread.excelio.14.2.2.js',
- './lib/scripts/resources/zh/gc.spread.sheets.resources.zh.14.2.2.js',
- './lib/scripts/gc.spread.sheets.designer.resource.cn.14.2.2.js',
- './lib/scripts/gc.spread.sheets.designer.all.14.2.2.js',
- ];
- // 监听 install 事件,安装完成后,进行文件缓存
- self.addEventListener('install', function (e) {
- console.log('Service Worker 状态: install');
- var cacheOpenPromise = caches.open(cacheName).then(function (cache) {
- // 把要缓存的 cacheFiles 列表传入
- return cache.addAll(cacheFiles);
- });
- e.waitUntil(cacheOpenPromise);
- });
- // 监听 fetch 事件,安装完成后,进行文件缓存
- self.addEventListener('fetch', function (e) {
- console.log('Service Worker 状态: fetch');
- var cacheMatchPromise = caches.match(e.request).then(function (cache) {
- // 如果有cache则直接返回,否则通过fetch请求
- return cache || fetch(e.request);
- }).catch(function (err) {
- console.log(err);
- return fetch(e.request);
- })
- e.respondWith(cacheMatchPromise);
- });
- // 监听 activate 事件,清除缓存
- self.addEventListener('activate', function (e) {
- console.log('Service Worker 状态: activate');
- var cachePromise = caches.keys().then(function (keys) {
- return Promise.all(keys.map(function (key) {
- if (key !== cacheName) {
- return caches.delete(key);
- }
- }));
- })
- e.waitUntil(cachePromise);
- return self.clients.claim();
- });
复制代码 index.html页面组册sw.js
- <script>
- if ('serviceWorker' in navigator) {
- window.addEventListener('load', function () {
- navigator.serviceWorker.register('./sw.js')
- .then(function (registration) {
- // 注册成功
- console.log('ServiceWorker registration successful with scope: ', registration.scope);
- })
- .catch(function (err) {
- // 注册失败:
- console.log('ServiceWorker registration failed: ', err);
- });
- });
- }
- </script>
复制代码
通过以上两个步骤的操作,spreadjs在线表格编辑器页面就支持PWA了。注意PWA需要https的支持,本地通过localhost测试不受影响。
通过localhost访问页面,可以在Chrome地址栏看到安装选项
安装后,就可以通过应用程序按钮双击访问了
对于Chrome 的PWA应用,同样可以通过快捷键开启开发者工具,在Network中可以看到,资源都是通过ServiceWorker缓存获取
|
|