Skip to content

Commit

Permalink
add revalidation for redirects
Browse files Browse the repository at this point in the history
  • Loading branch information
paulpopus committed May 23, 2024
1 parent d6a40bc commit 7a526e3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
9 changes: 9 additions & 0 deletions templates/website/src/_payload/hooks/revalidateRedirect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { CollectionAfterChangeHook } from 'payload/types'

import { revalidate } from '../utilities/revalidate'

export const revalidateRedirect: CollectionAfterChangeHook = ({ doc, req: { payload } }) => {
void revalidate({ type: 'tag', collection: 'redirects', payload, tag: `redirects_${doc.from}` })

return doc
}
6 changes: 6 additions & 0 deletions templates/website/src/payload.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { BoldFeature } from '@payloadcms/richtext-lexical'
import dotenv from 'dotenv'
import path from 'path'
import { buildConfig } from 'payload/config'
import { revalidateRedirect } from 'src/_payload/hooks/revalidateRedirect'
import { fileURLToPath } from 'url'

import Categories from './_payload/collections/Categories'
Expand Down Expand Up @@ -83,6 +84,11 @@ export default buildConfig({
plugins: [
redirectsPlugin({
collections: ['pages', 'posts'],
overrides: {
hooks: {
afterChange: [revalidateRedirect],
},
},
}),
nestedDocsPlugin({
collections: ['categories'],
Expand Down

0 comments on commit 7a526e3

Please sign in to comment.