Skip to content

Commit 3b03ac5

Browse files
authored
fix: deprecate previewDrafts-perspective in favor of drafts (#1007)
1 parent fbb2a70 commit 3b03ac5

File tree

4 files changed

+108
-10
lines changed

4 files changed

+108
-10
lines changed

README.md

+85-5
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ export async function updateDocumentTitle(_id, title) {
6969
- [Performing queries](#performing-queries)
7070
- [Using perspectives](#using-perspectives)
7171
- [`published`](#published)
72-
- [`previewDrafts`](#previewdrafts)
72+
- [`drafts`](#drafts)
73+
- [`raw`](#raw)
7374
- [Fetching Content Source Maps](#fetching-content-source-maps)
7475
- [Using Visual editing with steganography](#using-visual-editing-with-steganography)
7576
- [Creating Studio edit intent links](#creating-studio-edit-intent-links)
@@ -554,7 +555,9 @@ Then `authors` will only contain documents that don't have a `drafts.` prefix in
554555
]
555556
```
556557

557-
#### `previewDrafts`
558+
#### `drafts`
559+
560+
> Note: this was previously named `previewDrafts`. The `previewDrafts` perspective is still supported, but deprecated and may be removed in a future API version, so we recommend changing to `drafts`.
558561
559562
Designed to help answer the question "What is our app going to look like after all the draft documents are published?".
560563

@@ -592,8 +595,8 @@ import {createClient} from '@sanity/client'
592595

593596
const client = createClient({
594597
...config,
595-
useCdn: false, // the `previewDrafts` perspective requires this to be `false`
596-
perspective: 'previewDrafts',
598+
useCdn: false, // the `drafts` perspective requires this to be `false`
599+
perspective: 'drafts',
597600
})
598601

599602
const authors = await client.fetch('*[_type == "author"]')
@@ -624,7 +627,7 @@ Then `authors` will look like this. Note that the result dedupes documents with
624627
]
625628
```
626629

627-
Since the query simulates what the result will be after publishing the drafts, the `_id` doesn't contain the `drafts.` prefix. If you want to check if a document is a draft or not you can use the `_originalId` field, which is only available when using the `previewDrafts` perspective.
630+
Since the query simulates what the result will be after publishing the drafts, the `_id` doesn't contain the `drafts.` prefix. If you want to check if a document is a draft or not you can use the `_originalId` field, which is only available when using the `drafts` perspective.
628631

629632
```ts
630633
const authors = await client.fetch(`*[_type == "author"]{..., "status": select(
@@ -654,6 +657,83 @@ Which changes the result to be:
654657
]
655658
```
656659

660+
#### `raw`
661+
662+
Designed for working with all variations of documents, including, but not limited to, drafts and versions.
663+
664+
Given a dataset like this:
665+
666+
```json
667+
[
668+
{
669+
"_type": "author",
670+
"_id": "ecfef291-60f0-4609-bbfc-263d11a48c43",
671+
"name": "George Martin"
672+
},
673+
{
674+
"_type": "author",
675+
"_id": "drafts.ecfef291-60f0-4609-bbfc-263d11a48c43",
676+
"name": "George R.R. Martin"
677+
},
678+
{
679+
"_type": "author",
680+
"_id": "versions.r273M8K2Q.ecfef291-60f0-4609-bbfc-263d11a48c43",
681+
"name": "George R.R. Martin"
682+
},
683+
{
684+
"_type": "author",
685+
"_id": "drafts.f4898efe-92c4-4dc0-9c8c-f7480aef17e2",
686+
"name": "Stephen King"
687+
}
688+
]
689+
```
690+
691+
And a query like this:
692+
693+
```ts
694+
import {createClient} from '@sanity/client'
695+
696+
const client = createClient({
697+
...config,
698+
useCdn: false, // the `raw` perspective can be used with CDN, but versions and drafts will not be returned unless a token is provided
699+
perspective: 'raw',
700+
})
701+
702+
const authors = await client.fetch('*[_type == "author"]')
703+
```
704+
705+
Then `authors` will include the complete set:
706+
707+
```json
708+
[
709+
{
710+
"_type": "author",
711+
"_id": "ecfef291-60f0-4609-bbfc-263d11a48c43",
712+
"name": "George Martin"
713+
},
714+
{
715+
"_type": "author",
716+
"_id": "drafts.ecfef291-60f0-4609-bbfc-263d11a48c43",
717+
"name": "George R.R. Martin"
718+
},
719+
{
720+
"_type": "author",
721+
"_id": "versions.r273M8K2Q.ecfef291-60f0-4609-bbfc-263d11a48c43",
722+
"name": "George R.R. Martin"
723+
},
724+
{
725+
"_type": "author",
726+
"_id": "drafts.f4898efe-92c4-4dc0-9c8c-f7480aef17e2",
727+
"name": "Stephen King"
728+
}
729+
]
730+
```
731+
732+
Currently, Sanity supports two types of document variations: drafts and versions. Drafts and versions will not be visible to queries as long as the client is unauthenticated.
733+
However, using the `raw` perspective with an authenticated client will cause both drafts and versions to appear in query results, so if using `raw` with an authenticated client, make sure this is the behavior you want.
734+
735+
Also keep in mind that additional document variations may be introduced by Sanity in the future.
736+
657737
### Fetching Content Source Maps
658738

659739
Content Source Maps annotate fragments in your query results with metadata about its origin: the field, document, and dataset it originated from.

src/data/dataMethods.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import type {
3333
import {getSelection} from '../util/getSelection'
3434
import * as validate from '../validators'
3535
import * as validators from '../validators'
36-
import {printCdnPreviewDraftsWarning} from '../warnings'
36+
import {printCdnPreviewDraftsWarning, printPreviewDraftsDeprecationWarning} from '../warnings'
3737
import {encodeQueryString} from './encodeQueryString'
3838
import {ObservablePatch, Patch} from './patch'
3939
import {ObservableTransaction, Transaction} from './transaction'
@@ -407,15 +407,24 @@ export function _requestObservable<R>(
407407
}
408408
const perspectiveOption = options.perspective || config.perspective
409409
if (typeof perspectiveOption !== 'undefined') {
410+
if (perspectiveOption === 'previewDrafts') {
411+
printPreviewDraftsDeprecationWarning()
412+
}
410413
validateApiPerspective(perspectiveOption)
411414
options.query = {
412415
perspective: Array.isArray(perspectiveOption)
413416
? perspectiveOption.join(',')
414417
: perspectiveOption,
415418
...options.query,
416419
}
417-
// If the perspective is set to `previewDrafts` we can't use the CDN, the API will throw
418-
if (perspectiveOption === 'previewDrafts' && useCdn) {
420+
// If the perspective is set to `drafts` or multiple perspectives we can't use the CDN, the API will throw
421+
if (
422+
((Array.isArray(perspectiveOption) && perspectiveOption.length > 0) ||
423+
// previewDrafts was renamed to drafts, but keep for backwards compat
424+
perspectiveOption === 'previewDrafts' ||
425+
perspectiveOption === 'drafts') &&
426+
useCdn
427+
) {
419428
useCdn = false
420429
printCdnPreviewDraftsWarning()
421430
}

src/types.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,17 @@ export interface RequestOptions {
3737
*/
3838
export type ReleaseId = `r${string}`
3939

40+
/**
41+
* @deprecated use 'drafts' instead
42+
*/
43+
type DeprecatedPreviewDrafts = 'previewDrafts'
44+
4045
/** @public */
4146
export type StackablePerspective = ('published' | 'drafts' | string) & {}
4247

4348
/** @public */
4449
export type ClientPerspective =
45-
| 'previewDrafts'
50+
| DeprecatedPreviewDrafts
4651
| 'published'
4752
| 'drafts'
4853
| 'raw'

src/warnings.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,14 @@ export const printCdnWarning = createWarningPrinter([
1818
])
1919

2020
export const printCdnPreviewDraftsWarning = createWarningPrinter([
21-
`The Sanity client is configured with the \`perspective\` set to \`previewDrafts\`, which doesn't support the API-CDN.`,
21+
`The Sanity client is configured with the \`perspective\` set to \`drafts\` or \`previewDrafts\`, which doesn't support the API-CDN.`,
2222
`The Live API will be used instead. Set \`useCdn: false\` in your configuration to hide this warning.`,
2323
])
2424

25+
export const printPreviewDraftsDeprecationWarning = createWarningPrinter([
26+
`The \`previewDrafts\` perspective has been renamed to \`drafts\` and will be removed in a future API version`,
27+
])
28+
2529
export const printBrowserTokenWarning = createWarningPrinter([
2630
'You have configured Sanity client to use a token in the browser. This may cause unintentional security issues.',
2731
`See ${generateHelpUrl(

0 commit comments

Comments
 (0)