-
Notifications
You must be signed in to change notification settings - Fork 0
feat(cms): add live preview for pages and posts #418
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6842eb8
feat(cms): add live preview
hakalb 912ecab
fix(cms): treat enum schema migration as temporary fix and make it no-op
hakalb 2348e48
ci(workflow): ensure only nx-payload-e2e run smoke test
hakalb bc2dbf6
fix(cms): live preview should only be enabled in tenant mode
hakalb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| 'use client'; | ||
|
|
||
| import { RenderPage } from '@codeware/shared/ui/cms-renderer'; | ||
| import type { Page } from '@codeware/shared/util/payload-types'; | ||
| import type { BlocksData } from '@codeware/shared/util/payload-utils'; | ||
|
|
||
| import { LivePreview } from '../../../components/LivePreview.client'; | ||
|
|
||
| type Props = { | ||
| page: Page; | ||
| blocksData?: BlocksData; | ||
| }; | ||
|
|
||
| export function PagePreview({ page, blocksData }: Props) { | ||
| return ( | ||
| <LivePreview initialData={page}> | ||
| {(data) => <RenderPage page={data} blocksData={blocksData} />} | ||
| </LivePreview> | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| 'use client'; | ||
|
|
||
| import { RenderLandingPage } from '@codeware/shared/ui/cms-renderer'; | ||
| import type { Page } from '@codeware/shared/util/payload-types'; | ||
|
|
||
| import { LivePreview } from '../../components/LivePreview.client'; | ||
|
|
||
| type Props = { | ||
| landingPage: Page; | ||
| }; | ||
|
|
||
| export function LandingPagePreview({ landingPage }: Props) { | ||
| return ( | ||
| <LivePreview initialData={landingPage}> | ||
| {(data) => <RenderLandingPage landingPage={data} />} | ||
| </LivePreview> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
apps/cms/src/app/(site)/posts/[...slug]/post-preview.client.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| 'use client'; | ||
|
|
||
| import { RenderPost } from '@codeware/shared/ui/cms-renderer'; | ||
| import type { Post } from '@codeware/shared/util/payload-types'; | ||
|
|
||
| import { LivePreview } from '../../../../components/LivePreview.client'; | ||
|
|
||
| type Props = { | ||
| post: Post; | ||
| }; | ||
|
|
||
| export function PostPreview({ post }: Props) { | ||
| return ( | ||
| <LivePreview initialData={post} depth={1}> | ||
| {(data) => <RenderPost post={data} />} | ||
| </LivePreview> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| 'use client'; | ||
|
|
||
| import { useLivePreview } from '@payloadcms/live-preview-react'; | ||
|
|
||
| import { usePayload } from '@codeware/shared/ui/cms-renderer'; | ||
|
|
||
| interface LivePreviewProps<T extends Record<string, any>> { | ||
| initialData: T; | ||
| depth?: number; | ||
| children: (data: T) => React.ReactNode; | ||
|
hakalb marked this conversation as resolved.
|
||
| } | ||
|
|
||
| /** | ||
| * Generic client component for Payload live preview. | ||
| * | ||
| * Subscribes to live preview updates from the CMS admin panel and passes | ||
| * the current data (updated as-you-type) to the render function. | ||
| * Falls back to `initialData` when not in preview mode. | ||
| * | ||
| * @example | ||
| * ```tsx | ||
| * <LivePreview initialData={page}> | ||
| * {(data) => <RenderPage page={data} blocksData={blocksData} />} | ||
| * </LivePreview> | ||
| * ``` | ||
| */ | ||
| export function LivePreview<T extends Record<string, any>>({ | ||
| initialData, | ||
| depth = 2, | ||
| children | ||
| }: LivePreviewProps<T>) { | ||
| const { payloadUrl } = usePayload(); | ||
| const { data } = useLivePreview<T>({ | ||
| initialData, | ||
| serverURL: payloadUrl, | ||
| depth | ||
| }); | ||
|
|
||
| return <>{children(data)}</>; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| 'use client'; | ||
|
|
||
| import { RefreshRouteOnSave as PayloadLivePreview } from '@payloadcms/live-preview-react'; | ||
| import { useRouter } from 'next/navigation'; | ||
| import React from 'react'; | ||
|
|
||
| import { usePayload } from '@codeware/shared/ui/cms-renderer'; | ||
|
|
||
| /** | ||
| * Client component for server-side live preview. | ||
| * Listens for save events from Payload CMS and triggers a route refresh. | ||
| * | ||
| * Add this component to your layout or page to enable live preview updates when content is saved in the CMS. | ||
| */ | ||
| export const RefreshRouteOnSave: React.FC = () => { | ||
| const { payloadUrl } = usePayload(); | ||
| const router = useRouter(); | ||
|
|
||
| return ( | ||
| <PayloadLivePreview | ||
| refresh={() => router.refresh()} | ||
| serverURL={payloadUrl} | ||
| /> | ||
| ); | ||
| }; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.