- Chrome 52 or above
- Web Server for Chrome
git clone https://github.com/SunShinewyf/first-pwa.git
open the url and click the blue button to Launch App
click "CHOOSE FOLDER" as follow,and choose the folder downloaded just now
then click the url
git clone https://github.com/SunShinewyf/first-pwa.git
open the url and click the blue button to Launch App
click "CHOOSE FOLDER" as follow,and choose the folder downloaded just now
then click the url
现在开始从头部署一个PWA项目
其中manifest.json
如下所示:
{
"name": "Weather",
"short_name": "Weather",
"icons": [{
"src": "images/icons/icon-128x128.png",
"sizes": "128x128",
"type": "image/png"
}, {
"src": "images/icons/icon-144x144.png",
"sizes": "144x144",
"type": "image/png"
}, {
"src": "images/icons/icon-152x152.png",
"sizes": "152x152",
"type": "image/png"
}, {
"src": "images/icons/icon-192x192.png",
"sizes": "192x192",
"type": "image/png"
}, {
"src": "images/icons/icon-256x256.png",
"sizes": "256x256",
"type": "image/png"
}],
"start_url": "/index.html",
"display": "standalone",
"background_color": "#3E4EB8",
"theme_color": "#2F3BA2"
}
然后根目录下面的service-worker.js文件内容如下:
/**
* author:SunShinewyf
* date:2017-4-20
* desc:some setting of worker
**/
var dataCacheName = 'ppData-v1';
var cacheName = 'ppPWA-final-1';
var filesToCache = [
'/',
'/index.html',
'styles/index.css'
];
self.addEventListener('install',function(e){
console.log('[pp serviceWorker install');
e.waitUntil(
caches.open(cacheName).then(function(cache){
console.log('serviceWorker Caching app shell');
return cache.addAll(filesToCache);
})
);
});
self.addEventListener('activate', function(e) {
console.log('[ServiceWorker] Activate');
e.waitUntil(
caches.keys().then(function(keyList) {
return Promise.all(keyList.map(function(key) {
if (key !== cacheName && key !== dataCacheName) {
console.log('[ServiceWorker] Removing old cache', key);
return caches.delete(key);
}
}));
})
);
return self.clients.claim();
});
self.addEventListener('fetch', function(e) {
console.log('[Service Worker] Fetch', e.request.url);
var dataUrl = 'https://query.yahooapis.com/v1/public/yql';
if (e.request.url.indexOf(dataUrl) > -1) {
e.respondWith(
caches.open(dataCacheName).then(function(cache) {
return fetch(e.request).then(function(response){
cache.put(e.request.url, response.clone());
return response;
});
})
);
} else {
e.respondWith(
caches.match(e.request).then(function(response) {
return response || fetch(e.request);
})
);
}
});
至于app.js
和index.html
则根据你具体的项目而定,你可以根据自己的项目填充一些东西
添加web server for Chrome
点击这里
添加后会跳转到这个页面:
点击choose folder
,然后选中上面新建的index.html
,然后打开下面的http://127.0.0.1:8887
就可以访问了
部署到firebase
Firebase
账户npm
安装 Firebase
:npm install -g firebase-tools
firebase login
,会看到下面的页面:这时浏览器会自动跳转到下面这个页面
点击'允许'之后就会弹出一个页面
这里是public
目录里面初始的index.html
页面,这里需要将这个页面更改为你自己的项目文件
执行firebase init
这时会跳出一些选项,你可以针对自己的实际情况进行选择。这里不多说
执行firebase deploy
当看到控制台出现下面这样的时候:
就说明已经部署成功了
至此,大功告成。
不过这个demo比较简单,也没有配置数据库,仅当入门教程使用
PWA是Progressive Web App
的简称,主要有下面几个特点:
html
,css
,js
这些技术,还有manifest以及Service Worker。下面是一张service Worker在各个浏览器中兼容的图片:
从上图可以看出,Chrome和Firefox,Opera都已经支持Service Worker
,Safari2015年也提过会在5年计划中提供支持
web App Manifest
是指通过一个清单文件像浏览器暴露web应用的元数据,其中包括名字,要缓存的icons信息以及一些页面的主题颜色等。
下面附上一个例子:
{
"name": "pp android",
"short_name": "pp",
"icons":[{
"src":"public/images/1.jpg",
"type":"image/jpg"
}],
"start_url": "/index.html",
"description": "the pp android of pwa version",
"display": "standalone",
"background_color": "#FFF",
"theme_color": "#2F3BA2",
"lang":"en"
}
这里解释一下比较生疏的字段含义:
scope
:定义了 web
应用的浏览作用域.start_url
:定义了一个 PWA 的入口页面。orientation
:屏幕旋转theme_color/background_color
:主题色与背景色manifest
还有一些其他的字段,具体信息可移步这里
Service Workers
是谷歌 chrome
团队提出并大力推广的一项 web 技术,可以充当应用通服务器之间的代理服务器,可以用于拦截请求
Service Workers
有几点需要注意的地方:
Service Workers
基于 Https
,这是硬性条件Service Worker
都有自己的作用域,它只会处理自己作用域下的请求,而 Service Worker
的存放位置就是它的最大作用域Service Workder
是 Web Worker
的一种,它不能够直接操作 DOM
Service Worker
有一个生命周期,如下图所示:
关于Service Worker
的一些api和用法,可以异步这里和这里
下面收录一些关于PWA的文章
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.