Refactors URL slug case sensitivity implementation: - Moves case conversion to creation time instead of lookup - Updates middleware to handle case sensitivity more efficiently - Adds configuration docs for case sensitivity and list query limits - Simplifies nanoid implementation to always use lowercase This change improves performance and consistency in URL handling while maintaining backwards compatibility with existing links.
43 lines
1.6 KiB
TypeScript
43 lines
1.6 KiB
TypeScript
import type { LinkSchema } from '@/schemas/link'
|
|
import type { z } from 'zod'
|
|
import { parsePath, withQuery } from 'ufo'
|
|
|
|
export default eventHandler(async (event) => {
|
|
const { pathname: slug } = parsePath(event.path.replace(/^\/|\/$/g, '')) // remove leading and trailing slashes
|
|
const { slugRegex, reserveSlug } = useAppConfig(event)
|
|
const { homeURL, linkCacheTtl, redirectWithQuery, caseSensitive } = useRuntimeConfig(event)
|
|
const { cloudflare } = event.context
|
|
|
|
if (event.path === '/' && homeURL)
|
|
return sendRedirect(event, homeURL)
|
|
|
|
if (slug && !reserveSlug.includes(slug) && slugRegex.test(slug) && cloudflare) {
|
|
const { KV } = cloudflare.env
|
|
|
|
let link: z.infer<typeof LinkSchema> | null = null
|
|
|
|
const getLink = async (key: string) =>
|
|
await KV.get(`link:${key}`, { type: 'json', cacheTtl: linkCacheTtl })
|
|
|
|
const lowerCaseSlug = slug.toLowerCase()
|
|
link = await getLink(caseSensitive ? slug : lowerCaseSlug)
|
|
|
|
// fallback to original slug if caseSensitive is false and the slug is not found
|
|
if (!caseSensitive && !link && lowerCaseSlug !== slug) {
|
|
console.log('original slug fallback:', `slug:${slug} lowerCaseSlug:${lowerCaseSlug}`)
|
|
link = await getLink(slug)
|
|
}
|
|
|
|
if (link) {
|
|
event.context.link = link
|
|
try {
|
|
await useAccessLog(event)
|
|
}
|
|
catch (error) {
|
|
console.error('Failed write access log:', error)
|
|
}
|
|
const target = redirectWithQuery ? withQuery(link.url, getQuery(event)) : link.url
|
|
return sendRedirect(event, target, +useRuntimeConfig(event).redirectStatusCode)
|
|
}
|
|
}
|
|
})
|