mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-23 21:33:19 +00:00
fix(android): keep the start page's boot scripts in the offline copy
theme-boot.js and shell-guard.js come from the public folder, which the build manifest does not list, so an offline start waited on the unreachable server for them before the first paint. They are now stored and refreshed on every launch.
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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 = [
|
||||
'<script src="/theme-boot.js"></script>',
|
||||
'<script src="/shell-guard.js"></script>',
|
||||
'<script type="module" src="/assets/index-a1b2c3d4.js"></script>',
|
||||
'<script src="/plugins/demo/widget.js"></script>',
|
||||
'<script src="https://elsewhere.example/tracker.js"></script>',
|
||||
].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;
|
||||
|
||||
@@ -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<HTMLScriptElement>('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<unknown>): Promise<boolean> {
|
||||
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user