diff --git a/client/android/app/src/main/java/com/liketrek/trek/TrekShellPlugin.java b/client/android/app/src/main/java/com/liketrek/trek/TrekShellPlugin.java index b88ddfb42..7990514c1 100644 --- a/client/android/app/src/main/java/com/liketrek/trek/TrekShellPlugin.java +++ b/client/android/app/src/main/java/com/liketrek/trek/TrekShellPlugin.java @@ -556,7 +556,7 @@ public class TrekShellPlugin extends Plugin { if (path == null || path.isEmpty()) continue; String clean = path.startsWith("/") ? path.substring(1) : path; keep.add(clean); - if (TrekOfflineShell.hasFile(context, clean)) continue; + if (clean.contains("/") && TrekOfflineShell.hasFile(context, clean)) continue; byte[] data = download(server, clean, null); if (data == null || !TrekOfflineShell.saveFile(context, clean, data)) complete = false; } diff --git a/client/src/native/offlineShell.test.ts b/client/src/native/offlineShell.test.ts index 8d6f85aa1..548a0dfaf 100644 --- a/client/src/native/offlineShell.test.ts +++ b/client/src/native/offlineShell.test.ts @@ -116,6 +116,30 @@ describe('keepOfflineShellFresh', () => { expect(files).toContain('assets/AtlasPage-99887766.js'); }); + it('on Android also hands over the classic scripts the start page loads from the root', async () => { + vi.mocked(TrekShell.refreshOfflineCopy).mockResolvedValue({ saved: true, complete: true }); + document.head.innerHTML = [ + '', + '', + '', + '', + '', + ].join(''); + const fetchImpl = vi.fn(async () => new Response(JSON.stringify(manifest))) as unknown as typeof fetch; + + try { + await keepOfflineShellFresh(fetchImpl, 'en', true); + } finally { + document.head.innerHTML = ''; + } + + const { files } = vi.mocked(TrekShell.refreshOfflineCopy).mock.calls[0][0] as { files: string[] }; + expect(files).toContain('theme-boot.js'); + expect(files).toContain('shell-guard.js'); + expect(files.filter((f) => f === 'assets/index-a1b2c3d4.js')).toHaveLength(1); + expect(files.some((f) => f.includes('plugins') || f.includes('tracker'))).toBe(false); + }); + it('on Android asks again on the next launch while the store is incomplete', async () => { vi.mocked(TrekShell.refreshOfflineCopy).mockResolvedValue({ saved: true, complete: false }); const fetchImpl = vi.fn(async () => new Response(JSON.stringify(manifest))) as unknown as typeof fetch; diff --git a/client/src/native/offlineShell.ts b/client/src/native/offlineShell.ts index 09a715a76..f278b8dc2 100644 --- a/client/src/native/offlineShell.ts +++ b/client/src/native/offlineShell.ts @@ -51,6 +51,22 @@ export function manifestFiles(manifest: unknown, language?: string): string[] { return [...files]; } +/** + * The classic scripts the start page loads from the server's root + * (theme-boot.js, shell-guard.js). They block the first paint and come from + * the public folder, which the build's manifest does not list. + */ +export function pageScripts(doc: Document = document): string[] { + const files: string[] = []; + doc.querySelectorAll('script[src]').forEach((script) => { + if (script.type === 'module') return; + const url = new URL(script.getAttribute('src') ?? '', doc.baseURI); + const path = url.pathname.slice(1); + if (url.origin === window.location.origin && path && !path.includes('/')) files.push(path); + }); + return files; +} + /** True when every file was fetched. */ async function warmCache(files: string[], fetchFile: (path: string) => Promise): Promise { const queue = [...files]; @@ -98,7 +114,7 @@ export async function keepOfflineShellFresh( const build = `${[...files].sort((a, b) => a.localeCompare(b)).find((f) => /index-[^/]+\.js$/.test(f)) ?? files.length}:${language ?? 'en'}`; if (nativeStore) { // The native side fetches only what its store lacks, and keeps the start page fresh each time. - const { complete } = await TrekShell.refreshOfflineCopy({ files }); + const { complete } = await TrekShell.refreshOfflineCopy({ files: [...new Set([...files, ...pageScripts()])] }); if (complete) rememberWarmed(build); return; }