Lesson 7 路 Apps for your phone

From website to phone app

Learn how a website becomes an installable app that works offline.

About 20 minutes

Tab indents. Press Escape, then Tab, to leave the editor.

You've built a game that runs in a browser. What if it could sit on your phone's home screen with its own icon, open full screen with no address bar, and still work with no signal? That's a Progressive Web App (PWA): a normal website plus two extra files that let the browser treat it like an app. No app store needed, and it's the same HTML, CSS and JavaScript you already know.

Web app or native app?

There are two main ways to make a phone app:

  • Native apps are written in the phone's own languages, like Swift for iPhone or Kotlin for Android, and are usually installed from the App Store or Google Play. They get the deepest access to the phone's hardware.
  • Web apps are websites. A PWA is one that can be installed from the browser and works offline. One codebase runs on phones, tablets and laptops, and updating it is as simple as updating your site.

Neither is "better". Lots of products ship both. For a game, a tool or a guide you want to share with friends, a PWA gets you there with what you already know.

A PWA needs three things:

  1. HTTPS. Browsers only allow the app features on secure sites (localhost counts as secure while you're developing).
  2. A web app manifest: a small JSON file describing the app's name, icon and colors.
  3. A service worker: a script that can save files and serve them when there's no network.

The manifest: your app's ID card

The manifest is a JSON file, often called manifest.webmanifest, linked from your page's <head>:

<link rel="manifest" href="/manifest.webmanifest">

Here's a complete one:

{
  "name": "Target Rush",
  "short_name": "Target",
  "start_url": "/",
  "display": "standalone",
  "background_color": "#0b132b",
  "theme_color": "#5bc0be",
  "icons": [
    { "src": "/icons/icon-192.png", "sizes": "192x192", "type": "image/png" },
    { "src": "/icons/icon-512.png", "sizes": "512x512", "type": "image/png" }
  ]
}
  • name is the full name, shown in install dialogs and on the splash screen. short_name is used where space is tight, like under the home-screen icon, so keep it short (around 12 characters or fewer).
  • start_url is the page that opens when you tap the icon.
  • display: "standalone" opens it in its own window without the browser's address bar, so it feels like an app.
  • background_color fills the splash screen while the app loads; theme_color tints things like the status bar or title bar.
  • icons lists image files. Chrome wants at least a 192px and a 512px icon.

Chrome and Edge offer to install a site when it's served over HTTPS and its manifest has a name, those icons, a start_url and a display mode like standalone. On iPhone and iPad, anyone can add a site from Safari's Share menu with Add to Home Screen, and the manifest controls how it looks and opens.

Challenge

Design your app's home-screen identity

The preview draws a pretend home-screen icon and splash screen from the manifest object, plus the JSON your real manifest.webmanifest would contain. Fill in the missing fields, pressing Run to see each change:

  1. A short_name of 12 characters or fewer (watch the label under the icon).
  2. A start_url and display: 'standalone'.
  3. theme_color and background_color (any CSS color, like '#5bc0be').
  4. An icons array with a 192x192 and a 512x512 icon. Each is an object with src, sizes and type. (In a real app the src files would be PNGs; here only the names matter.)
Console
  1. Nothing logged yet.
  • The app has a name and a short_name of 12 characters or fewer
  • It has a start_url and opens standalone
  • The theme and background colors are real CSS colors
  • There are 192x192 and 512x512 icons, each with a src

Service workers: your app's offline brain

A service worker is a JavaScript file that the browser runs separately from your page. Once it's installed, it sits between your page and the network: every request the page makes (for HTML, CSS, images, data) goes through it first, and the service worker decides what to answer with. It can go to the network, or it can answer instantly from the Cache Storage it saved earlier, which works with no connection at all.

Step by step

A service worker's life

Register it from your page

Your page asks the browser to install the worker. Checking first means older browsers just skip it.

if ('serviceWorker' in navigator) {
  navigator.serviceWorker.register('/sw.js')
}
1 / 5
Question

Quick check

Your installed app opens with the right icon and no address bar, but shows the browser's "no internet" page in airplane mode. What's missing?
Pick an answer.

Picking a caching strategy

Cache first is perfect for files that rarely change, but bad for things that need to be fresh, like a leaderboard. There are a few standard strategies:

  • Cache first: fast and works offline, but can be stale until you update the cache.
  • Network first: fresh when online, falls back to the cache when offline. Slow on a bad connection, because it waits for the network first.
  • Stale-while-revalidate: answers from the cache straight away, and fetches a fresh copy in the background for next time.
  • Network only: never cached. Fine for things that make no sense offline.

Try each one on a fast network, a slow one and offline, with and without a cached copy.

Lab

Caching strategy simulator

A service worker decides where each response comes from. Pick a strategy and a network, then send requests.

Strategy

Answers from the cache when it can; only uses the network on a miss. Fast and offline-proof, but can serve old copies forever unless the cache is versioned.

Network
Server has
v1
Cache holds
nothing
Requests

Send a request to see where the response comes from.

Timings are illustrative, not measured.

Question

Quick check

Your game shows a high-score leaderboard from a server. Online, it should always be up to date. Offline, the last scores you saw are better than nothing. Which strategy fits?
Pick an answer.

Recap

  • A PWA is a website that can be installed and work offline: HTTPS, a manifest and a service worker.
  • The manifest gives the app its name, short name, icons, colors, start page and display mode.
  • A service worker sits between the page and the network and can answer requests from Cache Storage.
  • Its lifecycle is register, install (cache the app shell), activate (clean old caches), then fetch (answer requests).
  • Choose a caching strategy per kind of content: cache first for files that rarely change, network first for things that must be fresh.

Finished this lesson?

Mark it complete to track your progress on this device.