Skip to content

Commit e2e572f

Browse files
authored
Merge pull request #363 from smalruby/fix-reload
fix: unregister pwa on dev-server
2 parents 5bcab1b + 99aa0ed commit e2e572f

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

src/playground/index.ejs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
</style>
3838
</head>
3939
<body>
40+
<% if (htmlWebpackPlugin.options.pwa) { %>
4041
<script>
4142
if ('serviceWorker' in navigator) {
4243
window.addEventListener('load', function () {
@@ -52,5 +53,25 @@
5253
console.log('Service Worker is not supported by browser.');
5354
}
5455
</script>
56+
<% } else { %>
57+
<script>
58+
if ('serviceWorker' in navigator) {
59+
window.addEventListener('load', function () {
60+
navigator.serviceWorker.getRegistration('./service-worker.js').then(function (registration) {
61+
if (registration) {
62+
console.log('found PWA worker registration', registration.scope);
63+
registration.unregister().then(function (result) {
64+
console.log('PWA worker unregister', result);
65+
});
66+
} else {
67+
console.log('no PWA worker registration');
68+
}
69+
});
70+
});
71+
} else {
72+
console.log('Service Worker is not supported by browser.');
73+
}
74+
</script>
75+
<% } %>
5576
</body>
5677
</html>

webpack.config.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,21 +117,24 @@ const buildConfig = baseConfig.clone()
117117
chunks: ['gui'],
118118
template: 'src/playground/index.ejs',
119119
title: 'Smalruby',
120-
originTrials: JSON.parse(fs.readFileSync(path.resolve(__dirname, 'origin-trials.json')))
120+
originTrials: JSON.parse(fs.readFileSync('origin-trials.json')),
121+
pwa: process.env.NODE_ENV === 'production'
121122
}))
122123
.addPlugin(new HtmlWebpackPlugin({
123124
chunks: ['gui'],
124125
template: 'src/playground/index.ejs',
125126
filename: 'ja.html',
126127
title: 'スモウルビー',
127-
originTrials: JSON.parse(fs.readFileSync(path.resolve(__dirname, 'origin-trials.json')))
128+
originTrials: JSON.parse(fs.readFileSync('origin-trials.json')),
129+
pwa: process.env.NODE_ENV === 'production'
128130
}))
129131
.addPlugin(new HtmlWebpackPlugin({
130132
chunks: ['player'],
131133
filename: 'player.html',
132134
template: 'src/playground/index.ejs',
133135
title: 'Smalruby: Player Example',
134-
originTrials: JSON.parse(fs.readFileSync(path.resolve(__dirname, 'origin-trials.json')))
136+
originTrials: JSON.parse(fs.readFileSync('origin-trials.json')),
137+
pwa: process.env.NODE_ENV === 'production'
135138
}))
136139
.addPlugin(new CopyWebpackPlugin({
137140
patterns: [
@@ -145,7 +148,9 @@ const buildConfig = baseConfig.clone()
145148
context: 'src/examples'
146149
}
147150
]
148-
}))
151+
}));
152+
153+
const buildWithPwaConfig = buildConfig.clone()
149154
.addPlugin(
150155
new WorkboxPlugin.GenerateSW({
151156
disableDevLogs: !process.env.DEBUG,
@@ -188,5 +193,5 @@ const buildConfig = baseConfig.clone()
188193
const buildDist = process.env.NODE_ENV === 'production' || process.env.BUILD_MODE === 'dist';
189194

190195
module.exports = buildDist ?
191-
[buildConfig.get(), distConfig.get()] :
196+
[buildWithPwaConfig.get(), distConfig.get()] :
192197
buildConfig.get();

0 commit comments

Comments
 (0)