fix: local page not found

This commit is contained in:
DIYgod
2022-11-18 16:06:57 +00:00
parent 43e1ef377a
commit 9490d032c7
3 changed files with 59 additions and 42 deletions
+30 -27
View File
@@ -301,21 +301,39 @@ export async function getPage<TRender extends boolean = false>(
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<TRender extends boolean = false>(
(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)) {
+1 -1
View File
@@ -11,7 +11,7 @@ function SitePagePage() {
const page = useGetPage({
site: site,
page: pageSlug,
pageId: pageSlug,
})
return <SitePage page={page.data} />
+28 -14
View File
@@ -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