Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added flag to control online reload behaviour #236

Merged
merged 5 commits into from
Jul 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,8 @@ module.exports = withPWA({
- ~~default: `""` - i.e. default with no prefix~~
- ~~example: `/subdomain` if the app is hosted on `example.com/subdomain`~~
- deprecated, use [basePath](https://nextjs.org/docs/api-reference/next.config.js/basepath) instead
- reloadOnOnline - changes the behaviour of the app when the device detects that it has gone back "online" and has a network connection. Indicate if the app should call `location.reload()` to refresh the app.
- default: `true`

### Other Options

Expand Down
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ module.exports = (nextConfig = {}) => ({
modifyURLPrefix = {},
fallbacks = {},
cacheOnFrontEndNav = false,
reloadOnOnline = true,
scope = basePath,
subdomainPrefix, // deprecated, use basePath in next.config.js instead
...workbox
Expand Down Expand Up @@ -77,7 +78,8 @@ module.exports = (nextConfig = {}) => ({
__PWA_SCOPE__: `'${_scope}'`,
__PWA_ENABLE_REGISTER__: `${Boolean(register)}`,
__PWA_START_URL__: dynamicStartUrl ? `'${basePath}'` : undefined,
__PWA_CACHE_ON_FRONT_END_NAV__: `${Boolean(cacheOnFrontEndNav)}`
__PWA_CACHE_ON_FRONT_END_NAV__: `${Boolean(cacheOnFrontEndNav)}`,
__PWA_RELOAD_ON_ONLINE__: `${Boolean(reloadOnOnline)}`
})
)

Expand Down
8 changes: 5 additions & 3 deletions register.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ if (typeof window !== 'undefined' && 'serviceWorker' in navigator && typeof cach
})
}

window.addEventListener('online', () => {
location.reload()
})
if(__PWA_RELOAD_ON_ONLINE__) {
window.addEventListener('online', () => {
location.reload()
})
}
}