From 0fc608ea266a4b7b1dd67258a58bfa8461b8ed39 Mon Sep 17 00:00:00 2001 From: eric Date: Mon, 28 Sep 2026 18:54:03 -0500 Subject: [PATCH] Fix ebook /read links to /book/read and add compat redirects. Co-authored-by: Cursor --- app/read/[slug]/page.tsx | 9 ++++ app/read/page.tsx | 6 +++ apps/ebook/src/components/BookEnd.tsx | 2 +- apps/ebook/src/components/BookSearch.tsx | 2 +- apps/ebook/src/components/ChapterMenu.tsx | 8 +-- apps/ebook/src/components/ChapterNav.tsx | 4 +- apps/ebook/src/components/ChapterSidebar.tsx | 4 +- apps/ebook/src/components/ContinueReading.tsx | 2 +- .../ebook/src/components/CoverChapterGrid.tsx | 2 +- apps/ebook/src/components/GrowthKit.tsx | 2 +- apps/ebook/src/components/MarkdownContent.tsx | 4 +- .../ebook/src/components/MobileChapterBar.tsx | 4 +- apps/ebook/src/components/ReaderShortcuts.tsx | 2 +- .../src/components/ScrollReadingTracker.tsx | 2 +- apps/ebook/src/components/SelectionShare.tsx | 2 +- apps/ebook/src/components/ShareQuoteCard.tsx | 2 +- apps/ebook/src/lib/metadata.ts | 4 +- apps/ebook/src/lib/share-text.ts | 4 +- scripts/fix-ebook-read-paths.py | 49 +++++++++++++++++++ 19 files changed, 89 insertions(+), 25 deletions(-) create mode 100644 app/read/[slug]/page.tsx create mode 100644 app/read/page.tsx create mode 100644 scripts/fix-ebook-read-paths.py diff --git a/app/read/[slug]/page.tsx b/app/read/[slug]/page.tsx new file mode 100644 index 0000000..4c96f8a --- /dev/null +++ b/app/read/[slug]/page.tsx @@ -0,0 +1,9 @@ +import { redirect } from "next/navigation"; + +type Props = { params: Promise<{ slug: string }> }; + +/** Compat: /read/[slug] → /book/read#slug (hash preserved by browser on redirect when possible) */ +export default async function ReadSlugCompatRedirect({ params }: Props) { + const { slug } = await params; + redirect(`/book/read#${slug}`); +} diff --git a/app/read/page.tsx b/app/read/page.tsx new file mode 100644 index 0000000..88aabd3 --- /dev/null +++ b/app/read/page.tsx @@ -0,0 +1,6 @@ +import { redirect } from "next/navigation"; + +/** Compat: old ebook URLs lived at /read — now mounted under /book/read */ +export default function ReadCompatRedirect() { + redirect("/book/read"); +} diff --git a/apps/ebook/src/components/BookEnd.tsx b/apps/ebook/src/components/BookEnd.tsx index 3b81619..2e76238 100644 --- a/apps/ebook/src/components/BookEnd.tsx +++ b/apps/ebook/src/components/BookEnd.tsx @@ -47,7 +47,7 @@ export function BookEnd({ book }: { book: Book }) { const first = book.chapters[0]?.slug; if (first) { document.getElementById(first)?.scrollIntoView({ behavior: 'smooth' }); - history.replaceState(null, '', `/read#${first}`); + history.replaceState(null, '', `/book/read#${first}`); } }} > diff --git a/apps/ebook/src/components/BookSearch.tsx b/apps/ebook/src/components/BookSearch.tsx index 5c2eba1..99cb80b 100644 --- a/apps/ebook/src/components/BookSearch.tsx +++ b/apps/ebook/src/components/BookSearch.tsx @@ -9,7 +9,7 @@ import { useT } from '@ebook/components/LocaleProvider'; function jumpToHit(slug: string, query: string) { const section = document.getElementById(slug); section?.scrollIntoView({ behavior: 'smooth', block: 'start' }); - history.replaceState(null, '', `/read#${slug}`); + history.replaceState(null, '', `/book/read#${slug}`); window.setTimeout(() => { const root = document.getElementById(slug); diff --git a/apps/ebook/src/components/ChapterMenu.tsx b/apps/ebook/src/components/ChapterMenu.tsx index f931002..47256e7 100644 --- a/apps/ebook/src/components/ChapterMenu.tsx +++ b/apps/ebook/src/components/ChapterMenu.tsx @@ -8,7 +8,7 @@ import type { Book } from '@ebook/lib/book-types'; function scrollToChapterSlug(slug: string) { document.getElementById(slug)?.scrollIntoView({ behavior: 'smooth', block: 'start' }); - history.replaceState(null, '', `/read#${slug}`); + history.replaceState(null, '', `/book/read#${slug}`); } export function ChapterMenu({ @@ -24,7 +24,7 @@ export function ChapterMenu({ const [mounted, setMounted] = useState(false); const [filter, setFilter] = useState(''); const pathname = usePathname(); - const isScroll = scrollMode || pathname === '/read'; + const isScroll = scrollMode || pathname === '/book/read'; useEffect(() => setMounted(true), []); @@ -98,10 +98,10 @@ export function ChapterMenu({ chapter.title.toLowerCase().includes(filter.trim().toLowerCase()) ) .map(({ chapter, i }) => { - const href = isScroll ? `/read#${chapter.slug}` : `/read/${chapter.slug}`; + const href = isScroll ? `/book/read#${chapter.slug}` : `/book/read/${chapter.slug}`; const active = isScroll ? activeSlug === chapter.slug - : pathname === `/read/${chapter.slug}`; + : pathname === `/book/read/${chapter.slug}`; if (isScroll) { return ( diff --git a/apps/ebook/src/components/ChapterNav.tsx b/apps/ebook/src/components/ChapterNav.tsx index 6fae462..c9b4b96 100644 --- a/apps/ebook/src/components/ChapterNav.tsx +++ b/apps/ebook/src/components/ChapterNav.tsx @@ -18,7 +18,7 @@ export function ChapterNav({
{prev ? ( {t('reader.prevChapter')} @@ -32,7 +32,7 @@ export function ChapterNav({ {next ? ( {t('reader.nextChapter')} diff --git a/apps/ebook/src/components/ChapterSidebar.tsx b/apps/ebook/src/components/ChapterSidebar.tsx index d83a4cf..873e06f 100644 --- a/apps/ebook/src/components/ChapterSidebar.tsx +++ b/apps/ebook/src/components/ChapterSidebar.tsx @@ -21,7 +21,7 @@ function scrollToId(id: string) { function scrollToChapterSlug(slug: string) { scrollToId(slug); - history.replaceState(null, '', `/read#${slug}`); + history.replaceState(null, '', `/book/read#${slug}`); } function ChapterNavList({ @@ -115,7 +115,7 @@ function ChapterNavList({ )} { e.preventDefault(); scrollToChapterSlug(chapter.slug); diff --git a/apps/ebook/src/components/ContinueReading.tsx b/apps/ebook/src/components/ContinueReading.tsx index 286a27c..7af4094 100644 --- a/apps/ebook/src/components/ContinueReading.tsx +++ b/apps/ebook/src/components/ContinueReading.tsx @@ -30,7 +30,7 @@ export function ContinueReading({ const readCount = progress?.readChapters.length ?? 0; const percent = calcBookProgress(readCount, book.chapters.length); const continueSlug = progress?.slug; - const continueHref = continueSlug ? `/read#${continueSlug}` : '/read'; + const continueHref = continueSlug ? `/book/read#${continueSlug}` : '/book/read'; const continueChapter = continueSlug ? book.chapters.find((c) => c.slug === continueSlug) diff --git a/apps/ebook/src/components/CoverChapterGrid.tsx b/apps/ebook/src/components/CoverChapterGrid.tsx index 1f1aa3d..855a6b8 100644 --- a/apps/ebook/src/components/CoverChapterGrid.tsx +++ b/apps/ebook/src/components/CoverChapterGrid.tsx @@ -28,7 +28,7 @@ export function CoverChapterGrid({ return ( {String(i + 1).padStart(2, '0')} diff --git a/apps/ebook/src/components/GrowthKit.tsx b/apps/ebook/src/components/GrowthKit.tsx index d94291a..7c990c1 100644 --- a/apps/ebook/src/components/GrowthKit.tsx +++ b/apps/ebook/src/components/GrowthKit.tsx @@ -36,7 +36,7 @@ export function ViralSharePrompt({ book }: { book: Book }) { const [show, setShow] = useState(false); const hashtags = book.viralTags.slice(0, 3).map((t) => `#${t}`).join(' '); const text = `${t('cover.readingNow', { title: book.title })} 📖\n${book.tagline}\n\n${hashtags}`; - const url = buildShareUrl('/read', 'viral_prompt'); + const url = buildShareUrl('/book/read', 'viral_prompt'); useEffect(() => { const onScroll = () => { diff --git a/apps/ebook/src/components/MarkdownContent.tsx b/apps/ebook/src/components/MarkdownContent.tsx index 30f64bf..db88c93 100644 --- a/apps/ebook/src/components/MarkdownContent.tsx +++ b/apps/ebook/src/components/MarkdownContent.tsx @@ -55,13 +55,13 @@ function CopyHeadingLink({ id }: { id: string }) { aria-label={t('reader.copyLink')} onClick={async (e) => { e.preventDefault(); - const url = `${window.location.origin}/read#${id}`; + const url = `${window.location.origin}/book/read#${id}`; try { await navigator.clipboard.writeText(url); } catch { /* ignore */ } - history.replaceState(null, '', `/read#${id}`); + history.replaceState(null, '', `/book/read#${id}`); setOk(true); window.setTimeout(() => setOk(false), 1200); }} diff --git a/apps/ebook/src/components/MobileChapterBar.tsx b/apps/ebook/src/components/MobileChapterBar.tsx index 9f6402d..e6480ec 100644 --- a/apps/ebook/src/components/MobileChapterBar.tsx +++ b/apps/ebook/src/components/MobileChapterBar.tsx @@ -15,14 +15,14 @@ export function MobileChapterBar({ return (
); diff --git a/apps/ebook/src/lib/metadata.ts b/apps/ebook/src/lib/metadata.ts index e860109..60643e7 100644 --- a/apps/ebook/src/lib/metadata.ts +++ b/apps/ebook/src/lib/metadata.ts @@ -38,10 +38,10 @@ export function buildChapterMetadata(slug: string, locale: Locale = 'zh'): Metad const chapter = getChapter(slug, locale); if (!chapter) return buildBookMetadata(locale); - const url = `${getSiteUrl()}/read#${slug}`; + const url = `${getSiteUrl()}/book/read#${slug}`; const title = `${chapter.emoji} ${chapter.title} · ${book.title}`; const description = chapter.hook; - const ogImage = `${getSiteUrl()}/read/opengraph-image`; + const ogImage = `${getSiteUrl()}/book/read/opengraph-image`; return { title, diff --git a/apps/ebook/src/lib/share-text.ts b/apps/ebook/src/lib/share-text.ts index 5660517..ca46c24 100644 --- a/apps/ebook/src/lib/share-text.ts +++ b/apps/ebook/src/lib/share-text.ts @@ -12,7 +12,7 @@ export function getBookShareText(book: Pick str: + out = text + for pat, repl in PATTERNS: + out = pat.sub(repl, out) + out = out.replace("/book/book/read", "/book/read") + return out + + +def main() -> None: + n = 0 + for base in TARGETS: + if not base.exists(): + continue + for path in base.rglob("*"): + if path.suffix not in {".ts", ".tsx", ".js", ".jsx"}: + continue + if "node_modules" in path.parts: + continue + raw = path.read_text(encoding="utf-8") + fixed = fix_text(raw) + if fixed != raw: + path.write_text(fixed, encoding="utf-8") + n += 1 + print("fixed", path.relative_to(ROOT)) + print(f"updated {n} files") + + +if __name__ == "__main__": + main()