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

85
pages/@slug/+Page.vue Normal file
View File

@@ -0,0 +1,85 @@
<template>
<main class="page">
<article v-if="page" class="page__content">
<header v-if="page.title" class="page__header">
<h1 class="page__title">{{ page.title }}</h1>
</header>
<div v-if="page.content" class="page__body">
<strapi-blocks :content="page.content" />
</div>
</article>
<div v-else class="page-not-found">
<h1>Page not found</h1>
<p>The requested page could not be found.</p>
</div>
</main>
</template>
<script setup>
import { useData } from 'vike-vue/useData'
const { page } = useData()
</script>
<style lang="scss" scoped>
.page {
max-width: 1200px;
margin: 0 auto;
padding: 2rem;
&__header {
margin-bottom: 3rem;
text-align: center;
}
&__title {
font-size: 3rem;
font-weight: 700;
line-height: 1.2;
margin: 0;
color: var(--theme-fg);
}
&__body {
line-height: 1.6;
:deep(h2) {
font-size: 2rem;
margin: 2.5rem 0 1rem 0;
}
:deep(h3) {
font-size: 1.5rem;
margin: 2rem 0 0.75rem 0;
}
:deep(p) {
margin: 1.25rem 0;
font-size: 1.1rem;
}
:deep(ul, ol) {
margin: 1.25rem 0;
padding-left: 2rem;
}
}
}
.page-not-found {
text-align: center;
padding: 4rem 0;
h1 {
font-size: 2.5rem;
margin-bottom: 1rem;
color: var(--theme-fg);
}
p {
font-size: 1.1rem;
color: var(--theme-fg-muted, #666);
}
}
</style>

54
pages/@slug/+data.js Normal file
View File

@@ -0,0 +1,54 @@
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 page = null
// If we have a documentId from preview, fetch by document ID
if (isPreview && documentId && uid === 'api::page.page') {
const response = await getByDocumentId('pages', documentId, {
isPreview,
status,
isSingle: false
})
page = response.data?.[0] || null
} else {
// Normal fetch by slug
const response = await getBySlug('pages', slug, {
isPreview,
status
})
page = response.data?.[0] || null
}
return {
page,
// Pass preview state to the component
isPreview,
status,
documentId,
uid
}
} catch (error) {
console.error('Error fetching page:', error)
// Return null page but preserve preview state
return {
page: null,
isPreview,
status,
documentId,
uid
}
}
}