我的团队和我有一个项目最初是作为PWA构建的,但后来决定废弃这个想法,因为我们意识到它需要比最初预期更频繁地改变。但是,服务工作者已经在线,以及新重新设计的网站登录页面。尽管我们努力清除PWA缓存,但我们的客户仍在报告他们正在接收该网站的旧缓存版本。
目前,我们已将服务工作者设置为在安装时删除所有缓存(以及任何事情发生时作为预防措施),以及在新页面实际>时取消注册服务工作者的一些JavaScript em>负载。但问题是,在用户向网站发出请求之前,这些都不会运行,此时浏览器已经加载了缓存的内容。是否可以清除此缓存和阻止浏览器加载已缓存的任何内容?
当前的service-worker.js
// Caching
var cacheCore = 'mkeSculptCore-0330121058';
var cacheAssets = 'mkeSculptAssets-0330121058';
self.addEventListener('install', function (event) {
self.skipWaiting();
caches.keys().then(function (names) {
for (let name of names)
caches.delete(name);
});
});
self.addEventListener('activate', function (event) {
caches.keys().then(function (names) {
for (let name of names)
caches.delete(name);
});
});
self.addEventListener('fetch', function (event) {
caches.keys().then(function (names) {
for (let name of names)
caches.delete(name);
});
});
index.html中的脚本
(function () {
if ('serviceWorker' in navigator) {
navigator.serviceWorker.getRegistrations().then(function (registrations) {
//returns installed service workers
if (registrations.length) {
for (let registration of registrations) {
registration.unregister();
}
}
});
}
})();
到目前为止,我已经阅读了其他一些类似的StackOverflow答案including this one,但他们倾向于依赖用户手动执行某些操作来获取新内容,即。通过硬重新加载或通过浏览器设置手动禁用服务工作者。但是,在我的情况下,我们不能依赖手动用户操作。
答案 0 :(得分:0)
解决此问题的一种方法是在文件末尾添加一个时间戳(js,css)名称,这样每次发出请求时,缓存键在服务工作者中都不可用,因此它倾向于在每次加载时获取该文件的新版本。
<script type="text/javascript" src="/js/scipt1.js?t=05042018121212"/>
要在文件名中动态添加新的时间戳,请查看此answer
但如果HTML本身被缓存,这可能不可靠。
答案 1 :(得分:0)
在“更新”所有内容之前添加此内容:
07-23 18:59:52.967 11643-11643/com.seanwilliam.pokersingle E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.seanwilliam.pokersingle, PID: 11643
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:389)
at android.view.View.performClick(View.java:5619)
at android.view.View$PerformClick.run(View.java:22298)
at android.os.Handler.handleCallback(Handler.java:754)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:165)
at android.app.ActivityThread.main(ActivityThread.java:6365)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:883)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:384)
at android.view.View.performClick(View.java:5619)
at android.view.View$PerformClick.run(View.java:22298)
at android.os.Handler.handleCallback(Handler.java:754)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:165)
at android.app.ActivityThread.main(ActivityThread.java:6365)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:883)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
Caused by: java.lang.OutOfMemoryError: Failed to allocate a 26269068 byte allocation with 3836592 free bytes and 3MB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:624)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:457)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1152)
at android.content.res.ResourcesImpl.createFromResourceStream(ResourcesImpl.java:1272)
at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:743)
at android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:585)
at android.content.res.MiuiResourcesImpl.loadDrawable(MiuiResourcesImpl.java:308)
at android.content.res.Resources.getDrawable(Resources.java:785)
at android.content.Context.getDrawable(Context.java:534)
at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:358)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:198)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:186)
at android.support.v7.content.res.AppCompatResources.getDrawable(AppCompatResources.java:100)
at android.support.v7.widget.AppCompatImageHelper.setImageResource(AppCompatImageHelper.java:85)
at android.support.v7.widget.AppCompatImageView.setImageResource(AppCompatImageView.java:93)
at com.seanwilliam.pokersingle.MainActivity.Draw(MainActivity.java:207)
at com.seanwilliam.pokersingle.MainActivity.btnChange(MainActivity.java:98)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:384)
at android.view.View.performClick(View.java:5619)
at android.view.View$PerformClick.run(View.java:22298)
at android.os.Handler.handleCallback(Handler.java:754)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:165)
at android.app.ActivityThread.main(ActivityThread.java:6365)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:883)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
int[] imgCard = [R.drawable.c_ac, R.drawable.c_ad, R.drawable.c_ah, R.drawable.c_as,
R.drawable.c_2c, R.drawable.c_2d, R.drawable.c_2h, R.drawable.c_2s,
R.drawable.c_3c, R.drawable.c_3d, R.drawable.c_3h, R.drawable.c_3s,
R.drawable.c_4c, R.drawable.c_4d, R.drawable.c_4h, R.drawable.c_4s,
R.drawable.c_5c, R.drawable.c_5d, R.drawable.c_5h, R.drawable.c_5s,
R.drawable.c_6c, R.drawable.c_6d, R.drawable.c_6h, R.drawable.c_6s,
R.drawable.c_7c, R.drawable.c_7d, R.drawable.c_7h, R.drawable.c_7s,
R.drawable.c_8c, R.drawable.c_8d, R.drawable.c_8h, R.drawable.c_8s,
R.drawable.c_9c, R.drawable.c_9d, R.drawable.c_9h, R.drawable.c_9s,
R.drawable.c_10c, R.drawable.c_10d, R.drawable.c_10h, R.drawable.c_10s,
R.drawable.c_jc, R.drawable.c_jd, R.drawable.c_jh, R.drawable.c_js,
R.drawable.c_qc, R.drawable.c_qd, R.drawable.c_qh, R.drawable.c_qs,
R.drawable.c_kc, R.drawable.c_kd, R.drawable.c_kh, R.drawable.c_ks];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
card1= (ImageView) findViewById(R.id.card1);
card2= (ImageView) findViewById(R.id.card2);
card3= (ImageView) findViewById(R.id.card3);
card4= (ImageView) findViewById(R.id.card4);
card5= (ImageView) findViewById(R.id.card5);
hold1= (ImageView) findViewById(R.id.hold1);
hold2= (ImageView) findViewById(R.id.hold2);
hold3= (ImageView) findViewById(R.id.hold3);
hold4= (ImageView) findViewById(R.id.hold4);
hold5= (ImageView) findViewById(R.id.hold5);
}
public void btnChange(View view) {
if (bDeal) {
NewDeal();
}else {
Draw();
}
bDeal= ! bDeal;
}
public void NewDeal (){
hold1.setVisibility(View.INVISIBLE);
hold2.setVisibility(View.INVISIBLE);
hold3.setVisibility(View.INVISIBLE);
hold4.setVisibility(View.INVISIBLE);
hold5.setVisibility(View.INVISIBLE);
card1.setImageResource(R.drawable.c_blue_back);
card2.setImageResource(R.drawable.c_blue_back);
card3.setImageResource(R.drawable.c_blue_back);
card4.setImageResource(R.drawable.c_blue_back);
card5.setImageResource(R.drawable.c_blue_back);
cardNo[0]=-1;
cardNo[1]=-1;
cardNo[2]=-1;
cardNo[3]=-1;
cardNo[4]=-1;
GenerateCards();
bHold1 =false;bHold2 =false;bHold3 =false;bHold4 =false;bHold5 =false;
sKind[0]="";sKind[1]="";sKind[2]="";sKind[3]="";sKind[4]="";
iCard[0]=0;iCard[1]=0;iCard[2]=0;iCard[3]=0;iCard[4]=0;
int iC1, iC2, iC3, iC4, iC5;
iC1=cardNo[0];
iC2=cardNo[1];
iC3=cardNo[2];
iC4=cardNo[3];
iC5=cardNo[4];
card1.setImageResource(imgCard[iC1]);
card2.setImageResource(imgCard[iC2]);
card3.setImageResource(imgCard[iC3]);
card4.setImageResource(imgCard[iC4]);
card5.setImageResource(imgCard[iC5]);
}
public void Draw () {
if (!bHold1) {
cardNo[0] = -1;
}
if (!bHold2) {
cardNo[1] = -1;
}
if (!bHold3) {
cardNo[2] = -1;
}
if (!bHold4) {
cardNo[3] = -1;
}
if (!bHold5) {
cardNo[4] = -1;
}
//Get random number
GenerateCards();
//bHold1 =false;bHold2 =false;bHold3 =false;bHold4 =false;bHold5 =false;
sKind[0] = "";
sKind[1] = "";
sKind[2] = "";
sKind[3] = "";
sKind[4] = "";
iCard[0] = 0;
iCard[1] = 0;
iCard[2] = 0;
iCard[3] = 0;
iCard[4] = 0;
int iC1, iC2, iC3, iC4, iC5;
iC1 = cardNo[0];
iC2 = cardNo[1];
iC3 = cardNo[2];
iC4 = cardNo[3];
iC5 = cardNo[4];
if (!bHold1) {
card1.setImageResource(imgCard[iC1]); <--sometimes this is the error
}
if (!bHold2) {
card2.setImageResource(imgCard[iC2]); <--sometimes this is the error
}
if (!bHold3) {
card3.setImageResource(imgCard[iC3]); <--sometimes this is the error
}
if (!bHold4) {
card4.setImageResource(imgCard[iC4]); <--sometimes this is the error
}
if (!bHold5) {
card5.setImageResource(imgCard[iC5]);
}
}
为了使ServiceWorker停止使用它的所有窗口,必须关闭。 如果是网络应用,则可以使用window.close();
此代码仅加载列表中文件的新版本。 如果有任何内部缓存,它们将全部更新。