diff --git a/CLAUDE.md b/CLAUDE.md index 8a3aee1..0055e21 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -74,7 +74,7 @@ The repository holds two apps during the rebuild (epic #94): - **Protocol**: `@atcute/*` packages (client, lexicons, identity resolver) - **Copy**: British English via Lingui (`web/src/i18n/`) - **Tests**: Vitest + Testing Library + MSW + axe-core; Playwright for e2e -- **Hosting**: Vercel project `lanyards` with Root Directory `web`, serving `lanyards.app` (`PUBLIC_BASE_URL=https://lanyards.app` in Production). `www` and the retired `next.lanyards.app` redirect to it (#154) +- **Hosting**: Vercel project `lanyards` with Root Directory `web`, serving `lanyards.app`; `www` redirects to it. Production's address is fixed in code (`PRODUCTION_BASE_URL`, #159) **Repository root: the legacy Next.js app.** It is frozen and no longer served: `lanyards.app` moved to the new app (#154), and Slice 1c (#103) deletes it. Don't start new work on it. @@ -92,7 +92,7 @@ AT Protocol OAuth will reject `localhost` with "invalid_request" error. **OAuth Client Configuration (`web/`):** - **Local development and e2e** (`http://127.0.0.1:5173` / `:4173`): uses the RFC 8252 loopback client. Its `client_id` is literally `http://localhost?redirect_uri=…&scope=…`; that's the only place `localhost` appears, and every redirect URI uses `127.0.0.1`. -- **Production and preview** (`https://`): the client metadata is generated at build time into `oauth-client-metadata.json` from `PUBLIC_BASE_URL` (or Vercel's branch URL for previews), by `web/plugins/oauthMetadata.ts`. +- **Production and preview** (`https://`): the client metadata is generated at build time into `oauth-client-metadata.json` by `web/plugins/oauthMetadata.ts`, for `https://lanyards.app` in Vercel production (`PRODUCTION_BASE_URL`), Vercel's branch URL for previews, and `PUBLIC_BASE_URL` for other builds. - Access the dev app via `http://127.0.0.1:5173` (NOT localhost) --- diff --git a/web/README.md b/web/README.md index 80f1dae..cdd2174 100644 --- a/web/README.md +++ b/web/README.md @@ -146,27 +146,22 @@ Environment Variables"** on, because preview builds use `VERCEL_ENV` and | Name | Environment | Value | | ------------------- | ------------------- | ---------------------------------- | -| `PUBLIC_BASE_URL` | Production only | `https://lanyards.app` | | `VITE_POSTHOG_KEY` | Production, Preview | optional: analytics is off without | | `VITE_POSTHOG_HOST` | Production, Preview | optional: defaults to PostHog's EU | -Production builds fail on purpose without `PUBLIC_BASE_URL`, because the -OAuth client metadata is generated from it. Don't set it for Preview: -previews use their own branch URL. +The OAuth client metadata and canonical links are built from the app's +address. Vercel production is always `https://lanyards.app`, fixed in code +(`PRODUCTION_BASE_URL` in `src/lib/atproto/oauthConfig.ts`), so no variable +can put another origin in the OAuth client (#159). Previews use their own +branch URL. `PUBLIC_BASE_URL` sets the address for other builds, such as +`npm run build:e2e`; Vercel doesn't need it. ### 3. Domains -| Domain | Setting | -| ------------------- | --------------------------------------------------------- | -| `lanyards.app` | Production, primary | -| `www.lanyards.app` | Redirect to `lanyards.app`, 308 | -| `next.lanyards.app` | Production, **no** redirect: `web/vercel.json` handles it | - -`next.lanyards.app` is the retired beta. `web/vercel.json` redirects its -page visits to the same path on `lanyards.app`, and only page visits, so a -copy installed from the beta can still fetch its update. The updated app -then sends it to `lanyards.app` (`src/lib/origin/movedOrigin.ts`). A Vercel -domain redirect would cut installed copies off from their update. +| Domain | Setting | +| ------------------ | ------------------------------- | +| `lanyards.app` | Production, primary | +| `www.lanyards.app` | Redirect to `lanyards.app`, 308 | DNS is on Cloudflare. Keep every Vercel record **DNS only** (grey cloud) so Vercel can issue certificates. The `_atproto` TXT record verifies the @@ -184,11 +179,6 @@ curl -sI https://lanyards.app/oauth-client-metadata.json | grep -i content-type # → application/json curl -s -o /dev/null -w '%{http_code} %{redirect_url}\n' https://www.lanyards.app/renderg.host # → 308 https://lanyards.app/renderg.host -for path in / /renderg.host; do - curl -s -o /dev/null -w '%{http_code} %{redirect_url}\n' \ - -H 'sec-fetch-dest: document' "https://next.lanyards.app$path" -done -# → 308 https://lanyards.app/ and 308 https://lanyards.app/renderg.host ``` Then sign in with a real account, check your name and handle on `/settings`, diff --git a/web/api/meta.ts b/web/api/meta.ts index 95a57b1..12c28f1 100644 --- a/web/api/meta.ts +++ b/web/api/meta.ts @@ -1,6 +1,7 @@ import { readFile } from 'node:fs/promises'; import { createMetaHandler } from '../server/metaHandler.ts'; +import { runtimeBaseUrl } from '../src/lib/atproto/oauthConfig.ts'; let index: Promise | undefined; @@ -16,5 +17,5 @@ function readIndex(): Promise { /** /:handle and /d/:did, with person-specific link-preview meta. */ export const GET = createMetaHandler({ readIndex, - baseUrl: process.env.PUBLIC_BASE_URL, + baseUrl: runtimeBaseUrl(process.env), }); diff --git a/web/scripts/vercelConfig.test.ts b/web/scripts/vercelConfig.test.ts index f86329f..86dc1b6 100644 --- a/web/scripts/vercelConfig.test.ts +++ b/web/scripts/vercelConfig.test.ts @@ -30,31 +30,10 @@ const config = JSON.parse( ) as Config; describe('vercel.json', () => { - // The beta's /@handle links, link previews included (#150). The beta - // subdomain's pages move to lanyards.app (#154); only page visits, so - // an installed copy's service worker can still fetch its update there. - it('sends next.lanyards.app pages to lanyards.app, and /@handle to /handle', () => { - const beta = [ - { type: 'host', value: 'next.lanyards.app' }, - { type: 'header', key: 'sec-fetch-dest', value: 'document' }, - ]; + // The beta's /@handle links, link previews included (#150). Nothing + // else redirects: next.lanyards.app is gone (#159). + it('sends /@handle on to /handle for good, and nothing else', () => { expect(config.redirects).toEqual([ - // Vercel's /:path* doesn't match the bare root, so it has its own. - { - source: '/', - has: beta, - destination: 'https://lanyards.app/', - permanent: true, - }, - { - source: '/:path*', - has: [ - { type: 'host', value: 'next.lanyards.app' }, - { type: 'header', key: 'sec-fetch-dest', value: 'document' }, - ], - destination: 'https://lanyards.app/:path*', - permanent: true, - }, { source: '/@:handle', destination: '/:handle', permanent: true }, ]); }); diff --git a/web/server/html.test.ts b/web/server/html.test.ts index 5fd12f5..23a3dec 100644 --- a/web/server/html.test.ts +++ b/web/server/html.test.ts @@ -72,11 +72,11 @@ describe('injectMeta', () => { describe('defaultMeta', () => { it('describes Lanyards itself, with the default image', () => { - expect(defaultMeta('https://next.lanyards.app')).toEqual({ + expect(defaultMeta('https://lanyards.app')).toEqual({ title: 'Lanyards', description: 'Your conference lanyard on the AT Protocol', - canonical: 'https://next.lanyards.app/', - image: 'https://next.lanyards.app/og/default.png', + canonical: 'https://lanyards.app/', + image: 'https://lanyards.app/og/default.png', imageAlt: 'Lanyards', type: 'website', }); diff --git a/web/server/metaHandler.test.ts b/web/server/metaHandler.test.ts index 16d2473..e0560aa 100644 --- a/web/server/metaHandler.test.ts +++ b/web/server/metaHandler.test.ts @@ -22,13 +22,13 @@ const handler = (fetchImpl: typeof fetch = profileFetch) => createMetaHandler({ readIndex: async () => INDEX, fetchImpl, - baseUrl: 'https://next.lanyards.app', + baseUrl: 'https://lanyards.app', }); describe('meta handler', () => { it('serves the app with person-specific meta, cached for ten minutes', async () => { const response = await handler()( - new Request('https://next.lanyards.app/alice.test?handle=alice.test') + new Request('https://lanyards.app/alice.test?handle=alice.test') ); const html = await response.text(); expect(response.status).toBe(200); @@ -42,14 +42,14 @@ describe('meta handler', () => { '' ); expect(html).toContain( - `` + `` ); }); it('reads the handle or DID from the path when the query has none', async () => { const fetchImpl = vi.fn(profileFetch); await handler(fetchImpl as unknown as typeof fetch)( - new Request(`https://next.lanyards.app/d/${DID}`) + new Request(`https://lanyards.app/d/${DID}`) ); expect(String(vi.mocked(fetchImpl).mock.calls[0]?.[0])).toContain( `actor=${encodeURIComponent(DID)}` @@ -59,7 +59,7 @@ describe('meta handler', () => { it('reads a bare handle from the path, the lanyard’s address (#150)', async () => { const fetchImpl = vi.fn(profileFetch); await handler(fetchImpl as unknown as typeof fetch)( - new Request('https://next.lanyards.app/alice.test') + new Request('https://lanyards.app/alice.test') ); expect(String(vi.mocked(fetchImpl).mock.calls[0]?.[0])).toContain( 'actor=alice.test' @@ -73,19 +73,19 @@ describe('meta handler', () => { { status: 400 } )) as typeof fetch; const response = await handler(missing)( - new Request('https://next.lanyards.app/nobody.test?handle=nobody.test') + new Request('https://lanyards.app/nobody.test?handle=nobody.test') ); expect(response.status).toBe(200); expect(response.headers.get('cache-control')).toBe('public, s-maxage=60'); expect(await response.text()).toContain( - '' + '' ); }); it.each([ - 'https://next.lanyards.app/@ada%', - 'https://next.lanyards.app/@x?handle=ada%25', - 'https://next.lanyards.app/d/did%3Aplc%3Aa%', + 'https://lanyards.app/@ada%', + 'https://lanyards.app/@x?handle=ada%25', + 'https://lanyards.app/d/did%3Aplc%3Aa%', ])('serves default meta for a malformed escape in %s', async (url) => { const fetchImpl = vi.fn(profileFetch); const response = await handler(fetchImpl as unknown as typeof fetch)( @@ -99,7 +99,7 @@ describe('meta handler', () => { it('never looks up something that is not a handle or DID', async () => { const fetchImpl = vi.fn(profileFetch); const response = await handler(fetchImpl as unknown as typeof fetch)( - new Request('https://next.lanyards.app/@x?handle=%3Cscript%3E') + new Request('https://lanyards.app/@x?handle=%3Cscript%3E') ); expect(fetchImpl).not.toHaveBeenCalled(); expect(await response.text()).toContain('Lanyards'); diff --git a/web/server/ogHandler.test.ts b/web/server/ogHandler.test.ts index 62e9d1d..dfaaf76 100644 --- a/web/server/ogHandler.test.ts +++ b/web/server/ogHandler.test.ts @@ -58,7 +58,7 @@ describe('OG image handler', () => { 'image/jpeg' ); await createOgHandler({ fetchImpl: impl, render })( - new Request(`https://next.lanyards.app/api/og?did=${DID}`) + new Request(`https://lanyards.app/api/og?did=${DID}`) ); expect(urls).toContain( `https://cdn.bsky.app/img/avatar_thumbnail/plain/${DID}/bafkreiavatar@jpeg` @@ -75,7 +75,7 @@ describe('OG image handler', () => { 'image/webp' ); await createOgHandler({ fetchImpl: impl, render })( - new Request(`https://next.lanyards.app/api/og?did=${DID}`) + new Request(`https://lanyards.app/api/og?did=${DID}`) ); expect(render).toHaveBeenCalledWith( expect.not.objectContaining({ avatarDataUri: expect.anything() }) @@ -85,7 +85,7 @@ describe('OG image handler', () => { it('draws the person’s badge with their small avatar', async () => { const render = vi.fn(async () => PNG); const response = await createOgHandler({ fetchImpl, render })( - new Request(`https://next.lanyards.app/og/p/${DID}.png?did=${DID}`) + new Request(`https://lanyards.app/og/p/${DID}.png?did=${DID}`) ); expect(response.headers.get('content-type')).toBe('image/png'); expect(response.headers.get('cache-control')).toBe( @@ -102,7 +102,7 @@ describe('OG image handler', () => { it('draws the default badge without a DID', async () => { const render = vi.fn(async () => PNG); await createOgHandler({ fetchImpl, render })( - new Request('https://next.lanyards.app/og/default.png') + new Request('https://lanyards.app/og/default.png') ); expect(render).toHaveBeenCalledWith(null); }); @@ -110,7 +110,7 @@ describe('OG image handler', () => { it('keeps the default badge for a day when no person was asked for', async () => { const render = vi.fn(async () => PNG); const response = await createOgHandler({ fetchImpl, render })( - new Request('https://next.lanyards.app/og/default.png') + new Request('https://lanyards.app/og/default.png') ); expect(response.headers.get('cache-control')).toBe( 'public, s-maxage=86400, stale-while-revalidate=604800' @@ -122,7 +122,7 @@ describe('OG image handler', () => { const failing = (async () => new Response(null, { status: 500 })) as typeof fetch; const response = await createOgHandler({ fetchImpl: failing, render })( - new Request(`https://next.lanyards.app/api/og?did=${DID}`) + new Request(`https://lanyards.app/api/og?did=${DID}`) ); expect(render).toHaveBeenCalledWith(null); expect(response.headers.get('cache-control')).toBe('public, s-maxage=60'); @@ -135,7 +135,7 @@ describe('OG image handler', () => { return PNG; }); const response = await createOgHandler({ fetchImpl, render })( - new Request(`https://next.lanyards.app/api/og?did=${DID}`) + new Request(`https://lanyards.app/api/og?did=${DID}`) ); expect(render).toHaveBeenLastCalledWith(null); expect(response.status).toBe(200); @@ -151,7 +151,7 @@ describe('OG image handler', () => { throw new Error('wasm failed to load'); }); const response = await createOgHandler({ fetchImpl, render })( - new Request(`https://next.lanyards.app/api/og?did=${DID}`) + new Request(`https://lanyards.app/api/og?did=${DID}`) ); expect(response.status).toBe(503); expect(response.headers.get('cache-control')).toBe('no-store'); diff --git a/web/src/components/QrCode/QrCode.test.tsx b/web/src/components/QrCode/QrCode.test.tsx index 349baa0..d8d58cb 100644 --- a/web/src/components/QrCode/QrCode.test.tsx +++ b/web/src/components/QrCode/QrCode.test.tsx @@ -8,7 +8,7 @@ import { renderWithProviders } from '@/test/render'; import { QrCode } from './QrCode'; -const URL = 'https://next.lanyards.app/d/did:plc:fixturealiceaaaaaaaaaaaa'; +const URL = 'https://lanyards.app/d/did:plc:fixturealiceaaaaaaaaaaaa'; /** Paints the module matrix, with a 4-module quiet zone, and scans it. */ function scan(code: Bitmap2D): string | undefined { diff --git a/web/src/lib/atproto/oauthConfig.test.ts b/web/src/lib/atproto/oauthConfig.test.ts index 7cc9e4e..6a4e0fc 100644 --- a/web/src/lib/atproto/oauthConfig.test.ts +++ b/web/src/lib/atproto/oauthConfig.test.ts @@ -6,7 +6,9 @@ import { FEEDBACK_SCOPE, FALLBACK_SCOPE, GRANULAR_SCOPE, + PRODUCTION_BASE_URL, resolveBaseUrl, + runtimeBaseUrl, safeReturnTo, } from './oauthConfig'; @@ -21,14 +23,12 @@ describe('buildOAuthEnv', () => { }); it('builds hosted client metadata for https origins', () => { - expect( - buildOAuthEnv('https://next.lanyards.app', 'atproto').metadata - ).toEqual({ - client_id: 'https://next.lanyards.app/oauth-client-metadata.json', + expect(buildOAuthEnv('https://lanyards.app', 'atproto').metadata).toEqual({ + client_id: 'https://lanyards.app/oauth-client-metadata.json', client_name: 'Lanyards', - client_uri: 'https://next.lanyards.app', - logo_uri: 'https://next.lanyards.app/pwa-192x192.png', - redirect_uris: ['https://next.lanyards.app/oauth/callback'], + client_uri: 'https://lanyards.app', + logo_uri: 'https://lanyards.app/pwa-192x192.png', + redirect_uris: ['https://lanyards.app/oauth/callback'], scope: 'atproto', grant_types: ['authorization_code', 'refresh_token'], response_types: ['code'], @@ -51,13 +51,28 @@ describe('resolveBaseUrl', () => { ); }); - it('uses PUBLIC_BASE_URL without a trailing slash for builds', () => { + // A stale variable once put the retired beta address in production's + // OAuth client, and sign-in broke (#159). + it('builds Vercel production for lanyards.app, whatever the variable says', () => { + expect(PRODUCTION_BASE_URL).toBe('https://lanyards.app'); expect( resolveBaseUrl({ command: 'build', - env: { PUBLIC_BASE_URL: 'https://next.lanyards.app/' }, + env: { + VERCEL_ENV: 'production', + PUBLIC_BASE_URL: 'https://next.lanyards.app', + }, }) - ).toBe('https://next.lanyards.app'); + ).toBe('https://lanyards.app'); + }); + + it('uses PUBLIC_BASE_URL without a trailing slash for other builds', () => { + expect( + resolveBaseUrl({ + command: 'build', + env: { PUBLIC_BASE_URL: 'http://127.0.0.1:4173/' }, + }) + ).toBe('http://127.0.0.1:4173'); }); it('falls back to the Vercel branch URL for preview builds', () => { @@ -69,13 +84,37 @@ describe('resolveBaseUrl', () => { ).toBe('https://x.vercel.app'); }); - it('refuses production builds without a base URL', () => { - expect(() => resolveBaseUrl({ command: 'build', env: {} })).toThrow( - 'PUBLIC_BASE_URL must be set for production builds' + it('refuses a preview build with no branch URL to use', () => { + expect(() => + resolveBaseUrl({ command: 'build', env: { VERCEL_ENV: 'preview' } }) + ).toThrow('Preview builds need VERCEL_BRANCH_URL'); + }); + + it('builds for lanyards.app when nothing else is said', () => { + expect(resolveBaseUrl({ command: 'build', env: {} })).toBe( + 'https://lanyards.app' ); }); }); +describe('runtimeBaseUrl (#159)', () => { + it('is lanyards.app in Vercel production, whatever the variable says', () => { + expect( + runtimeBaseUrl({ + VERCEL_ENV: 'production', + PUBLIC_BASE_URL: 'https://next.lanyards.app', + }) + ).toBe('https://lanyards.app'); + }); + + it('is PUBLIC_BASE_URL elsewhere, or nothing, so the request decides', () => { + expect(runtimeBaseUrl({ PUBLIC_BASE_URL: 'https://x.test/' })).toBe( + 'https://x.test' + ); + expect(runtimeBaseUrl({ VERCEL_ENV: 'preview' })).toBeUndefined(); + }); +}); + describe('safeReturnTo', () => { it('keeps same-origin paths', () => { expect(safeReturnTo('/settings')).toBe('/settings'); @@ -137,10 +176,7 @@ describe('OAuth scopes', () => { }); it('puts the client scope into the hosted metadata', () => { - const { metadata } = buildOAuthEnv( - 'https://next.lanyards.app', - CLIENT_SCOPE - ); + const { metadata } = buildOAuthEnv('https://lanyards.app', CLIENT_SCOPE); expect(metadata?.scope).toBe(CLIENT_SCOPE); }); }); diff --git a/web/src/lib/atproto/oauthConfig.ts b/web/src/lib/atproto/oauthConfig.ts index 3f8634a..dec9019 100644 --- a/web/src/lib/atproto/oauthConfig.ts +++ b/web/src/lib/atproto/oauthConfig.ts @@ -75,17 +75,47 @@ export type OAuthEnv = { metadata: ClientMetadata | null; }; +/** + * Where the app lives: its OAuth client and canonical links. Fixed, so a + * stale variable can't put another origin in the client again: it once + * kept the retired beta address there, and sign-in broke (#159). + */ +export const PRODUCTION_BASE_URL = 'https://lanyards.app'; + +type Env = Record; + +const withoutSlash = (url: string) => url.replace(/\/+$/, ''); + +/** + * The origin a build is for. Vercel's production is always lanyards.app; + * a preview, its branch URL; any other build, PUBLIC_BASE_URL (e2e uses + * the preview server), or else lanyards.app. + */ export function resolveBaseUrl(input: { command: 'serve' | 'build'; - env: Record; + env: Env; }): string { if (input.command === 'serve') return DEV_BASE_URL; const { PUBLIC_BASE_URL, VERCEL_ENV, VERCEL_BRANCH_URL } = input.env; - if (PUBLIC_BASE_URL) return PUBLIC_BASE_URL.replace(/\/+$/, ''); - if (VERCEL_ENV === 'preview' && VERCEL_BRANCH_URL) { - return `https://${VERCEL_BRANCH_URL}`; + if (VERCEL_ENV === 'production') return PRODUCTION_BASE_URL; + if (PUBLIC_BASE_URL) return withoutSlash(PUBLIC_BASE_URL); + if (VERCEL_ENV === 'preview') { + if (VERCEL_BRANCH_URL) return `https://${VERCEL_BRANCH_URL}`; + throw new Error( + 'Preview builds need VERCEL_BRANCH_URL: expose system environment variables' + ); } - throw new Error('PUBLIC_BASE_URL must be set for production builds'); + return PRODUCTION_BASE_URL; +} + +/** + * The origin a server function answers for: lanyards.app in Vercel's + * production, PUBLIC_BASE_URL elsewhere, or else undefined, so the + * request's own origin is used (a preview). + */ +export function runtimeBaseUrl(env: Env): string | undefined { + if (env.VERCEL_ENV === 'production') return PRODUCTION_BASE_URL; + return env.PUBLIC_BASE_URL ? withoutSlash(env.PUBLIC_BASE_URL) : undefined; } export function buildOAuthEnv(baseUrl: string, scope: string): OAuthEnv { diff --git a/web/src/lib/origin/movedOrigin.test.ts b/web/src/lib/origin/movedOrigin.test.ts deleted file mode 100644 index a5663b1..0000000 --- a/web/src/lib/origin/movedOrigin.test.ts +++ /dev/null @@ -1,77 +0,0 @@ -import { describe, expect, it, vi } from 'vitest'; - -import { leaveFor, movedTo } from './movedOrigin'; - -const HOME = 'https://lanyards.app'; - -describe('movedTo (#154)', () => { - it('sends a visit to next.lanyards.app on to lanyards.app, path and all', () => { - expect( - movedTo('https://next.lanyards.app/alice.test/edit?x=1#top', HOME) - ).toBe('https://lanyards.app/alice.test/edit?x=1#top'); - expect(movedTo('https://next.lanyards.app/', HOME)).toBe( - 'https://lanyards.app/' - ); - }); - - it('stays put where the app lives', () => { - expect(movedTo('https://lanyards.app/alice.test', HOME)).toBeNull(); - }); - - // Until the build's base URL moves, next.lanyards.app is still home. - it('stays put on next.lanyards.app while that is the base URL', () => { - expect( - movedTo( - 'https://next.lanyards.app/alice.test', - 'https://next.lanyards.app' - ) - ).toBeNull(); - }); - - it('leaves every other origin alone: previews, local, Vercel URLs', () => { - for (const href of [ - 'https://lanyards-git-x-renderghost.vercel.app/', - 'https://lanyards-three.vercel.app/', - 'http://127.0.0.1:5173/', - ]) { - expect(movedTo(href, HOME)).toBeNull(); - } - }); -}); - -describe('leaveFor (#154)', () => { - it('drops the old service worker and its caches, then goes', async () => { - const unregister = vi.fn(async () => true); - const remove = vi.fn(async (_key: string) => true); - const go = vi.fn(); - await leaveFor('https://lanyards.app/', { - registrations: async () => [{ unregister }, { unregister }], - cacheKeys: async () => ['workbox-precache-v2', 'other'], - deleteCache: remove, - go, - }); - expect(unregister).toHaveBeenCalledTimes(2); - expect(remove.mock.calls.map(([key]) => key)).toEqual([ - 'workbox-precache-v2', - 'other', - ]); - expect(go).toHaveBeenCalledWith('https://lanyards.app/'); - }); - - it('goes anyway when clearing up fails or hangs', async () => { - vi.useFakeTimers(); - const go = vi.fn(); - const leaving = leaveFor('https://lanyards.app/', { - registrations: () => new Promise(() => undefined), - cacheKeys: async () => { - throw new Error('no caches'); - }, - deleteCache: async () => true, - go, - }); - await vi.advanceTimersByTimeAsync(2000); - await leaving; - expect(go).toHaveBeenCalledWith('https://lanyards.app/'); - vi.useRealTimers(); - }); -}); diff --git a/web/src/lib/origin/movedOrigin.ts b/web/src/lib/origin/movedOrigin.ts deleted file mode 100644 index eaef2e2..0000000 --- a/web/src/lib/origin/movedOrigin.ts +++ /dev/null @@ -1,61 +0,0 @@ -/** - * Where the app used to live (#154). A visit there, including from a copy - * installed with its service worker, moves to the build's base URL once - * the base URL is somewhere else. - */ -export const RETIRED_ORIGINS: readonly string[] = ['https://next.lanyards.app']; - -/** How long to spend clearing up before leaving regardless. */ -const CLEAR_UP_MS = 1500; - -/** - * The same page at `baseUrl`, if `href` is on a retired origin that is no - * longer the base URL; otherwise null. - */ -export function movedTo(href: string, baseUrl: string): string | null { - const here = new URL(href); - const home = new URL(baseUrl); - if (here.origin === home.origin) return null; - if (!RETIRED_ORIGINS.includes(here.origin)) return null; - return new URL(`${here.pathname}${here.search}${here.hash}`, home).href; -} - -export type LeaveDeps = { - registrations(): Promise }[]>; - cacheKeys(): Promise; - deleteCache(key: string): Promise; - go(url: string): void; -}; - -/** The browser's own service workers, caches and navigation. */ -export const browserLeaveDeps = (): LeaveDeps => ({ - registrations: async () => - (await navigator.serviceWorker?.getRegistrations()) ?? [], - cacheKeys: async () => (await globalThis.caches?.keys()) ?? [], - deleteCache: (key) => caches.delete(key), - go: (url) => window.location.replace(url), -}); - -/** - * Leaves for `url`, first dropping this origin's service worker and - * caches so an installed copy doesn't keep serving the old app here. - * Leaves anyway if that fails or takes too long. - */ -export async function leaveFor( - url: string, - deps: LeaveDeps = browserLeaveDeps() -): Promise { - const clearUp = Promise.allSettled([ - deps - .registrations() - .then((all) => Promise.all(all.map((each) => each.unregister()))), - deps - .cacheKeys() - .then((keys) => Promise.all(keys.map((key) => deps.deleteCache(key)))), - ]); - await Promise.race([ - clearUp, - new Promise((resolve) => setTimeout(resolve, CLEAR_UP_MS)), - ]); - deps.go(url); -} diff --git a/web/src/main.tsx b/web/src/main.tsx index 851b51f..b49788a 100644 --- a/web/src/main.tsx +++ b/web/src/main.tsx @@ -14,7 +14,6 @@ import { track, } from './lib/analytics/analytics'; import { configureAuth } from './lib/atproto/session'; -import { leaveFor, movedTo } from './lib/origin/movedOrigin'; function start() { configureAuth(); @@ -38,11 +37,4 @@ function start() { ); } -// On a retired origin, such as an installed copy of the beta, go to where -// the app lives now instead of starting here (#154). -const moved = movedTo( - window.location.href, - import.meta.env.VITE_PUBLIC_BASE_URL -); -if (moved) void leaveFor(moved); -else start(); +start(); diff --git a/web/src/vite-env.d.ts b/web/src/vite-env.d.ts index 0577bb8..b8deef3 100644 --- a/web/src/vite-env.d.ts +++ b/web/src/vite-env.d.ts @@ -4,7 +4,7 @@ interface ImportMetaEnv { readonly VITE_OAUTH_CLIENT_ID: string; readonly VITE_OAUTH_REDIRECT_URI: string; - /** The origin this build serves, e.g. https://next.lanyards.app. */ + /** The origin this build serves, e.g. https://lanyards.app. */ readonly VITE_PUBLIC_BASE_URL: string; /** 'true' only in end-to-end test builds. */ readonly VITE_E2E?: string; diff --git a/web/vercel.json b/web/vercel.json index 4f3b3a1..b034e75 100644 --- a/web/vercel.json +++ b/web/vercel.json @@ -13,24 +13,6 @@ } }, "redirects": [ - { - "source": "/", - "has": [ - { "type": "host", "value": "next.lanyards.app" }, - { "type": "header", "key": "sec-fetch-dest", "value": "document" } - ], - "destination": "https://lanyards.app/", - "permanent": true - }, - { - "source": "/:path*", - "has": [ - { "type": "host", "value": "next.lanyards.app" }, - { "type": "header", "key": "sec-fetch-dest", "value": "document" } - ], - "destination": "https://lanyards.app/:path*", - "permanent": true - }, { "source": "/@:handle", "destination": "/:handle",