The*_*eoG 15 javascript reactjs service-worker sw-precache
操作系统:Windows 10 Pro
webpack:1.14.0
sw-precache-webpack-plugin:0.9.1
sw-precache:5.0.0
所以,我启动我的网站并暂时不做任何事情,然后在devTools中生成上面指定的错误消息.如果执行某个过程,则不会出现错误
我的React代码如下:
webpack.config.prod.js
var path = require('path');
var webpack = require('webpack');
var SWPrecacheWebpackPlugin = require('sw-precache-webpack-plugin');
module.exports = {
devtool: 'source-map',
context: __dirname,
entry: {
main: path.resolve(__dirname, './client/app'),
},
output: {
path: path.join(__dirname, '/public'),
filename: 'bundle.js',
publicPath: '/public/'
},
plugins: [
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.DefinePlugin({
'process.env': {
'NODE_ENV': "'production'"
}
}),
new webpack.optimize.UglifyJsPlugin({
compressor: {
warnings: false
}
}),
new SWPrecacheWebpackPlugin(
{
cacheId: 'flamingoCity',
filename: 'my-service-worker.js',
stripPrefix: path.join(__dirname, 'public').replace(/\\/g,"/"),
maximumFileSizeToCacheInBytes: 6194304,
minify: true,
runtimeCaching: [{
handler: 'cacheFirst',
urlPattern: /[.]mp3$/,
}],
}
),
],
module: {
loaders: [
// js
{
test: /\.js$/,
loaders: ['babel'],
include: path.join(__dirname, 'client')
},
// CSS
{
test: /\.styl$/,
include: path.join(__dirname, 'client'),
loader: 'style-loader!css-loader!stylus-loader'
}
]
}
};Run Code Online (Sandbox Code Playgroud)
app.js
/*
Import Dependencies
*/
import React from 'react';
import { render } from 'react-dom';
import { Provider } from 'react-redux';
import { Router, Route, IndexRoute } from 'react-router'
import 'babel-polyfill';
/*
Import Components
*/
import App from './components/App';
import Single from './components/Single';
import PhotoGrid from './components/PhotoGrid';
/* Import CSS */
import css from './styles/style.styl';
/* Import our data store */
import store, { history } from './store';
/*
Error Logging
*/
import Raven from 'raven-js';
import { sentry_url } from './data/config';
if(window) {
Raven.config(sentry_url).install();
}
/*
Register Service Worker
*/
if('serviceWorker' in navigator && process.env.NODE_ENV === 'production') {
navigator.serviceWorker.register('./my-service-worker.js').then(function(reg) {
// updatefound is fired if my-service-worker.js changes.
reg.onupdatefound = function() {
// The updatefound event implies that reg.installing is set; see
// https://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.html#service-worker-container-updatefound-event
var installingWorker = reg.installing;
installingWorker.onstatechange = function() {
switch (installingWorker.state) {
case 'installed':
if (navigator.serviceWorker.controller) {
// At this point, the old content will have been purged and the fresh content will
// have been added to the cache.
// It's the perfect time to display a "New content is available; please refresh."
// message in the page's interface.
console.log('New or updated content is available.');
} else {
// At this point, everything has been precached.
// It's the perfect time to display a "Content is cached for offline use." message.
console.log('Content is now available offline!');
}
break;
case 'redundant':
console.error('The installing service worker became redundant.');
break;
}
};
};
}).catch(function(e) {
console.error('Error during service worker registration:', e);
});
}
/*
Rendering
This is where we hook up the Store with our actual component and the router
*/
render(
<Provider store={store}>
{ /* Tell the Router to use our enhanced history */ }
<Router history={history}>
<Route path="/" component={App}>
<IndexRoute component={PhotoGrid} />
<Route path="/view/:postId" component={Single}></Route>
</Route>
</Router>
</Provider>,
document.getElementById('root')
);Run Code Online (Sandbox Code Playgroud)
这是什么问题?
Jef*_*ick 41
通常情况下,空闲服务工作者会被优先杀死,以防止代码在不需要时在后台运行.
Chrome会采取措施来检测DevTools是否已打开,如果是,则不会自动终止服务工作者.假设如果开发人员使用DevTools,他们可能正在调试或以其他方式检查服务工作者的行为,并且杀死服务工作者会使开发人员感到沮丧.
延长以前默认使用的服务工作者的生命周期,但如此处所述,对于过去几个版本的Chrome,消息
由于附加了DevTools,超时计时器终止服务工作程序被取消.
登录到控制台,让开发人员知道服务工作人员通常会被杀死,但由于DevTools被打开,杀死它的工作被取消了.
你可能会问,这有什么不同?为什么要让开发人员知道某些事情没有发生呢?
指出这一点的动机是让开发人员知道真实用户在生产中会遇到的事情(即服务工作者被重复杀死/重新启动)在这个调试环境中没有发生.如果开发人员编写了错误的服务工作者代码,对全局状态的持久性做出错误的假设,那么这就有所不同.编写假定全局状态始终存在的代码很容易,并且在DevTools打开时运行(因为服务工作者永远不会被杀死),然后无法在生产中工作.
| 归档时间: |
|
| 查看次数: |
9360 次 |
| 最近记录: |