fix: getIdBySlug in api slug2id

This commit is contained in:
DIYgod 2023-05-09 01:47:36 +01:00
parent cc818a9f6b
commit be24125bd7
No known key found for this signature in database
7 changed files with 64 additions and 61 deletions

View File

@ -82,7 +82,7 @@ export async function GET(req: Request): Promise<Response> {
if (IS_VERCEL_PREVIEW) {
const path = new URL(link).pathname
redirect(`/_site/${character.handle}${path}`)
redirect(`/site/${character.handle}${path}`)
} else {
redirect(link)
}

View File

@ -1,57 +1,5 @@
import { getNoteSlug } from "~/lib/helpers"
import { cacheDelete, cacheGet } from "~/lib/redis.server"
import { NextServerResponse, getQuery } from "~/lib/server-helper"
export async function getIdBySlug(slug: string, characterId: string | number) {
slug = (slug as string)?.toLowerCase?.()
const result = (await cacheGet({
key: ["slug2id", characterId, slug],
getValueFun: async () => {
let note
let cursor = ""
do {
const response = await (
await fetch(
`https://indexer.crossbell.io/v1/notes?characterId=${characterId}&sources=xlog&cursor=${cursor}&limit=100`,
)
).json()
cursor = response.cursor
note = response?.list?.find(
(item: any) =>
slug === getNoteSlug(item) ||
slug === `${characterId}-${item.noteId}`,
)
} while (!note && cursor)
return {
noteId: note?.noteId,
}
},
noUpdate: true,
})) as {
noteId: number
}
// revalidate
if (result) {
const noteIdMatch = slug.match(`^${characterId}-(\\d+)$`)
if (!noteIdMatch?.[1]) {
fetch(
`https://indexer.crossbell.io/v1/characters/${characterId}/notes/${result.noteId}`,
)
.then((res) => res.json())
.then((note) => {
if ((note && getNoteSlug(note) !== slug) || note.deleted) {
cacheDelete(["slug2id", characterId + "", slug])
}
})
}
}
return result
}
import { getIdBySlug } from "~/queries/page.server"
// GET /api/slug2id?characterId=52055&slug=note-144
export async function GET(req: Request): Promise<Response> {

View File

@ -505,7 +505,7 @@ export default function SubdomainEditor() {
const onPreviewButtonClick = useCallback(() => {
window.open(
`/_site/${subdomain}/preview/${draftKey.replace(
`/site/${subdomain}/preview/${draftKey.replace(
`draft-${site.data?.characterId}-`,
"",
)}`,

View File

@ -17,6 +17,10 @@ export default function SitePreviewPage({
const page = useGetPage({
characterId: site.data?.characterId,
noteId:
params.previewId && /\d+/.test(params.previewId)
? +params.previewId
: undefined,
slug: params.previewId,
useStat: true,
})

View File

@ -158,7 +158,7 @@ export const PagesManagerMenu: FC<{
onClick() {
const slug = getNoteSlugFromNote(page)
if (!slug) return
window.open(`/_site/${subdomain}/${slug}`)
window.open(`/site/${subdomain}/${slug}`)
},
},
{

View File

@ -15,7 +15,7 @@ export const getSiteLink = ({
subdomain: string
noProtocol?: boolean
}) => {
if (IS_VERCEL_PREVIEW) return `/_site/${subdomain}`
if (IS_VERCEL_PREVIEW) return `/site/${subdomain}`
if (domain) {
return `https://${domain}`
@ -30,11 +30,11 @@ export const getSiteLink = ({
export const getSlugUrl = (slug: string) => {
if (!isServerSide() && IS_VERCEL_PREVIEW) {
const pathArr = new URL(location.href).pathname.split("/").filter(($) => $)
const indicatorIndex = pathArr.findIndex(($) => $ === "_site")
const indicatorIndex = pathArr.findIndex(($) => $ === "site")
if (-~indicatorIndex) {
const handle = pathArr[indicatorIndex + 1]
return `/_site/${handle}${slug}`
return `/site/${handle}${slug}`
}
}

View File

@ -1,10 +1,61 @@
import { QueryClient } from "@tanstack/react-query"
import { getIdBySlug } from "~/app/api/slug2id/route"
import { getSummary } from "~/app/api/summary/route"
import { cacheGet } from "~/lib/redis.server"
import { getNoteSlug } from "~/lib/helpers"
import { cacheDelete, cacheGet } from "~/lib/redis.server"
import * as pageModel from "~/models/page.model"
export async function getIdBySlug(slug: string, characterId: string | number) {
slug = (slug as string)?.toLowerCase?.()
const result = (await cacheGet({
key: ["slug2id", characterId, slug],
getValueFun: async () => {
let note
let cursor = ""
do {
const response = await (
await fetch(
`https://indexer.crossbell.io/v1/notes?characterId=${characterId}&sources=xlog&cursor=${cursor}&limit=100`,
)
).json()
cursor = response.cursor
note = response?.list?.find(
(item: any) =>
slug === getNoteSlug(item) ||
slug === `${characterId}-${item.noteId}`,
)
} while (!note && cursor)
return {
noteId: note?.noteId,
}
},
noUpdate: true,
})) as {
noteId: number
}
// revalidate
if (result) {
const noteIdMatch = slug.match(`^${characterId}-(\\d+)$`)
if (!noteIdMatch?.[1]) {
fetch(
`https://indexer.crossbell.io/v1/characters/${characterId}/notes/${result.noteId}`,
)
.then((res) => res.json())
.then((note) => {
if ((note && getNoteSlug(note) !== slug) || note.deleted) {
cacheDelete(["slug2id", characterId + "", slug])
}
})
}
}
return result
}
export const fetchGetPage = async (
input: Partial<Parameters<typeof pageModel.getPage>[0]>,
queryClient: QueryClient,