171 lines
5.6 KiB
Python
171 lines
5.6 KiB
Python
#!/usr/bin/env python3
|
|
"""Show current state of all hooks then rewrite them cleanly."""
|
|
import os
|
|
|
|
def read(p):
|
|
if not os.path.exists(p): return None
|
|
with open(p) as f: return f.read()
|
|
|
|
def write(p, content):
|
|
os.makedirs(os.path.dirname(p), exist_ok=True)
|
|
with open(p, 'w') as f: f.write(content)
|
|
print(f" ✓ {p}")
|
|
|
|
print("=== Current state of hook files ===\n")
|
|
hooks = [
|
|
'src/Footer/hooks/revalidateFooter.ts',
|
|
'src/Header/hooks/revalidateHeader.ts',
|
|
'src/globals/PopupAnnouncement/hooks/revalidatePopup.ts',
|
|
'src/hooks/revalidateRedirects.ts',
|
|
'src/collections/Pages/hooks/revalidatePage.ts',
|
|
'src/collections/Posts/hooks/revalidatePost.ts',
|
|
]
|
|
for h in hooks:
|
|
content = read(h)
|
|
if content:
|
|
print(f"--- {h} ---")
|
|
print(content)
|
|
print()
|
|
else:
|
|
print(f"--- {h} --- MISSING\n")
|
|
|
|
print("\n=== Rewriting all hooks cleanly ===\n")
|
|
|
|
write('src/Footer/hooks/revalidateFooter.ts', """\
|
|
import type { GlobalAfterChangeHook } from 'payload'
|
|
import { revalidateTag } from 'next/cache'
|
|
|
|
export const revalidateFooter: GlobalAfterChangeHook = ({ req: { payload } }) => {
|
|
payload.logger.info({ msg: 'Revalidating footer' })
|
|
;(revalidateTag as any)('global_footer')
|
|
}
|
|
""")
|
|
|
|
write('src/Header/hooks/revalidateHeader.ts', """\
|
|
import type { GlobalAfterChangeHook } from 'payload'
|
|
import { revalidateTag } from 'next/cache'
|
|
|
|
export const revalidateHeader: GlobalAfterChangeHook = ({ req: { payload } }) => {
|
|
payload.logger.info({ msg: 'Revalidating header' })
|
|
;(revalidateTag as any)('global_header')
|
|
}
|
|
""")
|
|
|
|
write('src/globals/PopupAnnouncement/hooks/revalidatePopup.ts', """\
|
|
import type { GlobalAfterChangeHook } from 'payload'
|
|
import { revalidateTag } from 'next/cache'
|
|
|
|
export const revalidatePopup: GlobalAfterChangeHook = ({ req: { payload } }) => {
|
|
payload.logger.info({ msg: 'Revalidating popup announcement' })
|
|
;(revalidateTag as any)('global_popup-announcement')
|
|
}
|
|
""")
|
|
|
|
write('src/hooks/revalidateRedirects.ts', """\
|
|
import type { CollectionAfterChangeHook } from 'payload'
|
|
import { revalidateTag } from 'next/cache'
|
|
|
|
export const revalidateRedirects: CollectionAfterChangeHook = () => {
|
|
;(revalidateTag as any)('redirects')
|
|
}
|
|
""")
|
|
|
|
write('src/collections/Pages/hooks/revalidatePage.ts', """\
|
|
import type { CollectionAfterChangeHook, CollectionAfterDeleteHook } from 'payload'
|
|
import { revalidatePath, revalidateTag } from 'next/cache'
|
|
import type { Page } from '../../../payload-types'
|
|
|
|
export const revalidatePage: CollectionAfterChangeHook<Page> = ({
|
|
doc,
|
|
previousDoc,
|
|
req: { payload, context },
|
|
}) => {
|
|
if (!context.disableRevalidate) {
|
|
if (doc._status === 'published') {
|
|
const path = doc.slug === 'home' ? '/' : `/${doc.slug}`
|
|
payload.logger.info({ msg: `Revalidating page at path: ${path}` })
|
|
;(revalidatePath as any)(path, 'page')
|
|
;(revalidateTag as any)('pages-sitemap')
|
|
}
|
|
|
|
if (previousDoc?._status === 'published' && doc._status !== 'published') {
|
|
const oldPath = previousDoc.slug === 'home' ? '/' : `/${previousDoc.slug}`
|
|
payload.logger.info({ msg: `Revalidating old page at path: ${oldPath}` })
|
|
;(revalidatePath as any)(oldPath, 'page')
|
|
;(revalidateTag as any)('pages-sitemap')
|
|
}
|
|
}
|
|
return doc
|
|
}
|
|
|
|
export const revalidateDelete: CollectionAfterDeleteHook<Page> = ({ doc, req: { context } }) => {
|
|
if (!context.disableRevalidate) {
|
|
const path = doc?.slug === 'home' ? '/' : `/${doc?.slug}`
|
|
;(revalidatePath as any)(path, 'page')
|
|
;(revalidateTag as any)('pages-sitemap')
|
|
}
|
|
return doc
|
|
}
|
|
""")
|
|
|
|
write('src/collections/Posts/hooks/revalidatePost.ts', """\
|
|
import type { CollectionAfterChangeHook, CollectionAfterDeleteHook } from 'payload'
|
|
import { revalidatePath, revalidateTag } from 'next/cache'
|
|
import type { Post } from '../../../payload-types'
|
|
|
|
export const revalidatePost: CollectionAfterChangeHook<Post> = ({
|
|
doc,
|
|
previousDoc,
|
|
req: { payload, context },
|
|
}) => {
|
|
if (!context.disableRevalidate) {
|
|
if (doc._status === 'published') {
|
|
const path = `/posts/${doc.slug}`
|
|
payload.logger.info({ msg: `Revalidating post at path: ${path}` })
|
|
;(revalidatePath as any)(path, 'page')
|
|
;(revalidateTag as any)('posts-sitemap')
|
|
}
|
|
|
|
if (previousDoc?._status === 'published' && doc._status !== 'published') {
|
|
const oldPath = `/posts/${previousDoc.slug}`
|
|
payload.logger.info({ msg: `Revalidating old post at path: ${oldPath}` })
|
|
;(revalidatePath as any)(oldPath, 'page')
|
|
;(revalidateTag as any)('posts-sitemap')
|
|
}
|
|
}
|
|
return doc
|
|
}
|
|
|
|
export const revalidateDelete: CollectionAfterDeleteHook<Post> = ({ doc, req: { context } }) => {
|
|
if (!context.disableRevalidate) {
|
|
const path = `/posts/${doc?.slug}`
|
|
;(revalidatePath as any)(path, 'page')
|
|
;(revalidateTag as any)('posts-sitemap')
|
|
}
|
|
return doc
|
|
}
|
|
""")
|
|
|
|
# Also verify the global configs import the right export names
|
|
print("\n=== Checking global config import names ===\n")
|
|
configs = {
|
|
'src/Footer/config.ts': 'revalidateFooter',
|
|
'src/Header/config.ts': 'revalidateHeader',
|
|
'src/globals/PopupAnnouncement/config.ts': 'revalidatePopup',
|
|
}
|
|
for path, expected_export in configs.items():
|
|
content = read(path)
|
|
if not content:
|
|
print(f" ⚠ {path} not found")
|
|
continue
|
|
if expected_export in content:
|
|
print(f" ✓ {path} imports '{expected_export}' correctly")
|
|
else:
|
|
# Find what it actually imports
|
|
import re
|
|
imports = re.findall(r'import \{([^}]+)\}', content)
|
|
print(f" ⚠ {path} does NOT import '{expected_export}'. Found: {imports}")
|
|
print(f" Content:\n{content}\n")
|
|
|
|
print("\n=== Done! Run: npm run dev ===")
|