Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
191 changes: 176 additions & 15 deletions app/api/revalidate/route.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,196 @@
import { revalidateTag } from 'next/cache'

import { NextRequest, NextResponse } from 'next/server'
import { revalidatePath, revalidateTag } from 'next/cache'
import { clearPathsCache } from '@/utils/strapi'

const REVALIDATE_SECRET = process.env.REVALIDATE_SECRET
interface RevalidationResult {
path?: string
tag?: string
revalidated: boolean
type: 'route' | 'path' | 'tag'
timestamp: string
}

export async function POST(request: NextRequest) {
try {
const requestData = await request.json()
const { tag, secret } = requestData
const body = await request.json()
const {
paths,
path,
tags,
tag,
secret,
revalidateAll = false,
clearCache = false
} = body

if (secret !== process.env.REVALIDATE_SECRET) {
return NextResponse.json(
{ message: 'Invalid secret' },
{ status: 401 }
)
}

const results: RevalidationResult[] = []

if (clearCache) {
clearPathsCache()
console.log('Cleared paths cache')
}

if (revalidateAll) {
revalidatePath('/', 'layout')
revalidateTag('mdx-content-list')
revalidateTag('mdx-paths')

results.push({
path: '/',
revalidated: true,
type: 'route',
timestamp: new Date().toISOString(),
})
}

if (path) {
revalidatePath(path)
revalidateTag(`mdx-content-${path}`)

results.push({
path,
revalidated: true,
type: 'path',
timestamp: new Date().toISOString(),
})
}

if (paths && Array.isArray(paths)) {
for (const p of paths) {
revalidatePath(p)
revalidateTag(`mdx-content-${p}`)

results.push({
path: p,
revalidated: true,
type: 'path',
timestamp: new Date().toISOString(),
})
}
}

if (tag) {
revalidateTag(tag)

// Check for secret if configured
if (REVALIDATE_SECRET && secret !== REVALIDATE_SECRET) {
return NextResponse.json({ message: 'Invalid revalidation token' }, { status: 401 })
results.push({
tag,
revalidated: true,
type: 'tag',
timestamp: new Date().toISOString(),
})
}

// Verify tag is provided
if (!tag) {
return NextResponse.json({ message: 'Missing tag parameter' }, { status: 400 })
if (tags && Array.isArray(tags)) {
for (const t of tags) {
revalidateTag(t)

results.push({
tag: t,
revalidated: true,
type: 'tag',
timestamp: new Date().toISOString(),
})
}
}

// Revalidate the tag
revalidateTag(tag)
console.log('Revalidation completed:', results)

return NextResponse.json({
revalidated: true,
results,
timestamp: new Date().toISOString(),
})
} catch (error) {
console.error('Revalidation error:', error)
return NextResponse.json(
{
message: 'Error revalidating paths',
error: error instanceof Error ? error.message : 'Unknown error'
},
{ status: 500 }
)
}
}

export async function GET(request: NextRequest) {
const { searchParams } = new URL(request.url)
const path = searchParams.get('path')
const tag = searchParams.get('tag')
const secret = searchParams.get('secret')
const revalidateAll = searchParams.get('revalidateAll') === 'true'
const clearCache = searchParams.get('clearCache') === 'true'

if (secret !== process.env.REVALIDATE_SECRET) {
return NextResponse.json(
{ message: 'Invalid secret' },
{ status: 401 }
)
}

try {
const results: RevalidationResult[] = []

if (clearCache) {
clearPathsCache()
console.log('Cleared paths cache')
}

if (revalidateAll) {
revalidatePath('/', 'layout')
revalidateTag('mdx-content-list')
revalidateTag('mdx-paths')

results.push({
path: '/',
revalidated: true,
type: 'route',
timestamp: new Date().toISOString(),
})
}

if (path) {
revalidatePath(path)
revalidateTag(`mdx-content-${path}`)

results.push({
path,
revalidated: true,
type: 'path',
timestamp: new Date().toISOString(),
})
}

if (tag) {
revalidateTag(tag)

results.push({
tag,
revalidated: true,
type: 'tag',
timestamp: new Date().toISOString(),
})
}

return NextResponse.json({
revalidated: true,
message: `Tag "${tag}" revalidated successfully`,
timestamp: Date.now(),
results,
timestamp: new Date().toISOString(),
})
} catch (error) {
console.error('Revalidation error:', error)
return NextResponse.json(
{ message: 'Error processing revalidation request', error: String(error) },
{
message: 'Error revalidating paths',
error: error instanceof Error ? error.message : 'Unknown error'
},
{ status: 500 }
)
}
Expand Down
4 changes: 2 additions & 2 deletions app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export default function RootLayout({ children }: { children: React.ReactNode })

<ThemeProviders>
<GrowthBookProvider>
<Suspense>
{/* <Suspense> */}
<SectionContainer>
<div className="relative flex h-screen flex-col justify-between ">
<SearchProvider searchConfig={siteMetadata.search as SearchConfig}>
Expand All @@ -117,7 +117,7 @@ export default function RootLayout({ children }: { children: React.ReactNode })
<MainFooter />
</div>
</SectionContainer>
</Suspense>
{/* </Suspense> */}
</GrowthBookProvider>
</ThemeProviders>
</body>
Expand Down
1 change: 0 additions & 1 deletion app/loading.tsx

This file was deleted.

Loading