From 9490d032c77f51c07c7ddb945c832833a6b949c6 Mon Sep 17 00:00:00 2001 From: DIYgod Date: Fri, 18 Nov 2022 16:06:57 +0000 Subject: [PATCH] fix: local page not found --- src/models/page.model.ts | 57 ++++++++++++----------- src/pages/_site/[site]/preview/[page].tsx | 2 +- src/pages/api/slug2id.ts | 42 +++++++++++------ 3 files changed, 59 insertions(+), 42 deletions(-) diff --git a/src/models/page.model.ts b/src/models/page.model.ts index a531c9f7..320c0db6 100644 --- a/src/models/page.model.ts +++ b/src/models/page.model.ts @@ -301,21 +301,39 @@ export async function getPage( return null } - if (!input.pageId) { - const slug2Id = ( - await axios.get("/api/slug2id", { - params: { - handle: input.site, - slug: input.page, - }, - }) - ).data - if (!slug2Id?.noteId) { - return null + const mustLocal = input.pageId?.startsWith("local-") + + let page: Note | null = null + + if (!mustLocal) { + // on-chain page + if (!input.pageId) { + const slug2Id = ( + await axios.get("/api/slug2id", { + params: { + handle: input.site, + slug: input.page, + }, + }) + ).data + if (!slug2Id?.noteId) { + return null + } + input.pageId = `${slug2Id.characterId}-${slug2Id.noteId}` } - input.pageId = `${slug2Id.characterId}-${slug2Id.noteId}` + + const pages = await (customUnidata || unidata).notes.get({ + source: "Crossbell Note", + identity: input.site, + platform: "Crossbell", + filter: { + id: input.pageId, + }, + }) + page = pages?.list[0] || null } + // local page const local = getLocalPages({ site: input.site, }) @@ -323,21 +341,6 @@ export async function getPage( (page) => page.id === input.page || page.id === input.pageId, ) - const mustLocal = input.pageId?.startsWith("local-") - - const pages: Notes | null = mustLocal - ? null - : await (customUnidata || unidata).notes.get({ - source: "Crossbell Note", - identity: input.site, - platform: "Crossbell", - filter: { - id: input.pageId, - }, - }) - - let page = pages?.list[0] || null - if (localPage) { if (page) { if (new Date(localPage.date_updated) > new Date(page.date_updated)) { diff --git a/src/pages/_site/[site]/preview/[page].tsx b/src/pages/_site/[site]/preview/[page].tsx index 2133c3eb..90444e1a 100644 --- a/src/pages/_site/[site]/preview/[page].tsx +++ b/src/pages/_site/[site]/preview/[page].tsx @@ -11,7 +11,7 @@ function SitePagePage() { const page = useGetPage({ site: site, - page: pageSlug, + pageId: pageSlug, }) return diff --git a/src/pages/api/slug2id.ts b/src/pages/api/slug2id.ts index 8fefa2a9..0bf4a369 100644 --- a/src/pages/api/slug2id.ts +++ b/src/pages/api/slug2id.ts @@ -27,6 +27,14 @@ export async function getIdBySlug(slug: string, handle: string) { ).json() const cid = characterRes?.characterId + const noteIdMatch = slug.match(`^${cid}-(\\d+)$`) + if (noteIdMatch?.[1]) { + return { + noteId: noteIdMatch?.[1], + characterId: cid, + } + } + do { const response = await ( await fetch( @@ -35,7 +43,8 @@ export async function getIdBySlug(slug: string, handle: string) { ).json() cursor = response.cursor note = response?.list?.find( - (item: any) => slug === (getSlug(item) || `${cid}-${item.noteId}`), + (item: any) => + slug === getSlug(item) || slug === `${cid}-${item.noteId}`, ) } while (!note && cursor) @@ -49,22 +58,27 @@ export async function getIdBySlug(slug: string, handle: string) { true, ) + // revalidate if (result) { - fetch( - `https://indexer.crossbell.io/v1/characters/${result.characterId}/notes/${result.noteId}`, - ) - .then((res) => res.json()) - .then((note) => { - if (note) { - const currentSlug = - getSlug(note) || `${result.characterId}-${note.noteId}` - if (currentSlug !== slug) { + const noteIdMatch = slug.match(`^${result.characterId}-(\\d+)$`) + if (!noteIdMatch?.[1]) { + fetch( + `https://indexer.crossbell.io/v1/characters/${result.characterId}/notes/${result.noteId}`, + ) + .then((res) => res.json()) + .then((note) => { + if (note && getSlug(note) !== slug) { cacheDelete(["slug2id", handle, slug]) } - } else { - cacheDelete(["slug2id", handle, slug]) - } - }) + }) + fetch(`https://indexer.crossbell.io/v1/characters/${result.characterId}`) + .then((res) => res.json()) + .then((character) => { + if (character && character.handle !== handle) { + cacheDelete(["slug2id", handle, slug]) + } + }) + } } return result