Fresh Project: Android Shows White Screen

Hello, I’ve just run through the following nuxt/ionic tutorial in an effort to build a cross platform app. I’ve even followed their 2023 vue conf youtube video as well although they appear to be the same exact project.

Everything works fine until I run npx cap run android. After the app splash screen shows I get a blank white screen. I attached a chrome debugger and can see the following errors:

entry.8cac1e1d.js:1 [nuxt] error caught during app initialization TypeError: Cannot read properties of undefined (reading 'beforeResolve')
    at setup (entry.8cac1e1d.js:1:99853)
    at entry.8cac1e1d.js:1:77265
    at n (entry.8cac1e1d.js:1:77676)
    at Object.runWithContext (entry.8cac1e1d.js:1:32116)
    at Ao (entry.8cac1e1d.js:1:77706)
    at Object.runWithContext (entry.8cac1e1d.js:1:76353)
    at gp (entry.8cac1e1d.js:1:77246)
    at bp (entry.8cac1e1d.js:1:77401)
    at async al (entry.8cac1e1d.js:67:1546)
(anonymous) @ entry.8cac1e1d.js:1
localhost/:1 Uncaught (in promise) TypeError: Failed to register a ServiceWorker for scope ('https://localhost/') with script ('https://localhost/sw.js'): An unknown error occurred when fetching the script.
/favicon.ico:1     Failed to load resource: the server responded with a status of 404 (Not Found)

I’m not sure what beforeResolve is from, I have a hunch it’s from webpack but I don’t believe these starter projects even use webpack so I may be wrong.

Here are some things I’ve tried:

  1. The app runs completely find outside of an emulator eg: running npm run dev and loading it up in the browser.
  2. I’ve tried installing a new device in android studio. Originally I was using API 33 on a Pixel XL and I’ve tried using API 33 on a Pixel 7 as well as API 34 on a Pixel 7 and neither worked.

Has anyone run into this before and can help?
I can see there are tons of these help posts for white screens and almost always the responses are to check devtools but in this case I don’t even understand these error messages. Clearly there’s some lifecycle hook somewhere that is expected but is not found due to some dependency issue.

Environment
Windows 11
Java 17.0.9+11-LTS-201
VSCode 1.83.1
Node 18.13.0
Android SDKs: Android 13.0 (“Tiramisu”) & Android API 34
Android Emulator 32.1.15
Android SDK Platform Tools: 34.0.5
package.json

"devDependencies": {
    "@capacitor/cli": "5.5.0",
    "@nuxt/devtools": "latest",
    "@nuxtjs/ionic": "^0.12.1",
    "@vue/cli-service": "5.0.8",
    "nuxt": "^3.7.4",
    "vue": "^3.3.4",
    "vue-router": "^4.2.5"
  },
  "dependencies": {
    "@capacitor/android": "5.5.0",
    "@capacitor/app": "5.0.6",
    "@capacitor/core": "5.5.0",
    "@capacitor/haptics": "5.0.6",
    "@capacitor/keyboard": "5.0.6",
    "@capacitor/status-bar": "5.0.6"
  }

nuxt.config

// https://nuxt.com/docs/api/configuration/nuxt-config
export default defineNuxtConfig({
  devtools: { enabled: true },
  modules: ["@nuxtjs/ionic"],
  css: ["@/theme/variables.css"],
  ssr: false
})

ionic.config.json

{
  "name": "nuxt-app",
  "integrations": {
    "capacitor": {}
  },
  "type": "vue"
}

capacitor.config.json

{
  "appId": "brandchamp",
  "appName": "brandchamp",
  "webDir": "dist",
  "server": {
    "androidScheme": "https"
  }
}

I just tried opening in android studio by running npx cap open android and gradle updated but I’m still getting the white screen even in android studio

Hi all…
I was facing the same issue and it took me 2 days of researching and changing etc…

Finally I could solve the issue:

First of all what caused the problem:

after running ionic serve and checking that everything works on web I did create the android version of the app with ionic capacitor add android
This went well and after running npx cap sync I got the error that the www folder doesn’t exist and you also get the notice to create the www folder with an index.html file in it by your self, which I did. Than did run again npx cap sync and this went well as well. Than open the project in android studio with npx cap open anroid and run the project on my actual phone and got the white screen, no error, nothing.

The problem was exactly the www folder with nothing than an empty index.html file.

after researching in many forums and githubs gathering all information together this was the solution:

  1. delete the android folder
  2. delete the www folder
  3. run: ionic capacitor add android
  4. run: ionic build (this will create the www folder with all needed html and js files in it)
  5. run: npx cap sync
  6. run: npx cap open andorid
  7. run the project on your phone.

that’s it.

Maybe a word to the ionic framework developers: npx cap sync could easily run ionic build in background in order to create the www folder with all needed files in it automatically.

Has it been solved? I have the same problem

Has anyone figured it out? Having the same issue

Uncaught (in promise) TypeError: Failed to register a ServiceWorker for scope

Setting androidScheme to https or hostname in server config does not change anything.

Errors like this typically happen because the WebView version on the Android device is too old. What device/emulator are you testing on and what version of Android/SDK?

I started my project about two weeks ago, but I’m now ready to release so I’m just running into that issue when not using my local dev server to live-reload the app.

Currently running on a Pixel 8 Pro API 34 emulator and my own physical Pixel 8 Pro on Android 14.

Same issue on both.

Here is my full error @twestrick

File: http://localhost/ - Line 0 - Msg: Uncaught (in promise) TypeError: Failed to register a ServiceWorker for scope (‘http://localhost/’) with script (‘http://localhost/sw.js’): An unknown error occurred when fetching the script.

