65 lines
2.2 KiB
JavaScript
65 lines
2.2 KiB
JavaScript
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
|
|
}
|
|
}
|
|
} |