-
Notifications
You must be signed in to change notification settings - Fork 1
fix(core): add perspective to query cache key to avoid collisions #609
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
cngonzalez
merged 1 commit into
main
from
09-02-fix_core_add_perspective_to_cache_key_to_avoid_collisions
Sep 2, 2025
Merged
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import {expect, test} from '@repo/e2e' | ||
|
|
||
| test.describe('Perspectives route', () => { | ||
| test('published panel does not show draft content', async ({page, getClient, getPageContext}) => { | ||
| const client = getClient() | ||
|
|
||
| // Create a published author | ||
| const published = await client.create({ | ||
| _type: 'author', | ||
| name: 'Author Base Name', | ||
| }) | ||
|
|
||
| // Create a draft overlay for the same document id | ||
| await client.createOrReplace({ | ||
| _id: `drafts.${published._id}`, | ||
| _type: 'author', | ||
| name: 'Author Draft Name', | ||
| }) | ||
|
|
||
| // Navigate to the perspectives demo | ||
| await page.goto('./perspectives') | ||
|
|
||
| const pageContext = await getPageContext(page) | ||
|
|
||
| // Wait for both panels to render | ||
| const left = pageContext.getByRole('heading', {name: 'Drafts Resource Provider'}) | ||
| const right = pageContext.getByRole('heading', {name: 'Published Resource Provider'}) | ||
| await expect(left).toBeVisible() | ||
| await expect(right).toBeVisible() | ||
|
|
||
| // Panels render JSON with stable test ids | ||
| const draftsPanel = pageContext.getByTestId('panel-drafts-json') | ||
| const publishedPanel = pageContext.getByTestId('panel-published-json') | ||
|
|
||
| // Validate content eventually reflects correct perspectives | ||
| await expect(async () => { | ||
| const draftsText = await draftsPanel.textContent() | ||
| const publishedText = await publishedPanel.textContent() | ||
| // Drafts subtree should show the draft overlay | ||
| expect(draftsText).toContain('Author Draft Name') | ||
| // Published subtree should not show draft name | ||
| expect(publishedText).toContain('Author Base Name') | ||
| expect(publishedText).not.toContain('Author Draft Name') | ||
| }).toPass({timeout: 5000}) | ||
| }) | ||
| }) |
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,96 @@ | ||
| import {ResourceProvider, useQuery} from '@sanity/sdk-react' | ||
| import {Box, Card, Code, Flex, Heading, Stack, Text} from '@sanity/ui' | ||
| import {type JSX, Suspense} from 'react' | ||
|
|
||
| function QueryPanel({ | ||
| title, | ||
| docId, | ||
| testId, | ||
| }: { | ||
| title: string | ||
| docId: string | ||
| testId: string | ||
| }): JSX.Element { | ||
| const {data} = useQuery<Record<string, unknown> | null>({ | ||
| query: '*[_id == $id][0]', | ||
| params: {id: docId}, | ||
| }) | ||
|
|
||
| return ( | ||
| <Card padding={4} radius={3} shadow={1} tone="transparent" data-testid={`panel-${testId}`}> | ||
| <Stack space={3}> | ||
| <Heading size={2} as="h2"> | ||
| {title} | ||
| </Heading> | ||
| <Box> | ||
| <Text size={1} weight="semibold"> | ||
| Document | ||
| </Text> | ||
| <Card padding={3} radius={2} tone="transparent"> | ||
| <Code data-testid={`panel-${testId}-json`}> | ||
| {JSON.stringify(data ?? null, null, 2)} | ||
| </Code> | ||
| </Card> | ||
| </Box> | ||
| </Stack> | ||
| </Card> | ||
| ) | ||
| } | ||
|
|
||
| export function PerspectivesRoute(): JSX.Element { | ||
| // Get the latest published author document id once, outside the nested providers, | ||
| // so both subtrees use the same document id. | ||
| const {data: latest} = useQuery<{_id: string} | null>({ | ||
| query: '*[_type == "author"] | order(_updatedAt desc)[0]{_id}', | ||
| // may not always return a draft result, but usually does in test dataset | ||
| perspective: 'published', | ||
| }) | ||
|
|
||
| const docId = latest?._id | ||
|
|
||
| return ( | ||
| <Box padding={4}> | ||
| <Stack space={4}> | ||
| <Heading as="h1" size={5}> | ||
| Perspectives Demo (Key Collision) | ||
| </Heading> | ||
| <Text size={1} muted> | ||
| This nests ResourceProviders with the same project/dataset but different implicit | ||
| perspectives (drafts vs published). Both panels run the same useQuery for the same | ||
| document id without passing a perspective option. | ||
| </Text> | ||
| <Text size={1} muted> | ||
| Latest published author id: <Code>{docId ?? 'Loading…'}</Code> | ||
| </Text> | ||
|
|
||
| {/* ResourceProvider with drafts perspective */} | ||
| <ResourceProvider perspective="drafts" fallback={null}> | ||
| <Flex gap={4} wrap="wrap"> | ||
| <Box style={{minWidth: 320, flex: 1}}> | ||
| <Suspense> | ||
| {docId ? ( | ||
| <QueryPanel title="Drafts Resource Provider" docId={docId} testId="drafts" /> | ||
| ) : null} | ||
| </Suspense> | ||
| </Box> | ||
|
|
||
| {/* ResourceProvider with published perspective */} | ||
| <ResourceProvider perspective="published" fallback={null}> | ||
| <Box style={{minWidth: 320, flex: 1}}> | ||
| <Suspense> | ||
| {docId ? ( | ||
| <QueryPanel | ||
| title="Published Resource Provider" | ||
| docId={docId} | ||
| testId="published" | ||
| /> | ||
| ) : null} | ||
| </Suspense> | ||
| </Box> | ||
| </ResourceProvider> | ||
| </Flex> | ||
| </ResourceProvider> | ||
| </Stack> | ||
| </Box> | ||
| ) | ||
| } |
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not related, but the old implementation caused infinite loading due to recalculating the params passed to documentProjection. That might be something we want to resolve in the hook itself at a later date, but it was annoying me 😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm I thought kitchensink was using react 19 and compiler, maybe we removed it