Add preview handling

This commit is contained in:
nicwands
2026-06-01 11:08:03 -04:00
parent ef70a0c756
commit 6776a14757
18 changed files with 1186 additions and 14 deletions

View File

@@ -0,0 +1,55 @@
import { getBySlug, getByDocumentId } from '@/libs/strapi'
export const data = async (pageContext) => {
const { routeParams, urlParsed } = pageContext
const { slug } = routeParams
const searchParams = urlParsed.search || {}
// Extract preview parameters from URL
const isPreview = searchParams.preview === 'true'
const status = searchParams.status || 'published'
const documentId = searchParams.documentId
const uid = searchParams.uid
try {
let article = null
// If we have a documentId from preview, fetch by document ID
// This ensures we get the exact document being previewed
if (isPreview && documentId && uid === 'api::article.article') {
const response = await getByDocumentId('articles', documentId, {
isPreview,
status,
isSingle: false
})
article = response.data?.[0] || null
} else {
// Normal fetch by slug
const response = await getBySlug('articles', slug, {
isPreview,
status
})
article = response.data?.[0] || null
}
return {
article,
// Pass preview state to the component
isPreview,
status,
documentId,
uid
}
} catch (error) {
console.error('Error fetching article:', error)
// Return null article but preserve preview state for proper UI rendering
return {
article: null,
isPreview,
status,
documentId,
uid
}
}
}