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

[Feature]: Customizable "Webpage not available" screen #7536

Closed
1 of 3 tasks
Alarson93 opened this issue Jun 26, 2024 · 5 comments
Closed
1 of 3 tasks

[Feature]: Customizable "Webpage not available" screen #7536

Alarson93 opened this issue Jun 26, 2024 · 5 comments
Labels

Comments

@Alarson93
Copy link

Description

I would like to be able to provide a custom screen for when web pages are not available.

Platforms

  • iOS
  • Android
  • Web

Request or proposed solution

A coworker occasionally sees a "Webpage not available" screen while using our Android Capacitor app. While the particular cause still needs to be investigated, the issue relevant here is the standard error screen does not look very good nor is there a way to refresh the page. So - a user that arrives at this screen does not have any path to recovery other than killing and reopening the app.

We are also able to reproduce the issue in airplane mode.

I've been asked to find a way to customize it. I could start overriding things, but issue #2751 seems to suggest that this is a bad idea. Is there an existing way to customize this screen? If not, could such a feature be added in the future?

Thanks!

Webpage Not Available

Alternatives

No response

Additional Information

No response

@Alarson93
Copy link
Author

I have found a workaround.

I created a custom WebViewListener that - upon receiving an error - navigates the user to a custom error screen:

class CustomListener(private val activity: WebPortalActivity): WebViewListener() {

    private var hasHandledError = false

    override fun onReceivedError(webView: WebView?) {
        super.onReceivedError(webView)

        // This conditional check prevents multiple errors from triggering multiple navigations
        if (!hasHandledError) {
            hasHandledError = true
            val intent = Intent(activity, WebViewErrorActivity::class.java)
            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
            activity.startActivity(intent)
        }
    }

}

The custom error screen basically says that something went wrong and it provides a "Return home" button. Tapping the button will navigate the user to a new Bridge Activity, giving them a fresh start without needing to kill the app. If the original issue persists (e.g. they still lack a data connect), the resulting error will simply navigate them back to our custom error screen.

Unfortunately, we cannot give the user any meaningful message about what went wrong because the WebViewListener does not pass the error forward. That's fine for our immediate use case, but I think it would be polite to provide an error in the onReceivedError function of WebViewListener.

@jcesarmobile
Copy link
Member

This is already possible, check errorPath inside server object
https://capacitorjs.com/docs/config

@jcesarmobile jcesarmobile closed this as not planned Won't fix, can't repro, duplicate, stale Jul 1, 2024
@Alarson93
Copy link
Author

Thanks for sharing!

Adding errorPath to the config certainly influences things. For example, I have the following server config:

  "server": {
    "url": "http://10.0.2.2:3000/", // I'm running a local NextJS / React instance
    "errorPath": "error.html"
  },

When I recreate the state mentioned above, I am brought to the "Webpage not available" screen with the offending URL being http://localhost/error.html. This is a little surprising as I expected to see the same domain (i.e. 10.0.2.2:3000) or, preferably, have the error page read directly from an HTML file compiled in the Android project (as the JS/TS of our frontend is not compiled into our apps).

@jcesarmobile
Copy link
Member

errorPath is the path to a local file in your app, so exactly what you say here

preferably, have the error page read directly from an HTML file compiled in the Android project

If you are using server.url and there is no internet connection it wouldn't be able to load a remote error page as there is no internet connection to load such page. So it can't use the same remote domain, it uses the local url.

@Alarson93
Copy link
Author

Great! I'm glad to hear it points to a local file.

Regarding the error I see, perhaps my errorPath is wrong and that is why I'm seeing the "Webpage not available" for http://localhost/error.html. What is the errorPath relative to?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants