feat: enhance Sanity client configuration and improve News component … - #32
Conversation
📝 WalkthroughWalkthroughTwo focused changes enhance Sanity client configuration and News component rendering logic. A Changes
Estimated Code Review Effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly Related PRs
Suggested Reviewers
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/lib/sanity/client.ts`:
- Around line 10-17: The env var value assigned to perspective
(import.meta.env.VITE_SANITY_PERSPECTIVE) is unvalidated and can be incompatible
with `@sanity/client` (expected 'published' | 'drafts' | 'raw' | release-array)
and with the hardcoded useCdn: true; add validation and typing for the
perspective variable before calling createClient: coerce the env value into a
typed union (allowing 'published','drafts','raw' or an explicitly parsed
release-ID array) and default to 'published' on invalid input, and then set
useCdn dynamically when constructing sanityClient (useCdn = true only for
'published' or explicitly safe raw cases, otherwise false) so that the
createClient({ projectId, dataset, apiVersion, perspective, useCdn, ... }) call
receives correct types and compatible CDN configuration.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 6f8c562c-f2fa-4f9c-a30a-c8605a7b9e3c
📒 Files selected for processing (2)
src/lib/sanity/client.tssrc/pages/News/index.tsx
| const perspective = import.meta.env.VITE_SANITY_PERSPECTIVE || 'published' // setting default perspective to published | ||
| export const sanityClient = hasSanityConfig | ||
| ? createClient({ | ||
| projectId, | ||
| dataset, | ||
| apiVersion, | ||
| useCdn: true, | ||
| perspective, |
There was a problem hiding this comment.
🧩 Analysis chain
🌐 Web query:
@sanity/client createClient perspective option accepted values and interaction with useCdn
💡 Result:
The perspective option in @sanity/client's createClient accepts the following values: - 'published' (default for API version >= 2025-02-19): Returns only published documents, excluding drafts and versions. Works with useCdn: true. - 'drafts' (previously previewDrafts, deprecated): Returns draft versions when available, falling back to published. Requires useCdn: false (CDN does not cache drafts; setting useCdn: true causes an error). - 'raw': Returns all versions (published, drafts, releases) as separate entries for authenticated requests. Can work with useCdn: true but drafts/versions require authentication and may not be cached without a token. - Array of release IDs (e.g., ['rABC', 'drafts']): Creates a perspective stack where releases take priority over drafts over published. Requires useCdn: false. Interaction with useCdn: - useCdn: true (default): Optimized for production with caching. Compatible with 'published' and 'raw' (limited), but incompatible with 'drafts' or release stacks (errors or incomplete results). - useCdn: false: Uses live API for freshest data. Required for 'drafts', 'raw' with full variants, and release perspectives. Common for preview clients: {perspective: 'drafts', useCdn: false}. Example preview client: const previewClient = createClient({ projectId: '...', dataset: '...', perspective: 'drafts', useCdn: false, token: process.env.SANITY_API_TOKEN, apiVersion: '2026-03-01', }); Perspectives can be overridden per-query via client.fetch(query, params, {perspective: 'drafts'}). Default changed from 'raw' to 'published' in API v2025-02-19.
Citations:
- 1: https://www.sanity.io/docs/apis-and-sdks/js-client-querying
- 2: https://www.sanity.io/docs/perspectives
- 3: https://www.sanity.io/docs/content-lake/perspectives
- 4: https://www.npmjs.com/package/@sanity/client
- 5: https://github.com/sanity-io/client
- 6: https://www.sanity.io/docs/content-lake/presenting-and-previewing-content
🏁 Script executed:
cat src/lib/sanity/client.tsRepository: TrustVC/trustvc-website
Length of output: 1160
🏁 Script executed:
# Search for `@sanity/client` type definitions or usages
rg -A 5 "ClientPerspective|fetchNewsArticle" --type ts --type tsxRepository: TrustVC/trustvc-website
Length of output: 94
🏁 Script executed:
# Search for fetchNewsArticle functions and check types
rg "fetchNewsArticle|ClientPerspective" --type tsRepository: TrustVC/trustvc-website
Length of output: 4222
🏁 Script executed:
cat src/lib/sanity/news.tsRepository: TrustVC/trustvc-website
Length of output: 3990
Validate and type perspective to prevent runtime errors.
import.meta.env.VITE_SANITY_PERSPECTIVE is an untyped string passed directly to createClient(), but @sanity/client expects a specific union: 'published', 'drafts', 'raw', or arrays of release IDs. A typo in the env var (e.g., publish, preview) will either cause a runtime error or silently produce incomplete results.
More critically: useCdn: true is hardcoded but incompatible with perspective: 'drafts' or release arrays—Sanity will error or return incomplete data. If the env var is set to 'drafts', the client will fail despite readToken being passed. Even 'raw' with useCdn: true limits CDN caching of draft/version data.
Add validation to narrow the value before passing it:
♻️ Suggested refactor
-const perspective = import.meta.env.VITE_SANITY_PERSPECTIVE || 'published' // setting default perspective to published
+import type { ClientPerspective } from '@sanity/client'
+
+const ALLOWED_PERSPECTIVES = ['published', 'drafts', 'raw'] as const
+const rawPerspective = import.meta.env.VITE_SANITY_PERSPECTIVE
+const perspective: ClientPerspective = (ALLOWED_PERSPECTIVES as readonly string[]).includes(rawPerspective)
+ ? (rawPerspective as ClientPerspective)
+ : 'published'Additionally, confirm the configuration aligns with the chosen perspective: useCdn: true is only safe with 'published' or limited 'raw' use; non-published perspectives require useCdn: false.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@src/lib/sanity/client.ts` around lines 10 - 17, The env var value assigned to
perspective (import.meta.env.VITE_SANITY_PERSPECTIVE) is unvalidated and can be
incompatible with `@sanity/client` (expected 'published' | 'drafts' | 'raw' |
release-array) and with the hardcoded useCdn: true; add validation and typing
for the perspective variable before calling createClient: coerce the env value
into a typed union (allowing 'published','drafts','raw' or an explicitly parsed
release-ID array) and default to 'published' on invalid input, and then set
useCdn dynamically when constructing sanityClient (useCdn = true only for
'published' or explicitly safe raw cases, otherwise false) so that the
createClient({ projectId, dataset, apiVersion, perspective, useCdn, ... }) call
receives correct types and compatible CDN configuration.
…rendering
Summary by CodeRabbit
New Features
Bug Fixes