diff --git a/client/src/components/Map/glLazy.tsx b/client/src/components/Map/glLazy.tsx index 3818cdd99..73cccce62 100644 --- a/client/src/components/Map/glLazy.tsx +++ b/client/src/components/Map/glLazy.tsx @@ -48,7 +48,7 @@ export function preloadTripMapRenderer(provider: string | undefined, token: stri const engine = pickGlRenderer(provider, token) if (!engine) return const view = engine === 'maplibre-gl' ? MapViewGLMaplibre : MapViewGLMapbox - void view.preload().catch((error: unknown) => console.debug('[map] renderer preload failed', error)) + void view.preload() } export const JourneyMapGLMapbox = lazyWithRetry(async () => { diff --git a/client/src/utils/lazyWithRetry.test.tsx b/client/src/utils/lazyWithRetry.test.tsx index d4dfd785e..565c7beb5 100644 --- a/client/src/utils/lazyWithRetry.test.tsx +++ b/client/src/utils/lazyWithRetry.test.tsx @@ -46,14 +46,22 @@ describe('lazyWithRetry', () => { it('shares the preloaded promise with the lazy component', async () => { const load = vi.fn(async () => ({ default: () =>
Map view
})) const Page = lazyWithRetry(load) - const pending = Page.preload() - expect(Page.preload()).toBe(pending) - await pending + await Promise.all([Page.preload(), Page.preload()]) render() expect(await screen.findByText('Map view')).toBeInTheDocument() expect(load).toHaveBeenCalledTimes(1) }) + it('lets navigation retry after a speculative preload fails', async () => { + const load = vi.fn(async () => ({ default: () =>
Loaded after navigation
})) + load.mockRejectedValueOnce(new Error('preload was interrupted')) + const Page = lazyWithRetry(load) + await Page.preload() + render() + expect(await screen.findByText('Loaded after navigation')).toBeInTheDocument() + expect(load).toHaveBeenCalledTimes(2) + }) + it('FE-UTIL-LAZYRETRY-006: renders the default export once the chunk arrives', async () => { const Page = lazyWithRetry(async () => ({ default: () =>
Atlas
})) render( diff --git a/client/src/utils/lazyWithRetry.ts b/client/src/utils/lazyWithRetry.ts index f82333f26..0d34c9d67 100644 --- a/client/src/utils/lazyWithRetry.ts +++ b/client/src/utils/lazyWithRetry.ts @@ -46,9 +46,9 @@ export function retryUrlFor(error: unknown): string | null { export function lazyWithRetry>( load: () => Promise<{ default: T }> -): LazyExoticComponent & { preload: () => Promise<{ default: T }> } { +): LazyExoticComponent & { preload: () => Promise } { let pending: Promise<{ default: T }> | undefined; - const preload = () => (pending ??= load() + const start = () => (pending ??= load() .catch((error: unknown) => { const url = retryUrlFor(error); // Rethrown on purpose: React.lazy hands the rejection to the nearest @@ -65,5 +65,13 @@ export function lazyWithRetry>( clearChunkReloadMarker(); return module; })); - return Object.assign(lazy(preload), { preload }); + const preload = async () => { + try { + await start(); + } catch { + // A failed speculative load leaves the actual navigation free to try again. + pending = undefined; + } + }; + return Object.assign(lazy(start), { preload }); } diff --git a/server/src/db/seeders/MapDefaultsSeeder.ts b/server/src/db/seeders/MapDefaultsSeeder.ts index a63f7f9b5..bb116ab25 100644 --- a/server/src/db/seeders/MapDefaultsSeeder.ts +++ b/server/src/db/seeders/MapDefaultsSeeder.ts @@ -1,5 +1,3 @@ -import { resolveEngine } from '../engine'; -import { writeTransaction } from '../engine/write-transaction'; import type { DB } from '../kysely/db'; import type { EntityManager } from '@mikro-orm/core'; import { Seeder } from '@mikro-orm/seeder'; @@ -12,27 +10,33 @@ const STYLE = 'default_user_setting_maplibre_style'; /** Give a new installation a starting basemap before its first user is created. */ export class MapDefaultsSeeder extends Seeder { async run(em: EntityManager): Promise { - await writeTransaction(em, resolveEngine(), async (tx) => { - const db = (tx as SqlEntityManager).getKysely>(); - // SchemaVersionSeeder writes this marker after the first bootstrap even on - // SSO-only installations that are still waiting for their first account. - const initialized = await db.selectFrom('schema_version').select('id').limit(1).executeTakeFirst(); - if (initialized) return; - const user = await db.selectFrom('users').select('id').limit(1).executeTakeFirst(); - if (user) return; + const db = (em as SqlEntityManager).getKysely>(); + const defaults = db + .selectNoFrom((eb) => [eb.val(PROVIDER).as('key'), eb.val(JSON.stringify('maplibre-gl')).as('value')]) + .unionAll( + db.selectNoFrom((eb) => [ + eb.val(STYLE).as('key'), + eb.val(JSON.stringify(OPENFREEMAP_DEFAULT_STYLE)).as('value'), + ]), + ) + .as('map_defaults'); - const provider = await db - .insertInto('app_settings') - .values({ key: PROVIDER, value: JSON.stringify('maplibre-gl') }) - .onConflict((conflict) => conflict.column('key').doNothing()) - .returning('key') - .executeTakeFirst(); - if (!provider) return; - await db - .insertInto('app_settings') - .values({ key: STYLE, value: JSON.stringify(OPENFREEMAP_DEFAULT_STYLE) }) - .onConflict((conflict) => conflict.column('key').doNothing()) - .execute(); - }); + // SchemaVersionSeeder marks completed first boots, including SSO-only installs + // still waiting for a user. Check and insert together on the bound database. + await db + .insertInto('app_settings') + .columns(['key', 'value']) + .expression( + db + .selectFrom(defaults) + .select(['key', 'value']) + .where(({ not, exists, selectFrom }) => not(exists(selectFrom('schema_version').select('id')))) + .where(({ not, exists, selectFrom }) => not(exists(selectFrom('users').select('id')))) + .where(({ not, exists, selectFrom }) => + not(exists(selectFrom('app_settings').select('key').where('key', '=', PROVIDER))), + ), + ) + .onConflict((conflict) => conflict.column('key').doNothing()) + .execute(); } }