diff --git a/.gitignore b/.gitignore index a944f0e..c0ab772 100644 --- a/.gitignore +++ b/.gitignore @@ -38,6 +38,9 @@ Thumbs.db # Go backend/vendor/ +# Wrangler +.wrangler/ + # other NOTES.md extension.zip diff --git a/backend/internal/api/readingroom.go b/backend/internal/api/readingroom.go index ecd5ecc..47fc1aa 100644 --- a/backend/internal/api/readingroom.go +++ b/backend/internal/api/readingroom.go @@ -879,7 +879,12 @@ func (h *Handler) AddCustomDomain(w http.ResponseWriter, r *http.Request) { h.db.UpsertReadingRoomConfig(r.Context(), rrConfig) if status == "active" { - h.cloudflare.EnsureRouting(domain, session.DID) + var handle string + h.db.Pool().QueryRow(r.Context(), "SELECT handle FROM oauth_sessions WHERE did = $1 LIMIT 1", session.DID).Scan(&handle) + if handle == "" { + handle = session.DID + } + h.cloudflare.EnsureRouting(domain, handle) } WriteSuccess(w, CustomDomainResponse{ @@ -926,7 +931,12 @@ func (h *Handler) PollCustomDomain(w http.ResponseWriter, r *http.Request) { h.db.UpdateReadingRoomDomain(r.Context(), session.DID, rrConfig.CustomDomain, rrConfig.CFHostnameID, status, string(recordsJSON)) if status == "active" && rrConfig.DomainStatus != "active" { - h.cloudflare.EnsureRouting(rrConfig.CustomDomain, session.DID) + var handle string + h.db.Pool().QueryRow(r.Context(), "SELECT handle FROM oauth_sessions WHERE did = $1 LIMIT 1", session.DID).Scan(&handle) + if handle == "" { + handle = session.DID + } + h.cloudflare.EnsureRouting(rrConfig.CustomDomain, handle) rrConfig.DomainStatus = "active" h.writeReadingRoomPublication(r, session, rrConfig.Title, rrConfig.Description, rrConfig) } diff --git a/edge/worker.js b/edge/worker.js index ade4194..046e709 100644 --- a/edge/worker.js +++ b/edge/worker.js @@ -18,6 +18,7 @@ export default { originUrl.hostname = ROOT_DOMAIN; let pathPrefix = "/reading-room/" + handle; + let isNote = false; if (url.pathname === "/" || url.pathname === "") { originUrl.pathname = pathPrefix; @@ -25,10 +26,23 @@ export default { originUrl.pathname = "/api/reading-room/rss/" + handle; } else if (url.pathname === "/.well-known/site.standard.publication" || url.pathname.startsWith("/.well-known/site.standard.publication/")) { originUrl.pathname = "/.well-known/site.standard.publication/reading-room/" + handle; - } else if (url.pathname.startsWith("/api/")) { + } else if ( + url.pathname.startsWith("/api/") || + url.pathname.startsWith("/_astro/") || + url.pathname.startsWith("/dist/") || + url.pathname.startsWith("/fonts/") || + url.pathname.startsWith("/.well-known/") || + /\.(js|css|woff2?|ttf|svg|png|jpg|jpeg|gif|ico|webp|webmanifest|map)$/.test(url.pathname) + ) { + originUrl.pathname = url.pathname; + } else if (url.pathname === "/note" || url.pathname.startsWith("/note/")) { + isNote = true; + originUrl.pathname = pathPrefix + "/note"; + } else if (url.pathname.startsWith("/reading-room/")) { originUrl.pathname = url.pathname; } else { - originUrl.pathname = pathPrefix + url.pathname; + const redirectUrl = new URL(url.pathname + url.search, "https://" + ROOT_DOMAIN); + return Response.redirect(redirectUrl.toString(), 302); } const modifiedRequest = new Request(originUrl, { @@ -44,6 +58,20 @@ export default { const response = await fetch(modifiedRequest); + const contentType = response.headers.get("content-type") || ""; + if (contentType.includes("text/html")) { + let html = await response.text(); + const inject = ``; + html = html.replace("", inject + ""); + const newResponse = new Response(html, { + status: response.status, + statusText: response.statusText, + headers: response.headers, + }); + newResponse.headers.set("X-Reading-Room-Domain", hostname); + return newResponse; + } + const newResponse = new Response(response.body, response); newResponse.headers.set("X-Reading-Room-Domain", hostname); return newResponse; diff --git a/web/src/views/AppShell.tsx b/web/src/views/AppShell.tsx index 829792a..5f54dfd 100644 --- a/web/src/views/AppShell.tsx +++ b/web/src/views/AppShell.tsx @@ -183,21 +183,38 @@ function AppLayout() { return () => document.removeEventListener("click", handleClick); }, [navigate]); + const rrHandle = (window as unknown as { __READING_ROOM_HANDLE__?: string }) + .__READING_ROOM_HANDLE__; + const rrIsNote = (window as unknown as { __READING_ROOM_IS_NOTE__?: boolean }) + .__READING_ROOM_IS_NOTE__; + const isReadingRoom = + location.pathname.startsWith("/reading-room/") || !!rrHandle; + return ( -
{t("readingRoom.notFoundDesc")}
- ← Back to Margin - +{t("readingRoom.notFoundDesc")}
- ← Back to Margin - +