From 256e0b578bd26d889cf29b19176c84ff90ad6a3d Mon Sep 17 00:00:00 2001 From: bwees Date: Sat, 14 Nov 2026 13:26:26 -0600 Subject: [PATCH 01/14] feat(map): dock the sheet on the left side in landscape --- .../components/ui/bottom-sheet/sheet.svelte | 11 ++++++- src/lib/managers/map.manager.svelte.ts | 30 ++++++++++++++----- src/lib/utils/layout.ts | 7 +++++ 3 files changed, 40 insertions(+), 8 deletions(-) create mode 100644 src/lib/utils/layout.ts diff --git a/src/lib/components/ui/bottom-sheet/sheet.svelte b/src/lib/components/ui/bottom-sheet/sheet.svelte index 5ae44496..7c511a2e 100644 --- a/src/lib/components/ui/bottom-sheet/sheet.svelte +++ b/src/lib/components/ui/bottom-sheet/sheet.svelte @@ -2,6 +2,7 @@ import { beforeNavigate, goto } from '$app/navigation'; import * as Card from '$lib/components/ui/card'; import { cn, type WithElementRef } from '$lib/utils.js'; + import { SIDE_SHEET_GUTTER_PX, SIDE_SHEET_WIDTH_PX, sideSheetLayout } from '$lib/utils/layout'; import { clamp, last, minBy, range, throttle } from 'lodash-es'; import { onMount, setContext, tick } from 'svelte'; import type { HTMLAttributes } from 'svelte/elements'; @@ -632,7 +633,15 @@ {#if visible}
Date: Sat, 14 Nov 2026 13:26:26 -0600 Subject: [PATCH 02/14] fix(map): offset map controls by the safe area instead of a fixed gap --- src/lib/components/ui/map/MapControls.svelte | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/components/ui/map/MapControls.svelte b/src/lib/components/ui/map/MapControls.svelte index b5620463..add4a8e6 100644 --- a/src/lib/components/ui/map/MapControls.svelte +++ b/src/lib/components/ui/map/MapControls.svelte @@ -25,8 +25,8 @@ const loaded = $derived(mapCtx.isLoaded()); const positionClasses = { - 'top-left': 'top-16 left-2', - 'top-right': 'top-16 right-2', + 'top-left': 'top-[calc(env(safe-area-inset-top)+8px)] left-2', + 'top-right': 'top-[calc(env(safe-area-inset-top)+8px)] right-2', 'bottom-left': 'bottom-2 left-2', 'bottom-right': 'bottom-10 right-2', }; From d0d3259115d62f952e8ca5db2866d23d9aa58a02 Mon Sep 17 00:00:00 2001 From: bwees Date: Sat, 14 Nov 2026 13:31:58 -0600 Subject: [PATCH 03/14] feat(api): send the app platform and version as request headers --- src/lib/api/client.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/lib/api/client.ts b/src/lib/api/client.ts index 5c36e207..850539db 100644 --- a/src/lib/api/client.ts +++ b/src/lib/api/client.ts @@ -2,6 +2,8 @@ import createClient from 'openapi-fetch'; import type { components, paths } from './openapi'; import { SyncLineError, SyncStatusError } from '$lib/sync/sync-errors'; import { syncStreamLineSchema } from './sync-lines'; +import { APP_VERSION } from '$lib/utils/app-version'; +import { Capacitor } from '@capacitor/core'; const PRODUCTION_API_URL = 'https://api.maroonrides.app'; const LINE_SEPARATOR = '\n'; @@ -22,7 +24,14 @@ export type TripWalkStep = Schemas['TripWalkStep']; export type TripLegType = Schemas['TripLegType']; export type TripTimeKind = Schemas['TripTimeKind']; -const client = createClient({ baseUrl: API_URL }); +// Browsers can't set the User-Agent, so the platform and version also go in headers. +const client = createClient({ + baseUrl: API_URL, + headers: { + 'X-App-Platform': Capacitor.getPlatform(), + 'X-App-Version': APP_VERSION, + }, +}); export async function getSupportedVersion(): Promise { const { data, error } = await client.GET('/api/version/supported'); From b2536503701a99ab949f74a922b61f6deed2d609 Mon Sep 17 00:00:00 2001 From: bwees Date: Sat, 14 Nov 2026 13:31:58 -0600 Subject: [PATCH 04/14] fix(web): create the static assets directory before copying the sqlite wasm --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index cba2f681..e49e4b94 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "build": "vite build", "preview": "vite preview", "prepare": "svelte-kit sync && pnpm sqlite:wasm || echo ''", - "sqlite:wasm": "cp node_modules/sql.js/dist/sql-wasm.wasm static/assets/sql-wasm.wasm", + "sqlite:wasm": "mkdir -p static/assets && cp node_modules/sql.js/dist/sql-wasm.wasm static/assets/sql-wasm.wasm", "api:generate": "openapi-typescript ../api/openapi.json -o src/lib/api/openapi.d.ts", "db:generate": "drizzle-kit generate", "check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json", From 70230a136958114d58e609ed5d8ac7cd9c53abd1 Mon Sep 17 00:00:00 2001 From: bwees Date: Sat, 14 Nov 2026 13:31:59 -0600 Subject: [PATCH 05/14] ci: deploy the web build to cloudflare pages --- .github/workflows/deploy-web.yml | 62 ++++++++++++++++++++++++++++ .github/workflows/release-please.yml | 9 ++++ 2 files changed, 71 insertions(+) create mode 100644 .github/workflows/deploy-web.yml diff --git a/.github/workflows/deploy-web.yml b/.github/workflows/deploy-web.yml new file mode 100644 index 00000000..b45f0266 --- /dev/null +++ b/.github/workflows/deploy-web.yml @@ -0,0 +1,62 @@ +name: Deploy Web + +on: + pull_request: + workflow_dispatch: + workflow_call: + +concurrency: + group: ${{ github.workflow }}-${{ github.head_ref || github.ref }} + cancel-in-progress: true + +permissions: + contents: read + deployments: write + +jobs: + deploy: + runs-on: ubuntu-latest + + # Fork PRs don't get repository secrets. + if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository + + environment: + name: ${{ github.event_name == 'pull_request' && 'web-preview' || 'web-production' }} + url: ${{ steps.deploy.outputs.deployment-url }} + + steps: + - name: Checkout Repository + uses: actions/checkout@v4 + + - name: Install mise and dependencies + uses: jdx/mise-action@v3 + with: + install_args: node pnpm github:protomaps/go-pmtiles + + - name: Install Dependencies + run: pnpm install + + - name: Cache Basemap + id: basemap + uses: actions/cache@v4 + with: + path: | + static/map/cstat.pmtiles + static/map/sprites + static/map/fonts + key: basemap-${{ hashFiles('pmtiles/cstat.sh') }} + + - name: Build Basemap + if: steps.basemap.outputs.cache-hit != 'true' + run: pnpm pmtiles + + - name: Build Web Assets + run: pnpm build + + - name: Deploy to Cloudflare Pages + id: deploy + uses: cloudflare/wrangler-action@v3 + with: + apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} + accountId: ${{ vars.CLOUDFLARE_ACCOUNT_ID }} + command: pages deploy build --project-name=${{ vars.CLOUDFLARE_PAGES_PROJECT }} --branch=${{ github.head_ref || 'main' }} diff --git a/.github/workflows/release-please.yml b/.github/workflows/release-please.yml index 07a9c169..dabae9bf 100644 --- a/.github/workflows/release-please.yml +++ b/.github/workflows/release-please.yml @@ -31,3 +31,12 @@ jobs: buildPlatform: all tagName: ${{ needs.release-please.outputs.tag_name }} changelog: ${{ needs.release-please.outputs.body }} + + deploy-web: + needs: release-please + if: needs.release-please.outputs.release_created == 'true' + uses: ./.github/workflows/deploy-web.yml + secrets: inherit + permissions: + contents: read + deployments: write From fdc623d619da9c21cd36ab21bfcce6b3dd46fbe2 Mon Sep 17 00:00:00 2001 From: bwees Date: Sat, 14 Nov 2026 13:53:43 -0600 Subject: [PATCH 06/14] ci: run wrangler through npx so pnpm build approvals do not block it --- .github/workflows/deploy-web.yml | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/.github/workflows/deploy-web.yml b/.github/workflows/deploy-web.yml index b45f0266..19d417b9 100644 --- a/.github/workflows/deploy-web.yml +++ b/.github/workflows/deploy-web.yml @@ -53,10 +53,17 @@ jobs: - name: Build Web Assets run: pnpm build + # npx keeps wrangler out of the pnpm project, whose build-script allowlist blocks workerd. - name: Deploy to Cloudflare Pages id: deploy - uses: cloudflare/wrangler-action@v3 - with: - apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} - accountId: ${{ vars.CLOUDFLARE_ACCOUNT_ID }} - command: pages deploy build --project-name=${{ vars.CLOUDFLARE_PAGES_PROJECT }} --branch=${{ github.head_ref || 'main' }} + run: | + npx --yes "wrangler@$WRANGLER_VERSION" pages deploy build \ + --project-name="${{ vars.CLOUDFLARE_PAGES_PROJECT }}" \ + --branch="${{ github.head_ref || 'main' }}" + url=$(jq -r 'select(.type == "pages-deploy-detailed") | .url' "$WRANGLER_OUTPUT_FILE_PATH") + echo "deployment-url=$url" >> "$GITHUB_OUTPUT" + env: + WRANGLER_VERSION: 4.156.0 + WRANGLER_OUTPUT_FILE_PATH: ${{ runner.temp }}/wrangler-output.json + CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} + CLOUDFLARE_ACCOUNT_ID: ${{ vars.CLOUDFLARE_ACCOUNT_ID }} From 4fa83a0a049043f5b05068d09ee344645c9e48a3 Mon Sep 17 00:00:00 2001 From: bwees Date: Sat, 14 Nov 2026 13:54:23 -0600 Subject: [PATCH 07/14] ci: install wrangler with mise --- .github/workflows/deploy-web.yml | 6 ++---- mise.toml | 1 + 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/.github/workflows/deploy-web.yml b/.github/workflows/deploy-web.yml index 19d417b9..59200187 100644 --- a/.github/workflows/deploy-web.yml +++ b/.github/workflows/deploy-web.yml @@ -31,7 +31,7 @@ jobs: - name: Install mise and dependencies uses: jdx/mise-action@v3 with: - install_args: node pnpm github:protomaps/go-pmtiles + install_args: node pnpm github:protomaps/go-pmtiles npm:wrangler - name: Install Dependencies run: pnpm install @@ -53,17 +53,15 @@ jobs: - name: Build Web Assets run: pnpm build - # npx keeps wrangler out of the pnpm project, whose build-script allowlist blocks workerd. - name: Deploy to Cloudflare Pages id: deploy run: | - npx --yes "wrangler@$WRANGLER_VERSION" pages deploy build \ + wrangler pages deploy build \ --project-name="${{ vars.CLOUDFLARE_PAGES_PROJECT }}" \ --branch="${{ github.head_ref || 'main' }}" url=$(jq -r 'select(.type == "pages-deploy-detailed") | .url' "$WRANGLER_OUTPUT_FILE_PATH") echo "deployment-url=$url" >> "$GITHUB_OUTPUT" env: - WRANGLER_VERSION: 4.156.0 WRANGLER_OUTPUT_FILE_PATH: ${{ runner.temp }}/wrangler-output.json CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} CLOUDFLARE_ACCOUNT_ID: ${{ vars.CLOUDFLARE_ACCOUNT_ID }} diff --git a/mise.toml b/mise.toml index b220db18..5950ff0e 100644 --- a/mise.toml +++ b/mise.toml @@ -2,4 +2,5 @@ node = "24" pnpm = "latest" "github:protomaps/go-pmtiles" = "1.30.0" +"npm:wrangler" = "4.156.0" java = "21" From 433bbeb80ea69e2eb30e328b30bef59803f572b1 Mon Sep 17 00:00:00 2001 From: bwees Date: Sat, 14 Nov 2026 13:55:03 -0600 Subject: [PATCH 08/14] ci: deploy with wrangler-action using the mise wrangler --- .github/workflows/deploy-web.yml | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/.github/workflows/deploy-web.yml b/.github/workflows/deploy-web.yml index 59200187..cb6dc06d 100644 --- a/.github/workflows/deploy-web.yml +++ b/.github/workflows/deploy-web.yml @@ -55,13 +55,10 @@ jobs: - name: Deploy to Cloudflare Pages id: deploy - run: | - wrangler pages deploy build \ - --project-name="${{ vars.CLOUDFLARE_PAGES_PROJECT }}" \ - --branch="${{ github.head_ref || 'main' }}" - url=$(jq -r 'select(.type == "pages-deploy-detailed") | .url' "$WRANGLER_OUTPUT_FILE_PATH") - echo "deployment-url=$url" >> "$GITHUB_OUTPUT" - env: - WRANGLER_OUTPUT_FILE_PATH: ${{ runner.temp }}/wrangler-output.json - CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} - CLOUDFLARE_ACCOUNT_ID: ${{ vars.CLOUDFLARE_ACCOUNT_ID }} + uses: cloudflare/wrangler-action@v3 + with: + # Must match mise.toml, otherwise the action runs `pnpm add wrangler`, which pnpm blocks. + wranglerVersion: 4.156.0 + apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} + accountId: ${{ vars.CLOUDFLARE_ACCOUNT_ID }} + command: pages deploy build --project-name=${{ vars.CLOUDFLARE_PAGES_PROJECT }} --branch=${{ github.head_ref || 'main' }} From d87831899fee57c2cf329cd4bda88269349cbdd5 Mon Sep 17 00:00:00 2001 From: bwees Date: Sat, 14 Nov 2026 14:01:07 -0600 Subject: [PATCH 09/14] fix(web): load the basemap in one request since pages ignores range headers --- src/lib/components/ui/map/Map.svelte | 7 +++- .../ui/map/whole-file-source.test.ts | 42 +++++++++++++++++++ .../components/ui/map/whole-file-source.ts | 30 +++++++++++++ 3 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 src/lib/components/ui/map/whole-file-source.test.ts create mode 100644 src/lib/components/ui/map/whole-file-source.ts diff --git a/src/lib/components/ui/map/Map.svelte b/src/lib/components/ui/map/Map.svelte index 6fec8745..571f647a 100644 --- a/src/lib/components/ui/map/Map.svelte +++ b/src/lib/components/ui/map/Map.svelte @@ -5,6 +5,8 @@ import { PMTiles, Protocol } from 'pmtiles'; import { onDestroy, onMount, setContext } from 'svelte'; import Spinner from '../spinner/spinner.svelte'; + import { WholeFileSource } from './whole-file-source'; + import { Capacitor } from '@capacitor/core'; type MapStyleOption = string | MapLibreGL.StyleSpecification; @@ -81,7 +83,10 @@ const PMTILE_URL = '/map/cstat.pmtiles'; - const p = new PMTiles(PMTILE_URL); + // Cloudflare Pages ignores Range headers, so web loads the whole file. + const p = new PMTiles( + Capacitor.getPlatform() === 'web' ? new WholeFileSource(PMTILE_URL) : PMTILE_URL, + ); // The JS code and the map renderer share this one instance. protocol.add(p); diff --git a/src/lib/components/ui/map/whole-file-source.test.ts b/src/lib/components/ui/map/whole-file-source.test.ts new file mode 100644 index 00000000..93c91937 --- /dev/null +++ b/src/lib/components/ui/map/whole-file-source.test.ts @@ -0,0 +1,42 @@ +import { afterEach, describe, expect, it, vi } from 'vitest'; +import { WholeFileSource } from './whole-file-source'; + +const URL = '/map/test.pmtiles'; +const FILE = Uint8Array.from({ length: 32 }, (_, i) => i); + +function mockFetch(...responses: Response[]) { + const fetchMock = vi.fn(); + for (const response of responses) fetchMock.mockResolvedValueOnce(response); + vi.stubGlobal('fetch', fetchMock); + return fetchMock; +} + +afterEach(() => vi.unstubAllGlobals()); + +describe('WholeFileSource', () => { + it('slices reads out of one download', async () => { + const fetchMock = mockFetch(new Response(FILE)); + const source = new WholeFileSource(URL); + + const [first, second] = await Promise.all([source.getBytes(0, 4), source.getBytes(10, 3)]); + + expect([...new Uint8Array(first.data)]).toEqual([0, 1, 2, 3]); + expect([...new Uint8Array(second.data)]).toEqual([10, 11, 12]); + expect(fetchMock).toHaveBeenCalledOnce(); + }); + + it('downloads again after a failed download', async () => { + const fetchMock = mockFetch(new Response(null, { status: 503 }), new Response(FILE)); + const source = new WholeFileSource(URL); + + await expect(source.getBytes(0, 4)).rejects.toThrow('503'); + const retry = await source.getBytes(0, 2); + + expect([...new Uint8Array(retry.data)]).toEqual([0, 1]); + expect(fetchMock).toHaveBeenCalledTimes(2); + }); + + it('keys the source by its url', () => { + expect(new WholeFileSource(URL).getKey()).toBe(URL); + }); +}); diff --git a/src/lib/components/ui/map/whole-file-source.ts b/src/lib/components/ui/map/whole-file-source.ts new file mode 100644 index 00000000..670a0739 --- /dev/null +++ b/src/lib/components/ui/map/whole-file-source.ts @@ -0,0 +1,30 @@ +import type { RangeResponse, Source } from 'pmtiles'; + +/** Serves PMTiles reads from a single full download, for hosts that ignore Range headers. */ +export class WholeFileSource implements Source { + #url: string; + #bytes: Promise | null = null; + + constructor(url: string) { + this.#url = url; + } + + getKey() { + return this.#url; + } + + async getBytes(offset: number, length: number): Promise { + this.#bytes ??= this.#download(); + const bytes = await this.#bytes; + return { data: bytes.slice(offset, offset + length) }; + } + + async #download() { + const response = await fetch(this.#url); + if (!response.ok) { + this.#bytes = null; + throw new Error(`basemap download failed with ${response.status}`); + } + return response.arrayBuffer(); + } +} From 222eb257e1aaf7aa5a2e449c6cc69309e3656943 Mon Sep 17 00:00:00 2001 From: bwees Date: Sat, 14 Nov 2026 14:07:40 -0600 Subject: [PATCH 10/14] fix(web): read the basemap from r2 and resolve the sprite url --- src/lib/components/ui/map/Map.svelte | 16 +++---- src/lib/components/ui/map/map-style.test.ts | 47 +++++++++++++++++++ src/lib/components/ui/map/map-style.ts | 22 +++++++++ .../ui/map/whole-file-source.test.ts | 42 ----------------- .../components/ui/map/whole-file-source.ts | 30 ------------ 5 files changed, 75 insertions(+), 82 deletions(-) create mode 100644 src/lib/components/ui/map/map-style.test.ts create mode 100644 src/lib/components/ui/map/map-style.ts delete mode 100644 src/lib/components/ui/map/whole-file-source.test.ts delete mode 100644 src/lib/components/ui/map/whole-file-source.ts diff --git a/src/lib/components/ui/map/Map.svelte b/src/lib/components/ui/map/Map.svelte index 571f647a..7e958621 100644 --- a/src/lib/components/ui/map/Map.svelte +++ b/src/lib/components/ui/map/Map.svelte @@ -5,8 +5,7 @@ import { PMTiles, Protocol } from 'pmtiles'; import { onDestroy, onMount, setContext } from 'svelte'; import Spinner from '../spinner/spinner.svelte'; - import { WholeFileSource } from './whole-file-source'; - import { Capacitor } from '@capacitor/core'; + import { PMTILES_URL, resolveMapStyle } from './map-style'; type MapStyleOption = string | MapLibreGL.StyleSpecification; @@ -56,6 +55,8 @@ const isReady = $derived(isMounted && isLoaded); + const transformStyle = resolveMapStyle(window.location.href, PMTILES_URL); + export type MapContext = { getMap: () => MapLibreGL.Map | null; getBounds: () => MapLibreGL.LngLatBounds | null; @@ -81,12 +82,7 @@ const protocol = new Protocol(); MapLibreGL.addProtocol('pmtiles', protocol.tile); - const PMTILE_URL = '/map/cstat.pmtiles'; - - // Cloudflare Pages ignores Range headers, so web loads the whole file. - const p = new PMTiles( - Capacitor.getPlatform() === 'web' ? new WholeFileSource(PMTILE_URL) : PMTILE_URL, - ); + const p = new PMTiles(PMTILES_URL); // The JS code and the map renderer share this one instance. protocol.add(p); @@ -99,7 +95,6 @@ const mapInstance = new MapLibreGL.Map({ container: mapContainer, - style: requestedStyle, renderWorldCopies: false, // TODO move attribution elsewhere attributionControl: false, @@ -125,6 +120,7 @@ loadedStyle = requestedStyle; }); + mapInstance.setStyle(requestedStyle, { transformStyle }); map = mapInstance; }); @@ -137,7 +133,7 @@ requestedStyle = style; loadedStyle = null; - map.setStyle(style, { diff: false }); + map.setStyle(style, { diff: false, transformStyle }); }); onDestroy(() => { diff --git a/src/lib/components/ui/map/map-style.test.ts b/src/lib/components/ui/map/map-style.test.ts new file mode 100644 index 00000000..b00c95a7 --- /dev/null +++ b/src/lib/components/ui/map/map-style.test.ts @@ -0,0 +1,47 @@ +import type { StyleSpecification } from 'maplibre-gl'; +import { describe, expect, it } from 'vitest'; +import { resolveMapStyle } from './map-style'; + +const BASE_URL = 'https://web.maroonrides.app/route/1'; +const R2_PMTILES = 'https://tiles.maroonrides.app/cstat-abc.pmtiles'; + +const STYLE: StyleSpecification = { + version: 8, + sprite: '/map/sprites/light', + glyphs: '/map/fonts/{fontstack}/{range}.pbf', + sources: { + protomaps: { type: 'vector', url: 'pmtiles:///map/cstat.pmtiles', attribution: 'OSM' }, + routes: { type: 'geojson', data: { type: 'FeatureCollection', features: [] } }, + }, + layers: [], +}; + +describe('resolveMapStyle', () => { + it('points the basemap at the given pmtiles url', () => { + const style = resolveMapStyle(BASE_URL, R2_PMTILES)(undefined, STYLE); + + expect(style.sources.protomaps).toEqual({ + type: 'vector', + url: `pmtiles://${R2_PMTILES}`, + attribution: 'OSM', + }); + }); + + it('keeps the bundled basemap url when given the bundled path', () => { + const style = resolveMapStyle(BASE_URL, '/map/cstat.pmtiles')(undefined, STYLE); + + expect(style.sources.protomaps).toMatchObject({ url: 'pmtiles:///map/cstat.pmtiles' }); + }); + + it('makes the sprite url absolute against the page origin', () => { + const style = resolveMapStyle(BASE_URL, R2_PMTILES)(undefined, STYLE); + + expect(style.sprite).toBe('https://web.maroonrides.app/map/sprites/light'); + }); + + it('leaves other sources untouched', () => { + const style = resolveMapStyle(BASE_URL, R2_PMTILES)(undefined, STYLE); + + expect(style.sources.routes).toBe(STYLE.sources.routes); + }); +}); diff --git a/src/lib/components/ui/map/map-style.ts b/src/lib/components/ui/map/map-style.ts new file mode 100644 index 00000000..5e41d053 --- /dev/null +++ b/src/lib/components/ui/map/map-style.ts @@ -0,0 +1,22 @@ +import type { TransformStyleFunction, VectorSourceSpecification } from 'maplibre-gl'; + +/** Web builds point this at R2, since Cloudflare Pages ignores the Range headers PMTiles needs. */ +export const PMTILES_URL: string = import.meta.env.VITE_PMTILES_URL ?? '/map/cstat.pmtiles'; + +const BASEMAP_SOURCE = 'protomaps'; + +export function resolveMapStyle(baseUrl: string, pmtilesUrl: string): TransformStyleFunction { + return (_previous, next) => { + const basemap = next.sources[BASEMAP_SOURCE] as VectorSourceSpecification; + + return { + ...next, + // MapLibre rejects relative sprite URLs. + sprite: typeof next.sprite === 'string' ? new URL(next.sprite, baseUrl).href : next.sprite, + sources: { + ...next.sources, + [BASEMAP_SOURCE]: { ...basemap, url: `pmtiles://${pmtilesUrl}` }, + }, + }; + }; +} diff --git a/src/lib/components/ui/map/whole-file-source.test.ts b/src/lib/components/ui/map/whole-file-source.test.ts deleted file mode 100644 index 93c91937..00000000 --- a/src/lib/components/ui/map/whole-file-source.test.ts +++ /dev/null @@ -1,42 +0,0 @@ -import { afterEach, describe, expect, it, vi } from 'vitest'; -import { WholeFileSource } from './whole-file-source'; - -const URL = '/map/test.pmtiles'; -const FILE = Uint8Array.from({ length: 32 }, (_, i) => i); - -function mockFetch(...responses: Response[]) { - const fetchMock = vi.fn(); - for (const response of responses) fetchMock.mockResolvedValueOnce(response); - vi.stubGlobal('fetch', fetchMock); - return fetchMock; -} - -afterEach(() => vi.unstubAllGlobals()); - -describe('WholeFileSource', () => { - it('slices reads out of one download', async () => { - const fetchMock = mockFetch(new Response(FILE)); - const source = new WholeFileSource(URL); - - const [first, second] = await Promise.all([source.getBytes(0, 4), source.getBytes(10, 3)]); - - expect([...new Uint8Array(first.data)]).toEqual([0, 1, 2, 3]); - expect([...new Uint8Array(second.data)]).toEqual([10, 11, 12]); - expect(fetchMock).toHaveBeenCalledOnce(); - }); - - it('downloads again after a failed download', async () => { - const fetchMock = mockFetch(new Response(null, { status: 503 }), new Response(FILE)); - const source = new WholeFileSource(URL); - - await expect(source.getBytes(0, 4)).rejects.toThrow('503'); - const retry = await source.getBytes(0, 2); - - expect([...new Uint8Array(retry.data)]).toEqual([0, 1]); - expect(fetchMock).toHaveBeenCalledTimes(2); - }); - - it('keys the source by its url', () => { - expect(new WholeFileSource(URL).getKey()).toBe(URL); - }); -}); diff --git a/src/lib/components/ui/map/whole-file-source.ts b/src/lib/components/ui/map/whole-file-source.ts deleted file mode 100644 index 670a0739..00000000 --- a/src/lib/components/ui/map/whole-file-source.ts +++ /dev/null @@ -1,30 +0,0 @@ -import type { RangeResponse, Source } from 'pmtiles'; - -/** Serves PMTiles reads from a single full download, for hosts that ignore Range headers. */ -export class WholeFileSource implements Source { - #url: string; - #bytes: Promise | null = null; - - constructor(url: string) { - this.#url = url; - } - - getKey() { - return this.#url; - } - - async getBytes(offset: number, length: number): Promise { - this.#bytes ??= this.#download(); - const bytes = await this.#bytes; - return { data: bytes.slice(offset, offset + length) }; - } - - async #download() { - const response = await fetch(this.#url); - if (!response.ok) { - this.#bytes = null; - throw new Error(`basemap download failed with ${response.status}`); - } - return response.arrayBuffer(); - } -} From e317a4f0c72083d1cd5d4f7c41289ff15ab37b69 Mon Sep 17 00:00:00 2001 From: bwees Date: Sat, 14 Nov 2026 14:07:40 -0600 Subject: [PATCH 11/14] ci: upload the basemap to r2 on web deploy --- .github/workflows/deploy-web.yml | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/.github/workflows/deploy-web.yml b/.github/workflows/deploy-web.yml index cb6dc06d..1116bc29 100644 --- a/.github/workflows/deploy-web.yml +++ b/.github/workflows/deploy-web.yml @@ -13,6 +13,10 @@ permissions: contents: read deployments: write +env: + # Must match mise.toml, otherwise wrangler-action runs `pnpm add wrangler`, which pnpm blocks. + WRANGLER_VERSION: 4.156.0 + jobs: deploy: runs-on: ubuntu-latest @@ -50,15 +54,33 @@ jobs: if: steps.basemap.outputs.cache-hit != 'true' run: pnpm pmtiles + # Content-addressed, so previews never overwrite the basemap production reads. + - name: Name Basemap Object + id: tiles + run: echo "key=cstat-$(sha256sum static/map/cstat.pmtiles | cut -c1-16).pmtiles" >> "$GITHUB_OUTPUT" + - name: Build Web Assets run: pnpm build + env: + VITE_PMTILES_URL: ${{ vars.MAP_TILES_URL }}/${{ steps.tiles.outputs.key }} + + - name: Upload Basemap to R2 + uses: cloudflare/wrangler-action@v3 + with: + wranglerVersion: ${{ env.WRANGLER_VERSION }} + apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} + accountId: ${{ vars.CLOUDFLARE_ACCOUNT_ID }} + command: r2 object put ${{ vars.MAP_TILES_BUCKET }}/${{ steps.tiles.outputs.key }} --file=static/map/cstat.pmtiles --remote --content-type=application/vnd.pmtiles --cache-control=public,max-age=31536000,immutable + + # The web build reads the basemap from R2. + - name: Remove Bundled Basemap + run: rm build/map/cstat.pmtiles - name: Deploy to Cloudflare Pages id: deploy uses: cloudflare/wrangler-action@v3 with: - # Must match mise.toml, otherwise the action runs `pnpm add wrangler`, which pnpm blocks. - wranglerVersion: 4.156.0 + wranglerVersion: ${{ env.WRANGLER_VERSION }} apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} accountId: ${{ vars.CLOUDFLARE_ACCOUNT_ID }} command: pages deploy build --project-name=${{ vars.CLOUDFLARE_PAGES_PROJECT }} --branch=${{ github.head_ref || 'main' }} From 7a7d8cb700ba55e50981b10c7980c082fe690fab Mon Sep 17 00:00:00 2001 From: bwees Date: Sat, 14 Nov 2026 14:16:44 -0600 Subject: [PATCH 12/14] ci: upload the basemap through the r2 s3 api --- .github/workflows/deploy-web.yml | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/.github/workflows/deploy-web.yml b/.github/workflows/deploy-web.yml index 1116bc29..13cc73c1 100644 --- a/.github/workflows/deploy-web.yml +++ b/.github/workflows/deploy-web.yml @@ -64,13 +64,17 @@ jobs: env: VITE_PMTILES_URL: ${{ vars.MAP_TILES_URL }}/${{ steps.tiles.outputs.key }} + # R2's REST object API rejects bucket-scoped tokens, so this uses the S3 API instead of wrangler. - name: Upload Basemap to R2 - uses: cloudflare/wrangler-action@v3 - with: - wranglerVersion: ${{ env.WRANGLER_VERSION }} - apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} - accountId: ${{ vars.CLOUDFLARE_ACCOUNT_ID }} - command: r2 object put ${{ vars.MAP_TILES_BUCKET }}/${{ steps.tiles.outputs.key }} --file=static/map/cstat.pmtiles --remote --content-type=application/vnd.pmtiles --cache-control=public,max-age=31536000,immutable + run: >- + aws s3 cp static/map/cstat.pmtiles "s3://${{ vars.MAP_TILES_BUCKET }}/${{ steps.tiles.outputs.key }}" + --endpoint-url "https://${{ vars.CLOUDFLARE_ACCOUNT_ID }}.r2.cloudflarestorage.com" + --content-type application/vnd.pmtiles + --cache-control "public, max-age=31536000, immutable" + env: + AWS_ACCESS_KEY_ID: ${{ secrets.R2_ACCESS_KEY_ID }} + AWS_SECRET_ACCESS_KEY: ${{ secrets.R2_SECRET_ACCESS_KEY }} + AWS_DEFAULT_REGION: auto # The web build reads the basemap from R2. - name: Remove Bundled Basemap From 5a6e890797183bda4f1231352cba1a4625ea13b5 Mon Sep 17 00:00:00 2001 From: bwees Date: Sat, 14 Nov 2026 14:32:05 -0600 Subject: [PATCH 13/14] ci: name web previews after the pr number --- .github/workflows/deploy-web.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/deploy-web.yml b/.github/workflows/deploy-web.yml index 13cc73c1..606d4b70 100644 --- a/.github/workflows/deploy-web.yml +++ b/.github/workflows/deploy-web.yml @@ -87,4 +87,4 @@ jobs: wranglerVersion: ${{ env.WRANGLER_VERSION }} apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} accountId: ${{ vars.CLOUDFLARE_ACCOUNT_ID }} - command: pages deploy build --project-name=${{ vars.CLOUDFLARE_PAGES_PROJECT }} --branch=${{ github.head_ref || 'main' }} + command: pages deploy build --project-name=${{ vars.CLOUDFLARE_PAGES_PROJECT }} --branch=${{ github.event.pull_request.number && format('pr{0}', github.event.pull_request.number) || 'main' }} From 775cd35070d1d2513ca66b2e455d30824a132b8e Mon Sep 17 00:00:00 2001 From: bwees Date: Sat, 14 Nov 2026 14:38:13 -0600 Subject: [PATCH 14/14] ci: comment the web preview url on the pr --- .github/workflows/deploy-web.yml | 22 ++++++++++++++++++++++ .github/workflows/release-please.yml | 1 + 2 files changed, 23 insertions(+) diff --git a/.github/workflows/deploy-web.yml b/.github/workflows/deploy-web.yml index 606d4b70..f58a1234 100644 --- a/.github/workflows/deploy-web.yml +++ b/.github/workflows/deploy-web.yml @@ -12,6 +12,7 @@ concurrency: permissions: contents: read deployments: write + pull-requests: write env: # Must match mise.toml, otherwise wrangler-action runs `pnpm add wrangler`, which pnpm blocks. @@ -88,3 +89,24 @@ jobs: apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} accountId: ${{ vars.CLOUDFLARE_ACCOUNT_ID }} command: pages deploy build --project-name=${{ vars.CLOUDFLARE_PAGES_PROJECT }} --branch=${{ github.event.pull_request.number && format('pr{0}', github.event.pull_request.number) || 'main' }} + + # Edits the bot's previous comment so the PR keeps one preview link. + - name: Comment Preview URL + if: github.event_name == 'pull_request' + run: | + gh pr comment "$PR_NUMBER" --edit-last --create-if-none --body "$(cat <