Ushka

ushka.web.pwa

Progressive Web App support, configured in [pwa] of ushka.toml.

Switch it on and add the tags to your layout's <head>:

[pwa]
enable = true
name = "My App"
theme_color = "#1b1720"
icons = ["/static/icon-192.png", "/static/icon-512.png"]

{{ pwa() }}

Ushka then serves a web app manifest (/manifest.webmanifest), a service worker (/sw.js), a small script that registers it (/pwa.js) and an offline page (/offline). Nothing needs inline JavaScript, so the default Content-Security-Policy is untouched.

The service worker is deliberately conservative, because a worker that caches pages can show one user's private page to the next person on a shared device:

  • page navigations always go to the network; only when it fails is the offline page shown (pages are never cached);
  • only GET requests to your own origin are handled;
  • the files under [static] (and any cache_paths) are cached and refreshed in the background;
  • precache lists the extra URLs fetched when the worker installs.

Change cache_version to drop every cache on the next visit.

In this module

def icons()
def manifest()The web app manifest, from [pwa] with the [seo]/[app] names as fallback.
def service_worker()The service worker script (see the module docstring for what it does).
def register_script()
def offline_page()
def pwa(){{ pwa() }} in <head>: manifest link, theme colour, icons and the registration script.
def install()Registers the manifest, worker, register script and offline page when [pwa] enable is true.
class Pwaapp.use(Pwa(name="Shop", icons=["/static/icon-512.png"], theme_color="#1b1720")).

def icons

def icons(config: Any) -> list[dict[str, str]]

def manifest

def manifest(config: Any) -> dict[str, Any]

The web app manifest, from [pwa] with the [seo]/[app] names as fallback.

def service_worker

def service_worker(config: Any) -> str

The service worker script (see the module docstring for what it does).

def register_script

def register_script(config: Any) -> str

def offline_page

def offline_page(config: Any) -> str

def pwa

def pwa(context) -> Markup

{{ pwa() }} in <head>: manifest link, theme colour, icons and the registration script.

def install

def install(app: Ushka) -> None

Registers the manifest, worker, register script and offline page when [pwa] enable is true.

class Pwa

app.use(Pwa(name="Shop", icons=["/static/icon-512.png"], theme_color="#1b1720")).

Using it turns the PWA on (no enable = true); ushka.toml and the environment still win, so enable = false there switches it off. **options are any other keys of [pwa].

install

def install(app: Ushka) -> None