Add preview handling
This commit is contained in:
@@ -1,6 +1,11 @@
|
||||
<!-- https://vike.dev/Head -->
|
||||
|
||||
<template>
|
||||
<!-- Allow the app to be embedded in iframes from Strapi admin -->
|
||||
<meta
|
||||
http-equiv="Content-Security-Policy"
|
||||
content="frame-ancestors 'self' https://cms.takerofnotes.com"
|
||||
/>
|
||||
<link rel="icon" href="/favicon.png" />
|
||||
</template>
|
||||
|
||||
|
||||
@@ -1,8 +1,14 @@
|
||||
<template>
|
||||
<lenis root>
|
||||
<preview-banner
|
||||
v-if="data"
|
||||
:is-preview="data.isPreview"
|
||||
:status="data.status"
|
||||
/>
|
||||
<div :class="classes">
|
||||
<slot />
|
||||
</div>
|
||||
<LivePreview v-if="data?.isPreview" />
|
||||
</lenis>
|
||||
</template>
|
||||
|
||||
@@ -11,7 +17,12 @@ import '@/styles/main.scss'
|
||||
import { ref, computed, onMounted } from 'vue'
|
||||
import loadFonts from '@fuzzco/font-loader'
|
||||
import { useWindowSize } from '@vueuse/core'
|
||||
import { useData } from 'vike-vue/useData'
|
||||
import Lenis from '@/components/Lenis.vue'
|
||||
import PreviewBanner from '@/components/PreviewBanner.vue'
|
||||
import LivePreview from '@/components/LivePreview.vue'
|
||||
|
||||
const data = useData()
|
||||
|
||||
const { height } = useWindowSize()
|
||||
|
||||
|
||||
@@ -1,14 +1,13 @@
|
||||
import type { Config } from "vike/types";
|
||||
import vikeVue from "vike-vue/config";
|
||||
import type { Config } from 'vike/types'
|
||||
import vikeVue from 'vike-vue/config'
|
||||
|
||||
// Default config (can be overridden by pages)
|
||||
// https://vike.dev/config
|
||||
|
||||
export default {
|
||||
// https://vike.dev/head-tags
|
||||
title: "My Vike App",
|
||||
description: "Demo showcasing Vike",
|
||||
title: 'My Vike App',
|
||||
description: 'Demo showcasing Vike',
|
||||
|
||||
prerender: true,
|
||||
extends: [vikeVue],
|
||||
} as Config;
|
||||
prerender: true,
|
||||
extends: [vikeVue],
|
||||
} as Config
|
||||
|
||||
85
pages/@slug/+Page.vue
Normal file
85
pages/@slug/+Page.vue
Normal 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
54
pages/@slug/+data.js
Normal 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
|
||||
}
|
||||
}
|
||||
}
|
||||
104
pages/articles/@slug/+Page.vue
Normal file
104
pages/articles/@slug/+Page.vue
Normal file
@@ -0,0 +1,104 @@
|
||||
<template>
|
||||
<main class="article-page">
|
||||
<article v-if="article" class="article">
|
||||
<header class="article__header">
|
||||
<h1 class="article__title">{{ article.title }}</h1>
|
||||
<div v-if="article.publishedAt" class="article__meta">
|
||||
<time :datetime="article.publishedAt">
|
||||
{{ formatDate(article.publishedAt) }}
|
||||
</time>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<div v-if="article.content" class="article__content">
|
||||
<strapi-blocks :content="article.content" />
|
||||
</div>
|
||||
</article>
|
||||
|
||||
<div v-else class="article-not-found">
|
||||
<h1>Article not found</h1>
|
||||
<p>The requested article could not be found.</p>
|
||||
</div>
|
||||
</main>
|
||||
</template>
|
||||
|
||||
<script setup>
|
||||
import { useData } from 'vike-vue/useData'
|
||||
|
||||
const { article } = useData()
|
||||
|
||||
const formatDate = (dateString) => {
|
||||
return new Date(dateString).toLocaleDateString('en-US', {
|
||||
year: 'numeric',
|
||||
month: 'long',
|
||||
day: 'numeric'
|
||||
})
|
||||
}
|
||||
</script>
|
||||
|
||||
<style lang="scss" scoped>
|
||||
.article-page {
|
||||
max-width: 800px;
|
||||
margin: 0 auto;
|
||||
padding: 2rem;
|
||||
}
|
||||
|
||||
.article {
|
||||
&__header {
|
||||
margin-bottom: 2rem;
|
||||
border-bottom: 1px solid var(--theme-border, #e0e0e0);
|
||||
padding-bottom: 1rem;
|
||||
}
|
||||
|
||||
&__title {
|
||||
font-size: 2.5rem;
|
||||
font-weight: 700;
|
||||
line-height: 1.2;
|
||||
margin: 0 0 1rem 0;
|
||||
color: var(--theme-fg);
|
||||
}
|
||||
|
||||
&__meta {
|
||||
color: var(--theme-fg-muted, #666);
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
|
||||
&__content {
|
||||
line-height: 1.6;
|
||||
|
||||
:deep(h2) {
|
||||
font-size: 1.8rem;
|
||||
margin: 2rem 0 1rem 0;
|
||||
}
|
||||
|
||||
:deep(h3) {
|
||||
font-size: 1.4rem;
|
||||
margin: 1.5rem 0 0.75rem 0;
|
||||
}
|
||||
|
||||
:deep(p) {
|
||||
margin: 1rem 0;
|
||||
}
|
||||
|
||||
:deep(ul, ol) {
|
||||
margin: 1rem 0;
|
||||
padding-left: 2rem;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.article-not-found {
|
||||
text-align: center;
|
||||
padding: 4rem 0;
|
||||
|
||||
h1 {
|
||||
font-size: 2rem;
|
||||
margin-bottom: 1rem;
|
||||
color: var(--theme-fg);
|
||||
}
|
||||
|
||||
p {
|
||||
color: var(--theme-fg-muted, #666);
|
||||
}
|
||||
}
|
||||
</style>
|
||||
55
pages/articles/@slug/+data.js
Normal file
55
pages/articles/@slug/+data.js
Normal 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
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,28 @@
|
||||
import { getGlobal } from '@/libs/strapi'
|
||||
|
||||
export const data = async () => {
|
||||
const global = await getGlobal()
|
||||
export const data = async (pageContext) => {
|
||||
const { urlParsed } = pageContext
|
||||
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
|
||||
|
||||
// Fetch global content with preview support
|
||||
const global = await getGlobal({
|
||||
isPreview,
|
||||
status,
|
||||
...(documentId && { params: { documentId } })
|
||||
})
|
||||
|
||||
return { global: global.data }
|
||||
return {
|
||||
global: global.data,
|
||||
// Pass preview state to the component
|
||||
isPreview,
|
||||
status,
|
||||
documentId,
|
||||
uid
|
||||
}
|
||||
}
|
||||
|
||||
32
pages/preview-unauthorized/+Page.vue
Normal file
32
pages/preview-unauthorized/+Page.vue
Normal file
@@ -0,0 +1,32 @@
|
||||
<template>
|
||||
<div class="unauthorized">
|
||||
<h1>Unauthorized</h1>
|
||||
<p>Invalid preview token. Please check your preview configuration.</p>
|
||||
<p><a href="/">Return to homepage</a></p>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.unauthorized {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
min-height: 50vh;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
h1 {
|
||||
color: #dc3545;
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
a {
|
||||
color: #007bff;
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
a:hover {
|
||||
text-decoration: underline;
|
||||
}
|
||||
</style>
|
||||
59
pages/preview/+Page.vue
Normal file
59
pages/preview/+Page.vue
Normal file
@@ -0,0 +1,59 @@
|
||||
<template>
|
||||
<div class="preview-redirect">
|
||||
<h1>Preview Handler</h1>
|
||||
|
||||
<div v-if="error" class="error">
|
||||
<h2>Debug Information</h2>
|
||||
<p><strong>Error:</strong> {{ error }}</p>
|
||||
<p v-if="pageContext"><strong>PageContext keys:</strong> {{ pageContext.join(', ') }}</p>
|
||||
<pre v-if="urlParsed">{{ JSON.stringify(urlParsed, null, 2) }}</pre>
|
||||
</div>
|
||||
|
||||
<div v-else-if="redirectUrl">
|
||||
<p>Redirecting to preview...</p>
|
||||
<p>If you are not redirected automatically, <a :href="redirectUrl">click here</a>.</p>
|
||||
</div>
|
||||
|
||||
<div v-else>
|
||||
<p>Processing preview request...</p>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup>
|
||||
import { useData } from 'vike-vue/useData'
|
||||
import { onMounted } from 'vue'
|
||||
|
||||
const data = useData()
|
||||
const { redirectUrl, error, pageContext, urlParsed } = data
|
||||
|
||||
onMounted(() => {
|
||||
if (redirectUrl) {
|
||||
window.location.href = redirectUrl
|
||||
}
|
||||
})
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.preview-redirect {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
min-height: 50vh;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
h1 {
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
a {
|
||||
color: #007bff;
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
a:hover {
|
||||
text-decoration: underline;
|
||||
}
|
||||
</style>
|
||||
65
pages/preview/+data.js
Normal file
65
pages/preview/+data.js
Normal file
@@ -0,0 +1,65 @@
|
||||
import { redirect } from 'vike/abort'
|
||||
|
||||
export const data = async (pageContext) => {
|
||||
try {
|
||||
// Extract URL parameters safely
|
||||
const { urlParsed } = pageContext
|
||||
const searchParams = urlParsed?.search || {}
|
||||
|
||||
console.log('Preview route - pageContext keys:', Object.keys(pageContext))
|
||||
console.log('Preview route - urlParsed:', urlParsed)
|
||||
console.log('Preview route - searchParams:', searchParams)
|
||||
|
||||
const {
|
||||
secret,
|
||||
path = '/',
|
||||
status = 'draft',
|
||||
documentId,
|
||||
uid
|
||||
} = searchParams
|
||||
|
||||
// Validate preview secret
|
||||
if (!secret || secret !== import.meta.env.VITE_PREVIEW_SECRET) {
|
||||
console.log('Invalid secret:', secret, 'Expected:', import.meta.env.VITE_PREVIEW_SECRET)
|
||||
throw redirect('/preview-unauthorized')
|
||||
}
|
||||
|
||||
// Build the preview URL path (not full URL since we're redirecting within same origin)
|
||||
const previewPath = path || '/'
|
||||
const previewUrl = new URL(previewPath, 'http://localhost')
|
||||
|
||||
// Add preview parameters
|
||||
previewUrl.searchParams.set('preview', 'true')
|
||||
previewUrl.searchParams.set('status', status)
|
||||
|
||||
// Optional: include document metadata for advanced preview functionality
|
||||
if (documentId) {
|
||||
previewUrl.searchParams.set('documentId', documentId)
|
||||
}
|
||||
if (uid) {
|
||||
previewUrl.searchParams.set('uid', uid)
|
||||
}
|
||||
|
||||
const redirectUrl = previewUrl.pathname + previewUrl.search
|
||||
console.log('Redirecting to:', redirectUrl)
|
||||
|
||||
// Use Vike's redirect
|
||||
throw redirect(redirectUrl)
|
||||
|
||||
} catch (error) {
|
||||
// If it's a redirect, let it pass through
|
||||
if (error.redirectTo) {
|
||||
throw error
|
||||
}
|
||||
|
||||
console.error('Preview data error:', error)
|
||||
|
||||
// For debugging, let's not redirect on error, but show what went wrong
|
||||
return {
|
||||
redirectUrl: null,
|
||||
error: error.message || 'Failed to process preview request',
|
||||
pageContext: Object.keys(pageContext),
|
||||
urlParsed: pageContext.urlParsed
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user