Wallos: Open-source, self-hostable personal subscription tracker. Visualize your recurring expenses, manage your budget, and save money.
fix(pwa): offline mode, icons and background
Miguel Ribeiro5ffa8417293d
diff --git a/includes/header.php b/includes/header.phpindex 942efec..f3b66ab 100644--- a/includes/header.php+++ b/includes/header.php@@ -91,7 +91,7 @@ $mobileNavigation = $settings['mobile_nav'] ? "mobile-navigation" : "";<link rel="apple-touch-icon" href="images/icon/apple-touch-icon.png"><link rel="apple-touch-icon" sizes="152x152" href="images/icon/apple-touch-icon-152.png"><link rel="apple-touch-icon" sizes="180x180" href="images/icon/apple-touch-icon-180.png">- <link rel="manifest" href="manifest.json" crossorigin="use-credentials">+ <link rel="manifest" href="manifest.php" crossorigin="use-credentials"><link rel="stylesheet" href="styles/theme.css?<?= $version ?>"><link rel="stylesheet" href="styles/styles.css?<?= $version ?>"><link rel="stylesheet" href="styles/dark-theme.css?<?= $version ?>" id="dark-theme" <?= $theme != "dark" ? "disabled" : "" ?>>diff --git a/login.php b/login.phpindex 95a5f91..6a47b11 100644--- a/login.php+++ b/login.php@@ -312,7 +312,7 @@ if (isset($_GET['error'])) {<link rel="apple-touch-icon" href="images/icon/apple-touch-icon.png"><link rel="apple-touch-icon" sizes="152x152" href="images/icon/apple-touch-icon-152.png"><link rel="apple-touch-icon" sizes="180x180" href="images/icon/apple-touch-icon-180.png">- <link rel="manifest" href="manifest.json">+ <link rel="manifest" href="manifest.php"><link rel="stylesheet" href="styles/theme.css?<?= $version ?>"><link rel="stylesheet" href="styles/login.css?<?= $version ?>"><link rel="stylesheet" href="styles/themes/red.css?<?= $version ?>" id="red-theme" <?= $colorTheme != "red" ? "disabled" : "" ?>>diff --git a/manifest.json b/manifest.jsondeleted file mode 100644index 9f746d4..0000000--- a/manifest.json+++ /dev/null@@ -1,126 +0,0 @@-{- "short_name": "Wallos",- "name": "Wallos - Subscription Tracker",- "icons": [- {- "src": "images/icon/android-chrome-192x192.png",- "type": "image/png",- "sizes": "192x192"- },- {- "src": "images/icon/android-chrome-512x512.png",- "type": "image/png",- "sizes": "512x512"- },- {- "src": "images/icon/maskable_icon_x192.png",- "type": "image/png",- "sizes": "192x192",- "purpose": "maskable"- },- {- "src": "images/icon/maskable_icon_x512.png",- "type": "image/png",- "sizes": "512x512",- "purpose": "maskable"- }- ],- "start_url": "/",- "id": "com.wallos.app",- "shortcuts": [- {- "name": "Dashboard",- "short_name": "Dashboard",- "description": "View your dashboard",- "url": "index.php",- "icons": [- {- "src": "images/siteicons/pwa/dashboard.png",- "sizes": "96x96"- }- ]- },- {- "name": "Subscriptions",- "short_name": "Subscriptions",- "description": "View your subscriptions",- "url": "subscriptions.php",- "icons": [- {- "src": "images/siteicons/pwa/subscriptions.png",- "sizes": "96x96"- }- ]- },- {- "name": "Calendar",- "short_name": "Calendar",- "description": "View your calendar",- "url": "calendar.php",- "icons": [- {- "src": "images/siteicons/pwa/calendar.png",- "sizes": "96x96"- }- ]- },- {- "name": "Stats",- "short_name": "Stats",- "description": "View your statistics",- "url": "stats.php",- "icons": [- {- "src": "images/siteicons/pwa/stats.png",- "sizes": "96x96"- }- ]- },- {- "name": "Settings",- "short_name": "Settings",- "description": "Change your settings",- "url": "settings.php",- "icons": [- {- "src": "images/siteicons/pwa/settings.png",- "sizes": "96x96"- }- ]- },- {- "name": "About",- "short_name": "About",- "description": "More info about Wallos",- "url": "about.php",- "icons": [- {- "src": "images/siteicons/pwa/about.png",- "sizes": "96x96"- }- ]- }- ],- "screenshots": [- {- "src": "images/screenshots/desktop.png",- "sizes": "1000x750",- "type": "image/png",- "form_factor": "wide"- },- {- "src": "images/screenshots/mobile.png",- "sizes": "600x1000",- "type": "image/png"- }- ],- "background_color": "#FFFFFF",- "display": "standalone",- "scope": "/",- "theme_color": "#FFFFFF",- "description": "Wallos is a personal subscription tracker that helps you keep track of your subscriptions and save money.",- "orientation": "portrait-primary",- "display_override": [- "window-controls-overlay"- ]-}\ No newline at end of filediff --git a/manifest.php b/manifest.phpnew file mode 100644index 0000000..78a909e--- /dev/null+++ b/manifest.php@@ -0,0 +1,135 @@+<?php++/**+ * Serves the PWA manifest dynamically so theme_color/background_color match+ * the user's actual theme instead of being hardcoded white - installed PWAs+ * (Android WebAPKs in particular) use the manifest's theme_color for the+ * status bar/header chrome, independent of the page's own <meta+ * name="theme-color"> tag.+ *+ * No login is required to read this (it's linked from the pre-auth pages+ * too), so theme comes from the same "theme"/"inUseTheme" cookies those+ * pages already use, not from a per-user DB setting.+ */++require_once __DIR__ . '/includes/theme_helpers.php';++$theme = sanitize_theme_mode($_COOKIE['theme'] ?? null);+if ($theme === 'automatic') {+ $theme = sanitize_resolved_theme($_COOKIE['inUseTheme'] ?? null);+}+$themeColor = $theme === 'light' ? '#FFFFFF' : '#12151C';++$manifest = [+ 'short_name' => 'Wallos',+ 'name' => 'Wallos - Subscription Tracker',+ 'icons' => [+ [+ 'src' => 'images/icon/android-chrome-192x192.png',+ 'type' => 'image/png',+ 'sizes' => '192x192',+ ],+ [+ 'src' => 'images/icon/android-chrome-512x512.png',+ 'type' => 'image/png',+ 'sizes' => '512x512',+ ],+ [+ 'src' => 'images/icon/maskable_icon_x192.png',+ 'type' => 'image/png',+ 'sizes' => '192x192',+ 'purpose' => 'maskable',+ ],+ [+ 'src' => 'images/icon/maskable_icon_x512.png',+ 'type' => 'image/png',+ 'sizes' => '512x512',+ 'purpose' => 'maskable',+ ],+ ],+ 'start_url' => '/',+ 'id' => 'com.wallos.app',+ 'shortcuts' => [+ [+ 'name' => 'Dashboard',+ 'short_name' => 'Dashboard',+ 'description' => 'View your dashboard',+ 'url' => 'index.php',+ 'icons' => [+ ['src' => 'images/siteicons/pwa/dashboard.png', 'type' => 'image/png', 'sizes' => '96x96'],+ ],+ ],+ [+ 'name' => 'Subscriptions',+ 'short_name' => 'Subscriptions',+ 'description' => 'View your subscriptions',+ 'url' => 'subscriptions.php',+ 'icons' => [+ ['src' => 'images/siteicons/pwa/subscriptions.png', 'type' => 'image/png', 'sizes' => '96x96'],+ ],+ ],+ [+ 'name' => 'Calendar',+ 'short_name' => 'Calendar',+ 'description' => 'View your calendar',+ 'url' => 'calendar.php',+ 'icons' => [+ ['src' => 'images/siteicons/pwa/calendar.png', 'type' => 'image/png', 'sizes' => '96x96'],+ ],+ ],+ [+ 'name' => 'Stats',+ 'short_name' => 'Stats',+ 'description' => 'View your statistics',+ 'url' => 'stats.php',+ 'icons' => [+ ['src' => 'images/siteicons/pwa/stats.png', 'type' => 'image/png', 'sizes' => '96x96'],+ ],+ ],+ [+ 'name' => 'Settings',+ 'short_name' => 'Settings',+ 'description' => 'Change your settings',+ 'url' => 'settings.php',+ 'icons' => [+ ['src' => 'images/siteicons/pwa/settings.png', 'type' => 'image/png', 'sizes' => '96x96'],+ ],+ ],+ [+ 'name' => 'About',+ 'short_name' => 'About',+ 'description' => 'More info about Wallos',+ 'url' => 'about.php',+ 'icons' => [+ ['src' => 'images/siteicons/pwa/about.png', 'type' => 'image/png', 'sizes' => '96x96'],+ ],+ ],+ ],+ 'screenshots' => [+ [+ 'src' => 'images/screenshots/desktop.png',+ 'sizes' => '1000x750',+ 'type' => 'image/png',+ 'form_factor' => 'wide',+ ],+ [+ 'src' => 'images/screenshots/mobile.png',+ 'sizes' => '600x1000',+ 'type' => 'image/png',+ ],+ ],+ 'background_color' => $themeColor,+ 'display' => 'standalone',+ 'scope' => '/',+ 'theme_color' => $themeColor,+ 'description' => 'Wallos is a personal subscription tracker that helps you keep track of your subscriptions and save money.',+ 'orientation' => 'portrait-primary',+ 'display_override' => ['window-controls-overlay'],+];++if (!headers_sent()) {+ header('Content-Type: application/manifest+json; charset=utf-8');+}+echo json_encode($manifest, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);++?>diff --git a/passwordreset.php b/passwordreset.phpindex cc93d78..600c7f8 100644--- a/passwordreset.php+++ b/passwordreset.php@@ -216,7 +216,7 @@ if (isset($_POST['password']) && $_POST['password'] != "" && isset($_POST['confi<link rel="apple-touch-icon" href="images/icon/apple-touch-icon.png"><link rel="apple-touch-icon" sizes="152x152" href="images/icon/apple-touch-icon-152.png"><link rel="apple-touch-icon" sizes="180x180" href="images/icon/apple-touch-icon-180.png">- <link rel="manifest" href="manifest.json">+ <link rel="manifest" href="manifest.php"><link rel="stylesheet" href="styles/theme.css?<?= $version ?>"><link rel="stylesheet" href="styles/login.css?<?= $version ?>"><link rel="stylesheet" href="styles/themes/red.css?<?= $version ?>" id="red-theme" <?= $colorTheme != "red" ? "disabled" : "" ?>>@@ -226,7 +226,7 @@ if (isset($_POST['password']) && $_POST['password'] != "" && isset($_POST['confi<link rel="stylesheet" href="styles/font-awesome.min.css"><link rel="stylesheet" href="styles/barlow.css"><link rel="stylesheet" href="styles/login-dark-theme.css?<?= $version ?>" id="dark-theme" <?= $theme == "light" ? "disabled" : "" ?>>- <script type="text/javascript" src="scripts/auth-theme.js?<?= $version ?>"></script>+ <script type="text/javascript" src="scripts/auth-theme.js?<?= $version ?>"></script><script type="text/javascript" src="scripts/password-toggle.js?<?= $version ?>"></script></head>diff --git a/registration.php b/registration.phpindex d3ed02d..2a761be 100644--- a/registration.php+++ b/registration.php@@ -337,7 +337,7 @@ if (isset($_POST['username'])) {<link rel="apple-touch-icon" href="images/icon/apple-touch-icon.png"><link rel="apple-touch-icon" sizes="152x152" href="images/icon/apple-touch-icon-152.png"><link rel="apple-touch-icon" sizes="180x180" href="images/icon/apple-touch-icon-180.png">- <link rel="manifest" href="manifest.json">+ <link rel="manifest" href="manifest.php"><link rel="stylesheet" href="styles/theme.css?<?= $version ?>"><link rel="stylesheet" href="styles/login.css?<?= $version ?>"><link rel="stylesheet" href="styles/themes/red.css?<?= $version ?>" id="red-theme" <?= $colorTheme != "red" ? "disabled" : "" ?>>diff --git a/service-worker.js b/service-worker.jsindex fb739e4..f9eb8a3 100644--- a/service-worker.js+++ b/service-worker.js@@ -1,9 +1,11 @@-const STATIC_CACHE = 'static-cache-v5';+const STATIC_CACHE = 'static-cache-v7';const PAGES_CACHE = 'pages-cache-v1';const LOGOS_CACHE = 'logos-cache-v2';+// manifest.php is intentionally not precached here: it's per-user (theme+// cookie dependent), so it falls through to the network-first handler below+// instead of being served stale from a cache-first bucket forever.const staticAssets = [- 'manifest.json','styles/styles.css','styles/theme.css','styles/dark-theme.css',@@ -165,17 +167,23 @@ const pagesToPrefetch = ['admin.php',];-// Install: cache static assets only+// Install: cache static assets in small batches. Firing all ~150 fetches at+// once starves large files (apexcharts is 600KB) of connections on HTTP/1.1+// origins, and a dropped one is silently skipped - see the static-asset+// fetch handler, which backfills anything that slips through here.self.addEventListener('install', function (event) {event.waitUntil(- caches.open(STATIC_CACHE).then(function (cache) {- return Promise.allSettled(- staticAssets.map(url =>- fetch(url).then(response => {- if (response.ok) cache.put(url, response);- }).catch(() => {}) // silently skip missing files- )- );+ caches.open(STATIC_CACHE).then(async function (cache) {+ const BATCH_SIZE = 12;+ for (let i = 0; i < staticAssets.length; i += BATCH_SIZE) {+ await Promise.allSettled(+ staticAssets.slice(i, i + BATCH_SIZE).map(url =>+ fetch(url).then(response => {+ if (response.ok) return cache.put(url, response);+ }).catch(() => {}) // silently skip missing files+ )+ );+ }}));self.skipWaiting();@@ -248,10 +256,28 @@ self.addEventListener('fetch', function (event) {return;}- // Static assets: cache-first (they only change on deploy)+ // Static assets: cache-first (they only change on deploy). ignoreSearch+ // because the pages request these with a "?<version>" cache-buster, while+ // they're precached under the bare path - without it every asset misses+ // the cache and the page renders unstyled offline.+ //+ // On a miss, fetch AND store the result: the install step fires ~150+ // parallel fetches and any that fail (a large file like apexcharts, a+ // dropped connection) are silently skipped, so without this backfill a+ // once-missed asset would never get cached and its feature (e.g. the+ // stats charts) would stay broken offline forever.if (staticAssets.some(asset => url.pathname.endsWith(asset))) {event.respondWith(- caches.match(request).then(response => response || fetch(request))+ caches.match(request, { ignoreSearch: true }).then(cached => {+ if (cached) return cached;+ return fetch(request).then(networkResponse => {+ if (networkResponse.ok) {+ const clone = networkResponse.clone();+ caches.open(STATIC_CACHE).then(cache => cache.put(request, clone));+ }+ return networkResponse;+ });+ }));return;}diff --git a/tests/cases/pwa_manifest_test.php b/tests/cases/pwa_manifest_test.phpnew file mode 100644index 0000000..adc523c--- /dev/null+++ b/tests/cases/pwa_manifest_test.php@@ -0,0 +1,125 @@+<?php+/*+ manifest.json used to hardcode theme_color/background_color to white,+ which an installed PWA (Android WebAPK in particular) uses for the status+ bar/splash screen independent of the page's own live theme - dark-theme+ users always got a white header there. manifest.php replaces the static+ file so those colors track the user's actual theme, resolved from the+ same "theme"/"inUseTheme" cookies the pre-auth pages already use (no+ login is required to read this file).+*/++/**+ * Runs manifest.php with the given cookies and returns the decoded output.+ *+ * @param array $cookies+ * @return array+ */+function render_manifest($cookies)+{+ $previousCookies = $_COOKIE;+ $_COOKIE = $cookies;++ ob_start();+ include WALLOS_ROOT . '/manifest.php';+ $output = ob_get_clean();++ $_COOKIE = $previousCookies;++ return json_decode($output, true);+}++wallos_test('the manifest is valid JSON with the expected content type', function () {+ ob_start();+ include WALLOS_ROOT . '/manifest.php';+ $output = ob_get_clean();++ $decoded = json_decode($output, true);+ assert_true($decoded !== null, 'manifest.php produces valid JSON');+ assert_same('Wallos', $decoded['short_name'] ?? null, 'the manifest content is otherwise unchanged');+});++wallos_test('theme_color and background_color track the theme cookie', function () {+ $light = render_manifest(['theme' => 'light']);+ $dark = render_manifest(['theme' => 'dark']);++ assert_same('#FFFFFF', $light['theme_color'], 'light theme is white');+ assert_same('#FFFFFF', $light['background_color'], 'and the background matches it');+ assert_same('#12151C', $dark['theme_color'], 'dark theme is dark, matching the header meta tag value');+ assert_same('#12151C', $dark['background_color'], 'and the background matches it');+});++wallos_test('automatic mode resolves from the inUseTheme cookie, not just "not light"', function () {+ $resolvedDark = render_manifest(['theme' => 'automatic', 'inUseTheme' => 'dark']);+ $resolvedLight = render_manifest(['theme' => 'automatic', 'inUseTheme' => 'light']);++ assert_same('#12151C', $resolvedDark['theme_color'], 'automatic + resolved dark is dark');+ assert_same('#FFFFFF', $resolvedLight['theme_color'], 'automatic + resolved light is light');+});++wallos_test('missing or invalid cookies default to light, same as the pre-auth pages', function () {+ $noCookie = render_manifest([]);+ $garbage = render_manifest(['theme' => '<script>alert(1)</script>']);++ assert_same('#FFFFFF', $noCookie['theme_color'], 'no cookie at all defaults to light');+ assert_same('#FFFFFF', $garbage['theme_color'], 'an invalid theme value is not trusted, falls back to light');+});++wallos_test('every shortcut icon declares its type, and the shortcut/icon set is unchanged', function () {+ $manifest = render_manifest(['theme' => 'light']);++ assert_same(6, count($manifest['shortcuts']), 'all six dashboard shortcuts are still present');+ foreach ($manifest['shortcuts'] as $shortcut) {+ foreach ($shortcut['icons'] as $icon) {+ assert_same('image/png', $icon['type'] ?? null, $shortcut['name'] . ' shortcut icon declares its type');+ }+ }+});++wallos_test('every page linking the manifest points at manifest.php, not the old static file', function () {+ $files = [+ 'includes/header.php',+ 'login.php',+ 'registration.php',+ 'passwordreset.php',+ 'verifyemail.php',+ 'totp.php',+ ];++ foreach ($files as $file) {+ $source = file_get_contents(WALLOS_ROOT . '/' . $file);+ assert_contains('href="manifest.php"', $source, $file . ' links the dynamic manifest');+ assert_not_contains('href="manifest.json"', $source, $file . ' does not still link the removed static file');+ }+});++wallos_test('the service worker does not cache-first the manifest forever', function () {+ $source = file_get_contents(WALLOS_ROOT . '/service-worker.js');++ assert_not_contains("'manifest.json'", $source,+ 'the old static manifest is not precached (it no longer exists)');+ assert_not_contains("'manifest.php'", $source,+ 'the dynamic, per-user manifest is not cache-first precached either - it must stay network-first so a theme change is reflected');+});++wallos_test('the service worker matches versioned static assets when offline', function () {+ $source = file_get_contents(WALLOS_ROOT . '/service-worker.js');++ // The pages request assets as "styles/styles.css?<version>" but they are+ // precached under the bare path, so the static-asset lookup must ignore+ // the query string or every asset misses the cache offline.+ $staticBranch = substr($source, strpos($source, 'Static assets: cache-first'));+ $staticBranch = substr($staticBranch, 0, strpos($staticBranch, 'PHP pages and everything else'));++ assert_contains('caches.match(request, { ignoreSearch: true })', $staticBranch,+ 'the static-asset lookup ignores the "?<version>" query string');+ assert_contains('cache.put(request, clone)', $staticBranch,+ 'a cache miss is backfilled, so an asset the install step dropped (e.g. apexcharts) still ends up cached');+});++wallos_test('the service worker installs static assets in batches, not all at once', function () {+ $source = file_get_contents(WALLOS_ROOT . '/service-worker.js');++ assert_contains('BATCH_SIZE', $source,+ 'the install step chunks its fetches so large files are not starved of connections');+});diff --git a/totp.php b/totp.phpindex e8cf7fa..6624cf9 100644--- a/totp.php+++ b/totp.php@@ -258,7 +258,7 @@ if (isset($_POST['one-time-code'])) {<link rel="apple-touch-icon" href="images/icon/apple-touch-icon.png"><link rel="apple-touch-icon" sizes="152x152" href="images/icon/apple-touch-icon-152.png"><link rel="apple-touch-icon" sizes="180x180" href="images/icon/apple-touch-icon-180.png">- <link rel="manifest" href="manifest.json">+ <link rel="manifest" href="manifest.php"><link rel="stylesheet" href="styles/theme.css?<?= $version ?>"><link rel="stylesheet" href="styles/login.css?<?= $version ?>"><link rel="stylesheet" href="styles/themes/red.css?<?= $version ?>" id="red-theme" <?= $colorTheme != "red" ? "disabled" : "" ?>>diff --git a/verifyemail.php b/verifyemail.phpindex 771c6dd..7f82bd1 100644--- a/verifyemail.php+++ b/verifyemail.php@@ -109,7 +109,7 @@ if (isset($_GET['email']) && isset($_GET['token'])) {<link rel="apple-touch-icon" href="images/icon/apple-touch-icon.png"><link rel="apple-touch-icon" sizes="152x152" href="images/icon/apple-touch-icon-152.png"><link rel="apple-touch-icon" sizes="180x180" href="images/icon/apple-touch-icon-180.png">- <link rel="manifest" href="manifest.json">+ <link rel="manifest" href="manifest.php"><link rel="stylesheet" href="styles/theme.css?<?= $version ?>"><link rel="stylesheet" href="styles/login.css?<?= $version ?>"><link rel="stylesheet" href="styles/themes/red.css?<?= $version ?>" id="red-theme" <?= $colorTheme != "red" ? "disabled" : "" ?>>