Skip to content

Commit 731b146

Browse files
authored
fix: do not redirect to the Dashboard if studioMode is specified (#588)
1 parent ba4cc58 commit 731b146

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

packages/react/src/components/SanityApp.test.tsx

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ describe('SanityApp', () => {
170170

171171
it('redirects to core if not inside iframe and not local url', async () => {
172172
const originalLocation = window.location
173-
const consoleLogSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
173+
const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
174174

175175
const mockLocation = {
176176
replace: vi.fn(),
@@ -204,7 +204,7 @@ describe('SanityApp', () => {
204204
value: originalLocation,
205205
writable: true,
206206
})
207-
consoleLogSpy.mockRestore()
207+
consoleWarnSpy.mockRestore()
208208
})
209209

210210
it('does not redirect to core if not inside iframe and local url', async () => {
@@ -243,4 +243,44 @@ describe('SanityApp', () => {
243243
writable: true,
244244
})
245245
})
246+
247+
it('does not redirect to core if studioMode is enabled', async () => {
248+
const originalLocation = window.location
249+
const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
250+
251+
const mockLocation = {
252+
replace: vi.fn(),
253+
href: 'http://sanity-test.app',
254+
}
255+
256+
const mockSanityConfig: SanityConfig = {
257+
projectId: 'test-project',
258+
dataset: 'test-dataset',
259+
studioMode: {enabled: true},
260+
}
261+
262+
Object.defineProperty(window, 'location', {
263+
value: mockLocation,
264+
writable: true,
265+
})
266+
267+
render(
268+
<SanityApp config={[mockSanityConfig]} fallback={<div>Fallback</div>}>
269+
<div>Test Child</div>
270+
</SanityApp>,
271+
)
272+
273+
// Wait for 1 second
274+
await new Promise((resolve) => setTimeout(resolve, 1010))
275+
276+
// Add assertions based on your iframe-specific behavior
277+
expect(mockLocation.replace).not.toHaveBeenCalled()
278+
279+
// Clean up the mock
280+
Object.defineProperty(window, 'location', {
281+
value: originalLocation,
282+
writable: true,
283+
})
284+
consoleWarnSpy.mockRestore()
285+
})
246286
})

packages/react/src/components/SanityApp.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,9 @@ export function SanityApp({
8686
}: SanityAppProps): ReactElement {
8787
useEffect(() => {
8888
let timeout: NodeJS.Timeout | undefined
89+
const primaryConfig = Array.isArray(config) ? config[0] : config
8990

90-
if (!isInIframe() && !isLocalUrl(window)) {
91+
if (!isInIframe() && !isLocalUrl(window) && !primaryConfig?.studioMode?.enabled) {
9192
// If the app is not running in an iframe and is not a local url, redirect to core.
9293
timeout = setTimeout(() => {
9394
// eslint-disable-next-line no-console
@@ -96,7 +97,7 @@ export function SanityApp({
9697
}, 1000)
9798
}
9899
return () => clearTimeout(timeout)
99-
}, [])
100+
}, [config])
100101

101102
return (
102103
<SDKProvider {...props} fallback={fallback} config={config}>

0 commit comments

Comments
 (0)