Skip to content

Commit

Permalink
fix: set useCdn: false automatically when `perspective: 'previewDra…
Browse files Browse the repository at this point in the history
…fts'` (#299)
  • Loading branch information
stipsan committed Aug 18, 2023
1 parent 7d52f98 commit 0cb98cf
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/data/dataMethods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import type {
import {getSelection} from '../util/getSelection'
import * as validate from '../validators'
import * as validators from '../validators'
import {printCdnPreviewDraftsWarning} from '../warnings'
import {encodeQueryString} from './encodeQueryString'
import {ObservablePatch, Patch} from './patch'
import {ObservableTransaction, Transaction} from './transaction'
Expand Down Expand Up @@ -306,7 +307,7 @@ export function _requestObservable<R>(
? ['GET', 'HEAD'].indexOf(options.method || 'GET') >= 0 && uri.indexOf('/data/') === 0
: options.canUseCdn

const useCdn = config.useCdn && canUseCdn
let useCdn = config.useCdn && canUseCdn

const tag =
options.tag && config.requestTagPrefix
Expand All @@ -329,6 +330,11 @@ export function _requestObservable<R>(
if (typeof perspective === 'string' && perspective !== 'raw') {
validateApiPerspective(perspective)
options.query = {perspective, ...options.query}
// If the perspective is set to `previewDrafts` we can't use the CDN, the API will throw
if (perspective === 'previewDrafts' && useCdn) {
useCdn = false
printCdnPreviewDraftsWarning()
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/warnings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ export const printCdnWarning = createWarningPrinter([
`\`useCdn: false\` to use the Live API. Note: You may incur higher costs using the live API.`,
])

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

export const printBrowserTokenWarning = createWarningPrinter([
'You have configured Sanity client to use a token in the browser. This may cause unintentional security issues.',
`See ${generateHelpUrl(
Expand Down
38 changes: 38 additions & 0 deletions test/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,26 @@ describe('client', async () => {
},
)

test.skipIf(isEdge)('automatically useCdn false if perspective is previewDrafts', async () => {
nock('https://abc123.api.sanity.io')
.get(`/v1/data/query/foo?query=*&perspective=previewDrafts`)
.reply(200, {
ms: 123,
query: '*',
result: [{_id: 'njgNkngskjg', rating: 5}],
})

const client = createClient({
projectId: 'abc123',
dataset: 'foo',
useCdn: true,
perspective: 'previewDrafts',
})
const res = await client.fetch('*', {})
expect(res.length, 'length should match').toBe(1)
expect(res[0].rating, 'data should match').toBe(5)
})

test.skipIf(isEdge)(
'can query for documents with resultSourceMap and perspective using the third client.fetch parameter',
async () => {
Expand Down Expand Up @@ -592,6 +612,24 @@ describe('client', async () => {
},
)

test.skipIf(isEdge)(
'setting a perspective previewDrafts override on client.fetch sets useCdn to false',
async () => {
nock('https://abc123.api.sanity.io')
.get(`/v1/data/query/foo?query=*&perspective=previewDrafts`)
.reply(200, {
ms: 123,
query: '*',
result: [{_id: 'njgNkngskjg', rating: 5}],
})

const client = createClient({projectId: 'abc123', dataset: 'foo', useCdn: true})
const res = await client.fetch('*', {}, {perspective: 'previewDrafts'})
expect(res.length, 'length should match').toBe(1)
expect(res[0].rating, 'data should match').toBe(5)
},
)

test.skipIf(isEdge)('throws on invalid request tag on request', () => {
nock(projectHost())
.get(`/v1/data/query/foo?query=*&tag=mycompany.syncjob`)
Expand Down

0 comments on commit 0cb98cf

Please sign in to comment.