与Vue中的Workbox进行后台同步



我正试图在一个非常简单的Vue示例应用程序中使用WorkBox发出后台同步请求。我修改了我的代码很长一段时间,但我没有找到解决方案,或者可能是我的代码出了问题。

服务工作者注册良好,当我发出ajax请求时,后台同步会被激活。

VUE

<template>
<div class="users">
<h1>USERS</h1>
<input type="text" v-model="qty" placeholder="How many users to find">
<button @click.prevent="search()">How many users would you like to find?</button>
<ul>
<li v-for="user in users">
{{user.email}}
</li>
</ul>
</div>
</template>
<script>
import axios from 'axios'
export default {
data(){
return {
users: null,
qty: 10
}
},
methods:{
search(){
axios.get('https://randomuser.me/api', {
params: {
results: this.qty
}
})
.then(response => {
console.log(response);
this.users = response.data.results
})
}
}
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>

VUE.CONFIG.JS

module.exports = {
// ...other vue-cli plugin options...
pwa: {
name: 'My App',
themeColor: '#4DBA87',
msTileColor: '#000000',
appleMobileWebAppCapable: 'yes',
appleMobileWebAppStatusBarStyle: 'black',
workboxPluginMode: 'GenerateSW',
workboxOptions: {
runtimeCaching: [{
urlPattern: new RegExp('https://randomuser.me/api'),
handler: 'networkOnly',
options: {
//background sync. conf
backgroundSync: {
name: 'my-queue-asier',
options: {
maxRetentionTime: 60 * 60,
}
}
}
}]
}
}
}

这些是事件启动时DevTools的屏幕截图。

当网络断开连接时,我在控制台中收到此错误。我不确定如果后台同步工作正常是否正常。。。

后台同步已在indexDB选项卡中注册

最后是网络选项卡。我们看到API调用很好,因为当网络返回时,它会进行调用,但应用程序中没有发生任何其他事情,并且旧数据不会被新数据取代:/

有人能帮我吗?我找了一下,但什么也没找到。。。

谢谢!

如果您有一个对缓存的GET调用,正如您的代码片段所建议的那样,那么您不需要后台同步。您只需要一个具有正确处理程序和url模式的运行时缓存结构,就可以缓存get调用。POST/PUT/DELETE调用需要后台同步。

最新更新