refactor: migrate to pnpm monorepo with Payload CMS backend and Astro frontend to support scalable website development and AI-assisted workflows
This commit is contained in:
27
apps/backend/src/hooks/formatSlug.ts
Normal file
27
apps/backend/src/hooks/formatSlug.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import type { FieldHook } from 'payload'
|
||||
|
||||
const format = (val: string): string =>
|
||||
val
|
||||
.replace(/ /g, '-')
|
||||
.replace(/[^\w-]+/g, '')
|
||||
.toLowerCase()
|
||||
|
||||
const formatSlug =
|
||||
(fallback: string): FieldHook =>
|
||||
({ data, operation, originalDoc, value }) => {
|
||||
if (typeof value === 'string') {
|
||||
return format(value)
|
||||
}
|
||||
|
||||
if (operation === 'create') {
|
||||
const fallbackData = data?.[fallback] || originalDoc?.[fallback]
|
||||
|
||||
if (fallbackData && typeof fallbackData === 'string') {
|
||||
return format(fallbackData)
|
||||
}
|
||||
}
|
||||
|
||||
return value
|
||||
}
|
||||
|
||||
export default formatSlug
|
||||
15
apps/backend/src/hooks/populatePublishedAt.ts
Normal file
15
apps/backend/src/hooks/populatePublishedAt.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import type { CollectionBeforeChangeHook } from 'payload'
|
||||
|
||||
export const populatePublishedAt: CollectionBeforeChangeHook = ({ data, operation, req }) => {
|
||||
if (operation === 'create' || operation === 'update') {
|
||||
if (req.data && !req.data.publishedAt) {
|
||||
const now = new Date()
|
||||
return {
|
||||
...data,
|
||||
publishedAt: now,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return data
|
||||
}
|
||||
11
apps/backend/src/hooks/revalidateRedirects.ts
Normal file
11
apps/backend/src/hooks/revalidateRedirects.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import type { CollectionAfterChangeHook } from 'payload'
|
||||
|
||||
import { revalidateTag } from 'next/cache'
|
||||
|
||||
export const revalidateRedirects: CollectionAfterChangeHook = ({ doc, req: { payload } }) => {
|
||||
payload.logger.info(`Revalidating redirects`)
|
||||
|
||||
revalidateTag('redirects')
|
||||
|
||||
return doc
|
||||
}
|
||||
Reference in New Issue
Block a user