Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/next/src/views/Document/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,9 @@ export const renderDocument = async ({
<LivePreviewProvider
breakpoints={livePreviewConfig?.breakpoints}
isLivePreviewEnabled={isLivePreviewEnabled && operation !== 'create'}
isLivePreviewing={entityPreferences?.value?.editViewType === 'live-preview'}
isLivePreviewing={Boolean(
entityPreferences?.value?.editViewType === 'live-preview' && livePreviewURL,
)}
typeofLivePreviewURL={typeof livePreviewConfig?.url as 'function' | 'string' | undefined}
url={livePreviewURL}
>
Expand Down
4 changes: 4 additions & 0 deletions packages/ui/src/providers/LivePreview/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ export const LivePreviewProvider: React.FC<LivePreviewProviderProps> = ({
incomingURL = formatAbsoluteURL(_incomingURL)
}

if (!incomingURL) {
setIsLivePreviewing(false)
}

if (incomingURL !== url) {
setAppIsReady(false)
setURL(incomingURL)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { CollectionConfig } from 'payload'

export const NoURLCollection: CollectionConfig = {
slug: 'no-url',
export const ConditionalURL: CollectionConfig = {
slug: 'conditional-url',
admin: {
livePreview: {
url: ({ data }) => (data?.enabled ? '/live-preview/static' : null),
Expand Down
2 changes: 1 addition & 1 deletion test/live-preview/collections/StaticURL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export const StaticURLCollection: CollectionConfig = {
slug: 'static-url',
admin: {
livePreview: {
url: '/live-preview/hello-world',
url: '/live-preview/static',
},
},
fields: [
Expand Down
4 changes: 2 additions & 2 deletions test/live-preview/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import { buildConfigWithDefaults } from '../buildConfigWithDefaults.js'
import { MediaBlock } from './blocks/MediaBlock/index.js'
import { Categories } from './collections/Categories.js'
import { CollectionLevelConfig } from './collections/CollectionLevelConfig.js'
import { ConditionalURL } from './collections/ConditionalURL.js'
import { CustomLivePreview } from './collections/CustomLivePreview.js'
import { Media } from './collections/Media.js'
import { NoURLCollection } from './collections/NoURL.js'
import { Pages } from './collections/Pages.js'
import { Posts } from './collections/Posts.js'
import { SSR } from './collections/SSR.js'
Expand Down Expand Up @@ -68,7 +68,7 @@ export default buildConfigWithDefaults({
CollectionLevelConfig,
StaticURLCollection,
CustomLivePreview,
NoURLCollection,
ConditionalURL,
],
globals: [Header, Footer],
onInit: seed,
Expand Down
37 changes: 31 additions & 6 deletions test/live-preview/e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,22 +179,47 @@ describe('Live Preview', () => {
await expect.poll(async () => iframe.getAttribute('src')).toMatch(/\/live-preview/)
})

test('collection — does not render iframe when live preview url is falsy', async () => {
const noURL = new AdminUrlUtil(serverURL, 'no-url')
test('collection — does not render live preview when url is null', async () => {
const noURL = new AdminUrlUtil(serverURL, 'conditional-url')
await page.goto(noURL.create)
await page.locator('#field-title').fill('No URL')
await saveDocAndAssert(page)

// No toggler should render
const toggler = page.locator('button#live-preview-toggler')
await expect(toggler).toBeHidden()
await expect(page.locator('iframe.live-preview-iframe')).toBeHidden()

// Check the `enabled` field
const enabledCheckbox = page.locator('#field-enabled')
await enabledCheckbox.check()
await saveDocAndAssert(page)

// Toggler is present but not iframe
await expect(toggler).toBeVisible()
await toggleLivePreview(page)
await expect(page.locator('iframe.live-preview-iframe')).toBeVisible()
await expect(page.locator('iframe.live-preview-iframe')).toBeHidden()

// Toggle the iframe back on, which will save to prefs
// We need to explicitly test for this, as we don't want live preview to suddenly appear
await toggleLivePreview(page, {
targetState: 'on',
})

// Uncheck the `enabled` field
await enabledCheckbox.uncheck()
await saveDocAndAssert(page)

// Toggler and iframe are gone
await expect(toggler).toBeHidden()
await expect(page.locator('iframe.live-preview-iframe')).toBeHidden()

// Check the `enabled` field
await enabledCheckbox.check()
await saveDocAndAssert(page)

// Toggler is present but still not iframe
await expect(toggler).toBeVisible()
await expect(page.locator('iframe.live-preview-iframe')).toBeHidden()
})

test('collection — retains static URL across edits', async () => {
Expand All @@ -204,12 +229,12 @@ describe('Live Preview', () => {
await toggleLivePreview(page, { targetState: 'on' })

const iframe = page.locator('iframe.live-preview-iframe')
await expect.poll(async () => iframe.getAttribute('src')).toMatch(/\/live-preview\/hello/)
await expect.poll(async () => iframe.getAttribute('src')).toMatch(/\/live-preview\/static/)

const titleField = page.locator('#field-title')
await titleField.fill('New Title')
await saveDocAndAssert(page)
await expect.poll(async () => iframe.getAttribute('src')).toMatch(/\/live-preview\/hello/)
await expect.poll(async () => iframe.getAttribute('src')).toMatch(/\/live-preview\/static/)
})

test('collection csr — iframe reflects form state on change', async () => {
Expand Down
Loading