mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-22 03:50:30 +00:00
fix(native): first visits no longer wait behind the offline warm-up, day rail follows the swipe
The iOS app pulls every file of a build into WebKit's cache so it can start offline. It did so four at a time at normal priority, for all 1,900 files of every new build including all 27 translations, and a page opened meanwhile queued behind it: right after a server update, a first visit to Vacay took five to seven seconds. The warm-up now runs two at a time at low priority, takes only the user's language and English, and walks a build once. The day rail in the trip header lit the new day only after the pager's snap had landed and the plan had re-rendered, a second or two after the swipe. The pager now reports the day a swipe heads for as soon as its page is the nearer one, and the rail and its pill move with the finger; the plan still switches when the snap lands.
This commit is contained in:
@@ -11,7 +11,16 @@ import { TrekShell } from './trekShell';
|
||||
*/
|
||||
|
||||
const MANIFEST_URL = '/asset-manifest.json';
|
||||
const CONCURRENCY = 4;
|
||||
/**
|
||||
* Few at a time and at low priority: the warm-up runs while the user already
|
||||
* moves through the app, and the page they open must never queue behind it.
|
||||
* Measured on a phone: four at normal priority held a first visit to Vacay
|
||||
* back by five to seven seconds right after a server update.
|
||||
*/
|
||||
const CONCURRENCY = 2;
|
||||
/** The build whose files are all in the cache; a launch on the same build skips the walk. */
|
||||
const WARMED_KEY = 'trek.offlineShellBuild';
|
||||
const LOCALE_KEY = /\/i18n\/([^/]+)\/index\.[cm]?js$/;
|
||||
|
||||
interface ManifestChunk {
|
||||
file?: string;
|
||||
@@ -19,10 +28,18 @@ interface ManifestChunk {
|
||||
assets?: string[];
|
||||
}
|
||||
|
||||
export function manifestFiles(manifest: unknown): string[] {
|
||||
/**
|
||||
* Every file of the build, without the translations of other languages: only
|
||||
* the user's own and English (the fallback) are needed offline, the other 25
|
||||
* would cost several megabytes per build for nothing.
|
||||
*/
|
||||
export function manifestFiles(manifest: unknown, language?: string): string[] {
|
||||
if (typeof manifest !== 'object' || manifest === null) return [];
|
||||
const keep = new Set(['en', language ?? 'en']);
|
||||
const files = new Set<string>();
|
||||
for (const chunk of Object.values(manifest as Record<string, ManifestChunk>)) {
|
||||
for (const [key, chunk] of Object.entries(manifest as Record<string, ManifestChunk>)) {
|
||||
const locale = LOCALE_KEY.exec(key)?.[1];
|
||||
if (locale && !keep.has(locale)) continue;
|
||||
if (typeof chunk?.file === 'string') files.add(chunk.file);
|
||||
for (const list of [chunk?.css, chunk?.assets]) {
|
||||
if (Array.isArray(list)) list.forEach((f) => typeof f === 'string' && files.add(f));
|
||||
@@ -31,24 +48,50 @@ export function manifestFiles(manifest: unknown): string[] {
|
||||
return [...files];
|
||||
}
|
||||
|
||||
async function warmCache(files: string[], fetchFile: (path: string) => Promise<unknown>): Promise<void> {
|
||||
/** True when every file was fetched. */
|
||||
async function warmCache(files: string[], fetchFile: (path: string) => Promise<unknown>): Promise<boolean> {
|
||||
const queue = [...files];
|
||||
let complete = true;
|
||||
const worker = async (): Promise<void> => {
|
||||
for (let path = queue.shift(); path !== undefined; path = queue.shift()) {
|
||||
try {
|
||||
await fetchFile(`/${path}`);
|
||||
} catch {
|
||||
// One missing chunk must not stop the rest; the next launch tries again.
|
||||
complete = false;
|
||||
}
|
||||
}
|
||||
};
|
||||
await Promise.all(Array.from({ length: CONCURRENCY }, worker));
|
||||
return complete;
|
||||
}
|
||||
|
||||
export async function keepOfflineShellFresh(fetchImpl: typeof fetch = fetch): Promise<void> {
|
||||
function warmedBuild(): string | null {
|
||||
try {
|
||||
return window.localStorage.getItem(WARMED_KEY);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function rememberWarmed(build: string): void {
|
||||
try {
|
||||
window.localStorage.setItem(WARMED_KEY, build);
|
||||
} catch {
|
||||
// Not remembered: the next launch walks the files again, from the cache.
|
||||
}
|
||||
}
|
||||
|
||||
export async function keepOfflineShellFresh(fetchImpl: typeof fetch = fetch, language?: string): Promise<void> {
|
||||
await TrekShell.refreshOfflineCopy();
|
||||
const response = await fetchImpl(MANIFEST_URL, { cache: 'no-store' });
|
||||
if (!response.ok) return;
|
||||
const files = manifestFiles(await response.json());
|
||||
await warmCache(files, (path) => fetchImpl(path).then((r) => r.blob()));
|
||||
const files = manifestFiles(await response.json(), language);
|
||||
// The build and the language together: a new language needs its own chunk.
|
||||
const build = `${[...files].sort().find((f) => /index-[^/]+\.js$/.test(f)) ?? files.length}:${language ?? 'en'}`;
|
||||
if (warmedBuild() === build) return;
|
||||
const complete = await warmCache(files, (path) =>
|
||||
fetchImpl(path, { priority: 'low' } as RequestInit).then((r) => r.blob())
|
||||
);
|
||||
if (complete) rememberWarmed(build);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user