Skip to content

Commit 86ec07e

Browse files
authored
fix(react): update app redirect url to new dashboard (#377)
1 parent f7d7ca1 commit 86ec07e

File tree

2 files changed

+121
-4
lines changed

2 files changed

+121
-4
lines changed

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

Lines changed: 118 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {AuthStateType} from '@sanity/sdk'
1+
import {AuthStateType, type SanityConfig} from '@sanity/sdk'
22
import {render, screen} from '@testing-library/react'
33
import {describe, expect, it, vi} from 'vitest'
44

@@ -142,4 +142,121 @@ describe('SanityApp', () => {
142142
// Config should be passed to SDKProvider in the same order
143143
expect(config).toEqual(legacyConfigs)
144144
})
145+
146+
it('handles iframe environment correctly', async () => {
147+
// Mock window.self and window.top to simulate iframe environment
148+
const originalTop = window.top
149+
const originalSelf = window.self
150+
151+
const mockSanityConfig: SanityConfig = {
152+
projectId: 'test-project',
153+
dataset: 'test-dataset',
154+
}
155+
156+
const mockTop = {}
157+
Object.defineProperty(window, 'top', {
158+
value: mockTop,
159+
writable: true,
160+
})
161+
Object.defineProperty(window, 'self', {
162+
value: window,
163+
writable: true,
164+
})
165+
166+
render(
167+
<SanityApp config={[mockSanityConfig]} fallback={<div>Fallback</div>}>
168+
<div>Test Child</div>
169+
</SanityApp>,
170+
)
171+
172+
// Wait for 1 second
173+
await new Promise((resolve) => setTimeout(resolve, 1010))
174+
175+
// Add assertions based on your iframe-specific behavior
176+
expect(window.location.href).toBe('http://localhost:3000/')
177+
178+
// Clean up the mock
179+
Object.defineProperty(window, 'top', {
180+
value: originalTop,
181+
writable: true,
182+
})
183+
Object.defineProperty(window, 'self', {
184+
value: originalSelf,
185+
writable: true,
186+
})
187+
})
188+
189+
it('redirects to core if not inside iframe and not local url', async () => {
190+
const originalLocation = window.location
191+
192+
const mockLocation = {
193+
replace: vi.fn(),
194+
href: 'http://sanity-test.app',
195+
}
196+
197+
const mockSanityConfig: SanityConfig = {
198+
projectId: 'test-project',
199+
dataset: 'test-dataset',
200+
}
201+
202+
Object.defineProperty(window, 'location', {
203+
value: mockLocation,
204+
writable: true,
205+
})
206+
207+
render(
208+
<SanityApp config={[mockSanityConfig]} fallback={<div>Fallback</div>}>
209+
<div>Test Child</div>
210+
</SanityApp>,
211+
)
212+
213+
// Wait for 1 second
214+
await new Promise((resolve) => setTimeout(resolve, 1010))
215+
216+
// Add assertions based on your iframe-specific behavior
217+
expect(mockLocation.replace).toHaveBeenCalledWith('https://sanity.io/welcome')
218+
219+
// Clean up the mock
220+
Object.defineProperty(window, 'location', {
221+
value: originalLocation,
222+
writable: true,
223+
})
224+
})
225+
226+
it('does not redirect to core if not inside iframe and local url', async () => {
227+
const originalLocation = window.location
228+
229+
const mockSanityConfig: SanityConfig = {
230+
projectId: 'test-project',
231+
dataset: 'test-dataset',
232+
}
233+
234+
const mockLocation = {
235+
replace: vi.fn(),
236+
href: 'http://localhost:3000',
237+
}
238+
239+
Object.defineProperty(window, 'location', {
240+
value: mockLocation,
241+
writable: true,
242+
})
243+
244+
render(
245+
<SanityApp config={[mockSanityConfig]} fallback={<div>Fallback</div>}>
246+
<div>Test Child</div>
247+
</SanityApp>,
248+
)
249+
250+
// Wait for 1 second
251+
await new Promise((resolve) => setTimeout(resolve, 1010))
252+
253+
// Add assertions based on your iframe-specific behavior
254+
expect(mockLocation.replace).not.toHaveBeenCalled()
255+
256+
// Clean up the mock
257+
Object.defineProperty(window, 'location', {
258+
value: originalLocation,
259+
writable: true,
260+
})
261+
})
145262
})

packages/react/src/components/SanityApp.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export interface SanityAppProps {
1515
fallback: React.ReactNode
1616
}
1717

18-
const CORE_URL = 'https://core.sanity.io'
18+
const REDIRECT_URL = 'https://sanity.io/welcome'
1919

2020
/**
2121
* @public
@@ -86,8 +86,8 @@ export function SanityApp({
8686
// If the app is not running in an iframe and is not a local url, redirect to core.
8787
timeout = setTimeout(() => {
8888
// eslint-disable-next-line no-console
89-
console.warn('Redirecting to core', CORE_URL)
90-
window.location.replace(CORE_URL)
89+
console.warn('Redirecting to core', REDIRECT_URL)
90+
window.location.replace(REDIRECT_URL)
9191
}, 1000)
9292
}
9393
return () => clearTimeout(timeout)

0 commit comments

Comments
 (0)