All articles

Tutorial

Custom Offline Experiences and Network Detection in TWAs

September 4, 2026 · 9 min read

When a user downloads an application from the Google Play Store, they expect it to launch instantly, regardless of their current network connectivity. For standard native Android applications, launching without an active internet connection usually results in a clean, branded screen explaining that the device is offline. For Trusted Web Activities, however, poor handling of network drops can result in the standard Chromium network error screen, complete with the offline dinosaur. This instantly breaks the illusion of a native application and looks unprofessional.

Because a TWA is powered directly by the system browser engine, you have access to robust modern web standards to handle connectivity changes. By combining a highly resilient Service Worker with the browser Network Information API, you can build a seamless offline state. This guide outlines how to prevent browser-native error screens, build custom offline landing pages, and handle network transitions dynamically inside your Android wrapper.

The Core Challenge of TWA Offline Rendering

In a standard web browser, when a server fails to respond due to lack of signal, the browser displays its own default error interface. Because a TWA is technically launching a specialized browser window, it will fallback to that exact same default interface unless your web application actively intercept the request. The native Android wrapper itself does not cache your web page files; that task is delegated entirely to the browser instance powering your TWA.

To guarantee your application launches when offline, you must ensure that your root URL and all primary assets are cached on the user device beforehand. This is the domain of your Service Worker. If your Service Worker is not properly installed and activated, or if it does not use a robust fallback strategy, the initial launch will fail the moment the device goes offline.

Implementing a Resilient Service Worker Caching Strategy

To avoid the dreaded offline dinosaur page, your Service Worker must intercept fetch requests and serve cached files when the network is unreachable. The most reliable pattern for a TWA is a Network-First strategy with a cached page fallback. This ensures that users always receive the newest updates when online, but have a functional, custom offline experience when they lose connection.

Your Service Worker should cache a specific offline fallback HTML page during its installation phase. This file must be self-contained, meaning it contains all its styles and scripting inline to avoid requiring additional network requests to render. Below is an example of how your Service Worker should manage this fallback behavior:

const CACHE_NAME = 'twa-offline-cache-v1';
const OFFLINE_URL = '/offline.html';

self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(CACHE_NAME).then((cache) => {
return cache.add(new Request(OFFLINE_URL, { cache: 'reload' }));
})
);
});

self.addEventListener('fetch', (event) => {
if (event.request.mode === 'navigate') {
event.respondWith(
fetch(event.request).catch(() => {
return caches.open(CACHE_NAME).then((cache) => {
return cache.match(OFFLINE_URL);
});
})
);
}
});

In this architecture, whenever the user attempts to navigate to a new screen while offline, the catch block intercepts the failed fetch request and immediately serve your custom offline HTML page. This keeps the user within your branded environment and allows you to display a helpful message instead of a generic browser error.

Detecting Real-Time Network Transitions

Handling the offline launch is only half the battle. Your application should also gracefully handle transitions when a user is actively using the app and suddenly passes through a tunnel or loses cellular coverage. In these scenarios, throwing a full-page error is disruptive. Instead, you can use the browser network status API to trigger temporary in-app banners.

By listening to the online and offline events on the window object, your web application can dynamically update its user interface. When the network drops, you can display a non-intrusive warning bar letting the user know they are offline and that certain actions may be limited, while still allowing them to browse already-cached content.

Comparing Offline Behaviors in TWAs

To understand the importance of implementing a comprehensive caching and detection strategy, consider how different configurations handle connection dropouts:

Configuration TypeLaunch Experience OfflineActive Usage Connection DropVisual Presentation
No Service WorkerFails immediatelyDisplays browser error screenChromium error page (Dinosaur)
Basic Service Worker (No fallback page)Launches shell, fails on dynamic pathsStalls on loading indicatorsIncomplete UI, broken states
Resilient Service Worker (With fallback page)Launches custom offline page immediatelyGraceful warning banner displayedBranded, professional offline screen

As illustrated, relying on the native platform defaults results in a poor application presentation. Implementing a robust web-based caching system is essential to meeting the quality guidelines required of applications distributed on the Google Play Store.

Configuring Native TWA Offline Fallbacks in the Android Wrapper

While a Service Worker is the ideal solution, there is a very rare edge case: when a user launches your app for the absolute first time immediately after installation without any internet connection. In this specific scenario, the Service Worker has never had a chance to install or run. Therefore, it cannot intercept the navigation request or serve your offline fallback file.

To mitigate this, modern TWA construction platforms and libraries built on Google's android-browser-helper support a basic native offline fallback screen. If the initial connection validation fails on the first launch, the Android wrapper can display a native error screen. This screen can be configured with your brand colors and matching typography, ensuring that even if the app fails to open your web origin, it still feels like a cohesive native application rather than a broken browser shortcut.

Testing Your Offline Implementation

Before publishing your application to the Google Play Store, you should thoroughly test its network transition behavior. You can do this easily by connecting your Android test device to your workstation and opening Chrome DevTools remote debugging.

Navigate to the Application tab in DevTools, inspect your Service Worker, and verify that your offline page is successfully stored in the Cache Storage. Once confirmed, use the DevTools network emulation panel to select the offline mode. Attempt to navigate within your TWA. If your Service Worker is configured correctly, your custom offline landing page will immediately appear, preserving your brand identity and providing a smooth path for the user to retry their connection.

Ready to ship your Android app?

Paste your PWA URL, get a signed APK and a Google Play ready AAB in minutes.

Build my app