From 13c684c078f190270023c7da05a4fe9251d983cf Mon Sep 17 00:00:00 2001 From: Heath Stewart Date: Thu, 26 Feb 2026 20:31:42 -0800 Subject: [PATCH] Automatically redirect to referring page Also fixes a bug I just noticed in the Subscribe help page. --- docs/docs/pages/subscribe.mdx | 2 +- docs/src/routes/subscribe.ts | 77 +++++++++++++++++++++++++++++++---- 2 files changed, 70 insertions(+), 9 deletions(-) diff --git a/docs/docs/pages/subscribe.mdx b/docs/docs/pages/subscribe.mdx index dbe4e9e..1a1016c 100644 --- a/docs/docs/pages/subscribe.mdx +++ b/docs/docs/pages/subscribe.mdx @@ -147,7 +147,7 @@ The `` component accepts the following attributes: The component dispatches custom events you can listen to: -| Event | Description | `detail` | +| Event | Description | Detail | |-------|-------------|----------| | `sequoia-subscribed` | Fired when the subscription is created successfully. | `{ publicationUri: string, recordUri: string }` | | `sequoia-subscribe-error` | Fired when the subscription fails. | `{ message: string }` | diff --git a/docs/src/routes/subscribe.ts b/docs/src/routes/subscribe.ts index ce30a08..45170be 100644 --- a/docs/src/routes/subscribe.ts +++ b/docs/src/routes/subscribe.ts @@ -35,6 +35,7 @@ async function getVocsStyleHref( const subscribe = new Hono<{ Bindings: Env }>(); const COLLECTION = "site.standard.graph.subscription"; +const REDIRECT_DELAY_SECONDS = 5; // ============================================================================ // Helpers @@ -162,9 +163,16 @@ subscribe.get("/", async (c) => { ); } + // Prefer an explicit returnTo query param (survives the OAuth round-trip); + // fall back to the Referer header on the first visit, ignoring self-referrals. + const referer = c.req.header("referer"); + const returnTo = + c.req.query("returnTo") ?? + (referer && !referer.includes("/subscribe") ? referer : undefined); + const did = getSessionDid(c); if (!did) { - return c.html(renderHandleForm(publicationUri, styleHref)); + return c.html(renderHandleForm(publicationUri, styleHref, returnTo)); } try { @@ -179,7 +187,7 @@ subscribe.get("/", async (c) => { ); if (existingUri) { return c.html( - renderSuccess(publicationUri, existingUri, true, styleHref), + renderSuccess(publicationUri, existingUri, true, styleHref, returnTo), ); } @@ -193,7 +201,13 @@ subscribe.get("/", async (c) => { }); return c.html( - renderSuccess(publicationUri, result.data.uri, false, styleHref), + renderSuccess( + publicationUri, + result.data.uri, + false, + styleHref, + returnTo, + ), ); } catch (error) { console.error("Subscribe GET error:", error); @@ -202,6 +216,7 @@ subscribe.get("/", async (c) => { renderHandleForm( publicationUri, styleHref, + returnTo, "Session expired. Please sign in again.", ), ); @@ -219,6 +234,7 @@ subscribe.post("/login", async (c) => { const body = await c.req.parseBody(); const handle = (body["handle"] as string | undefined)?.trim(); const publicationUri = body["publicationUri"] as string | undefined; + const formReturnTo = (body["returnTo"] as string | undefined) || undefined; if (!handle || !publicationUri) { const styleHref = await getVocsStyleHref(c.env.ASSETS, c.req.url); @@ -228,7 +244,9 @@ subscribe.post("/login", async (c) => { ); } - const returnTo = `${c.env.CLIENT_URL}/subscribe?publicationUri=${encodeURIComponent(publicationUri)}`; + const returnTo = + `${c.env.CLIENT_URL}/subscribe?publicationUri=${encodeURIComponent(publicationUri)}` + + (formReturnTo ? `&returnTo=${encodeURIComponent(formReturnTo)}` : ""); setReturnToCookie(c, returnTo, c.env.CLIENT_URL); return c.redirect( @@ -243,11 +261,15 @@ subscribe.post("/login", async (c) => { function renderHandleForm( publicationUri: string, styleHref: string, + returnTo?: string, error?: string, ): string { const errorHtml = error ? `

${escapeHtml(error)}

` : ""; + const returnToInput = returnTo + ? `` + : ""; return page( ` @@ -255,7 +277,8 @@ function renderHandleForm(

Enter your Bluesky handle to subscribe to this publication.

${errorHtml}
- + + ${returnToInput} Redirecting to ${escapeHtml(returnTo)} in ${REDIRECT_DELAY_SECONDS}\u00a0seconds\u2026

+ ` + : ""; + const headExtra = returnTo + ? `` + : ""; + return page( `

Subscribed ✓

${msg}

-

Publication: ${escapedPublicationUri}

-

Record: ${escapedRecordUri}

+ ${redirectHtml} + + + + + + + + + + + + +
Publication + +
Record + +
`, styleHref, + headExtra, ); } @@ -300,7 +360,7 @@ function renderError(message: string, styleHref: string): string { ); } -function page(body: string, styleHref: string): string { +function page(body: string, styleHref: string, headExtra = ""): string { return ` @@ -309,6 +369,7 @@ function page(body: string, styleHref: string): string { Sequoia · Subscribe + ${headExtra}