From 4e262702aadcaf737f1305597149b5b638f05392 Mon Sep 17 00:00:00 2001 From: shigahi Date: Mon, 20 Apr 2026 12:26:06 +0200 Subject: [PATCH] Fix pretty slug pages rendering root content (#90) The getPublicPageData handler was force-injecting the root page's blockId whenever the client omitted it. The client omits blockId for every URL without a 32-hex page ID, including pretty slug URLs like /about, so all slug pages rendered the root page. Resolve the current page from the Referer header: map the referring path to a slug in SLUG_TO_PAGE, fall back to a 32-hex match, and only use the root page when nothing else matches. --- src/code.ts | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/src/code.ts b/src/code.ts index 80af47e..ff2fdc2 100644 --- a/src/code.ts +++ b/src/code.ts @@ -634,12 +634,31 @@ ${ // Rewrite request body: replace custom space domain with original Notion space domain let reqBody = await request.text(); reqBody = reqBody.replace(new RegExp(CUSTOM_SPACE_DOMAIN, 'g'), NOTION_SPACE_DOMAIN); - // Inject root page blockId if not present (client sends spaceDomain only for root URL) + // Inject correct blockId if absent. The client omits blockId whenever the + // browser URL doesn't contain a 32-hex page ID (root "/" and pretty slugs + // like "/about"). Without this, all slug pages render the root page (#90). try { const reqJson = JSON.parse(reqBody); - if (!reqJson.blockId && SLUG_TO_PAGE['']) { - reqJson.blockId = SLUG_TO_PAGE[''].replace(/(.{8})(.{4})(.{4})(.{4})(.{12})/, '$1-$2-$3-$4-$5'); - reqBody = JSON.stringify(reqJson); + if (!reqJson.blockId) { + let pageId = ''; + const referer = request.headers.get('Referer'); + if (referer) { + try { + const refPath = new URL(referer).pathname; + const slug = decodeURIComponent(refPath.slice(1)); + if (SLUG_TO_PAGE.hasOwnProperty(slug)) { + pageId = SLUG_TO_PAGE[slug]; + } else { + const match = refPath.match(/[0-9a-f]{32}/); + if (match) pageId = match[0]; + } + } catch (e) {} + } + if (!pageId) pageId = SLUG_TO_PAGE[''] || ''; + if (pageId) { + reqJson.blockId = pageId.replace(/(.{8})(.{4})(.{4})(.{4})(.{12})/, '$1-$2-$3-$4-$5'); + reqBody = JSON.stringify(reqJson); + } } } catch (e) {} // Proxy getPublicPageData and rewrite domain info