navigator.serviceWorker is not defined on iOS
See original GitHub issueI’m trying to bundle a PWA in an iOS app. The PWA should use a ServiceWorker.
I used the starter app provided by npx @capacitor/cli create
and tried to enable the ServiceWorker there.
My www/index.html now looks like this
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="UTF-8">
<title>Awesome Capacitor App</title>
<meta name="viewport" content="viewport-fit=cover, width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<meta name="format-detection" content="telephone=no">
<meta name="msapplication-tap-highlight" content="no">
<script src="https://unpkg.com/@ionic/pwa-elements@0.0.12/dist/ionicpwaelements.js"></script>
<link rel="icon" type="image/x-icon" href="assets/icon/favicon.ico">
<link rel="manifest" href="manifest.json">
<link rel="stylesheet" href="css/style.css">
<meta name="theme-color" content="#31d53d">
<script>
console.log('\'serviceWorker\' in navigator', 'serviceWorker' in navigator); // logs false
if ('serviceWorker' in navigator) {
console.log('install service worker'); // never gets logger
navigator.serviceWorker.register('service-worker.js')
.then(() => console.log('service worker installed'))
.catch(err => console.log('Error', err));
}
</script>
</head>
<body>
<capacitor-welcome></capacitor-welcome>
<!--
This simple example uses Capacitor Core as a bundled file.
For large apps, we recommend using a bundler/module loader such as
rollup or webpack and importing Capacitor using ES2015 imports.
-->
<script src="capacitor.js"></script>
<script src="js/capacitor-welcome.js"></script>
</body>
</html>
As mentioned in the snippet 'serviceWorker' in navigator
returns false, therefore the service worker cannot be installed.
Am I missing something?
I am building an emulating the App on XCode (Version 9.4 (9F1027a)) with iOS 11.4 as deployment target.
The same code seems to work on Android 8.1 emulated with Android Studio 3.1.3
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:11 (6 by maintainers)
Top Results From Across the Web
navigator.serviceWorker always undefined on mobile Chrome ...
I'm seeing navigator.serviceWorker always return undefined in mobile Chrome and mobile Firefox on both iOS and Android devices.
Read more >Navigator.serviceWorker - Web APIs | MDN
The Navigator.serviceWorker read-only property returns the ServiceWorkerContainer object for the associated document, which provides access ...
Read more >serviceworker registration fails on iOS Chrome or Edge
I am developing a PWA app and cannot get my serviceworker to register in Chrome, Firefox or Edge on iOS. It registers successfully...
Read more >Testing Service Workers, TypeError: undefined is not an object ...
On iPhone an access to ServiceWorker over http causes: TypeError: undefined is not an object (evaluating 'navigator.serviceWorker.register').
Read more >navigator.serviceWorker always undefined on mobile Chrome ...
Anyways, perhaps this is helpful for someone else who may hit this. Mobile Firefox and mobile Chrome on both iOS and Android certainly...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@diachedelic If I’m not mistaken - Capacitor launches its own web server which may or may not have an SSL cert. If it does use SSL it may be an issue with it being self-signed.
Its been a few months since I’ve used Capacitor, so if there is an option to load your PWA via URL instead I would give that a shot.
I was able to run our PWA+SW using this repo https://github.com/xtools-at/Android-PWA-Wrapper just to verify that it wasn’t an issue with webview.
Please, stop commenting on a closed issue that is not even related to what you are commenting about as the original issues is about iOS