I am using https://vite-pwa-org.netlify.app/ to generate everything related to the PWA configrations and SWs.

Assuming you have run npx cap sync android. In your project check android/src/main/assets to see if that sw.js file is there. That is the folder Capacitor copies all of your built/compiled JS project files to.

Looks like this in /android/app/src/main/assets/
image

So it seems right to me?

Yeah, looks correct to me. Is sw.js your own class/file or some npm package?

It is generated by vite-plugin-pwa - npm

Works fine on iOS and a normal browser.

You might then need to install the Vite Legacy plugin - this page mentions how to install. This also will honor your .browserlistrc file as Vite doesn’t out-of-the-box.

I instantiate the plugin without any options, just legacy().

And my .browserlistrc looks like (for Ionic 7 for reference):

Chrome >=79
ChromeAndroid >=79
Firefox >=70
Edge >=79
Safari >=14
iOS >=14

I can try it out, but I’m unsure what the purpose of this plugin is?
(At least for this issue)

Seems like all major browsers support the requirements of Vite

Vite’s default browser support baseline is Native ESM, native ESM dynamic import, and import.meta. This plugin provides support for legacy browsers that do not support those features when building for production.
vite/packages/plugin-legacy at main · vitejs/vite · GitHub

Thanks for your help so far!

I agree, it shouldn’t be needed, just throwing something out there :slight_smile:

Is the error you sent from LogCat in Android Studio?

Have you tried looking at DevTools by going to chrome://inspect/#devices?

Yeah

LogCat:

File: http://localhost/ - Line 0 - Msg: Uncaught (in promise) TypeError: Failed to register a ServiceWorker for scope (‘http://localhost/’) with script (‘http://localhost/sw.js’): An unknown error occurred when fetching the script.

Chrome console

Uncaught (in promise) TypeError: Failed to register a ServiceWorker for scope (‘http://localhost/’) with script (‘http://localhost/sw.js’): An unknown error occurred when fetching the script.

I removed the service worker and now I’m seeing errors in LogCat like

2024-05-09 10:17:32.553 20246-20293 chromium                com.distech.eclypsecontrol           E  [ERROR:simple_file_enumerator.cc(21)] opendir /data/user/0/com.distech.eclypsecontrol/cache/WebView/Default/HTTP Cache/Code Cache/wasm: No such file or directory (2)
2024-05-09 10:17:32.553 20246-20293 chromium                com.distech.eclypsecontrol           E  [ERROR:simple_index_file.cc(614)] Could not reconstruct index from disk
2024-05-09 10:17:32.553 20246-20308 chromium                com.distech.eclypsecontrol           E  [ERROR:simple_file_enumerator.cc(43)] readdir /data/user/0/com.distech.eclypsecontrol/cache/WebView/Default/HTTP Cache/Code Cache/js: No such file or directory (2)
2024-05-09 10:17:32.553 20246-20308 chromium                com.distech.eclypsecontrol           E  [ERROR:simple_index_file.cc(614)] Could not reconstruct index from disk
2024-05-09 10:35:16.709 13635-13694 chromium                com.distech.eclypsecontrol           E  [ERROR:simple_version_upgrade.cc(152)] Failed to write a new fake index.
2024-05-09 10:35:16.709 13635-13701 chromium                com.distech.eclypsecontrol           E  [ERROR:simple_version_upgrade.cc(152)] Failed to write a new fake index.

And a warning that worries me?

2024-05-09 10:35:16.809 13635-13635 Capacitor               com.distech.eclypsecontrol           W  Unable to read file at path public/plugins

Also getting this warning about Bluetooth, but I’m not using Bluetooth anywhere.

2024-05-09 10:35:16.822 13635-13697 cr_media                com.distech.eclypsecontrol           W  BLUETOOTH_CONNECT permission is missing.
2024-05-09 10:35:16.834 13635-13697 cr_media                com.distech.eclypsecontrol           W  registerBluetoothIntentsIfNeeded: Requires BLUETOOTH permission

Page stays blank

I will create a minimal repo to reproduce and create an issue on GitHub, this seems a bit more problematic than just failing to load my service worker.

EDIT
Ok, seems like an issue with my App on Chrome, even on desktop.
Testing with vite preview, loads in Safari, but not Chrome and Firefox.

Most likely not related to Capacitor at all.

1 Like

Ok, it might be capacitor related, but the issue occurred because I use GitHub - aparajita/capacitor-secure-storage: Secure, flexible storage for Capacitor apps using iOS Keychain and Android Keystore. to save my Redux store (user preferences) and I loaded the initial state using a top level await like so:

import { SecureStorage } from '@aparajita/capacitor-secure-storage';
const preloadedState = await SecureStorage.get(STORAGE_KEY);

Which would then be used to set up my store

export const store = configureStore({
  reducer: combineReducers(rootReducer),
  // Restore the previously persisted state
  preloadedState: preloadedState,
});

I have now switched to a better approach by making my store setup async and now looking at GitHub - rt2zz/redux-persist: persist and rehydrate a redux store to handle it more cleanly.

I’m not exactly sure why it worked fine in Safari and when the Capacitor server was pointed to my vite dev server (live reload), but I’m suspecting it’s because of how plugins are loaded in Capacitor using a Proxy object. capacitor-secure-storage/src/index.ts at f40f5cae7e9a5fac3c387372dfd09c9ea65095e6 · aparajita/capacitor-secure-storage · GitHub

It’s just very odd that it produced no errors and just died when trying to fetch the data on app start.

Hopefully this helps someone else in the future :sweat_smile:

1 Like