Skip to content

feat: enhance Sanity client configuration and improve News component … - #32

Merged
manishdex25 merged 1 commit into
mainfrom
sanity/perspective-configuration
Apr 20, 2026
Merged

feat: enhance Sanity client configuration and improve News component …#32
manishdex25 merged 1 commit into
mainfrom
sanity/perspective-configuration

Conversation

@manishdex25

@manishdex25 manishdex25 commented Apr 20, 2026

Copy link
Copy Markdown
Contributor

…rendering

Summary by CodeRabbit

  • New Features

    • Added support for Sanity content perspective configuration to control content visibility settings.
  • Bug Fixes

    • Fixed News page empty state to properly display when no posts or featured articles are available.

@manishdex25 manishdex25 self-assigned this Apr 20, 2026
@coderabbitai

coderabbitai Bot commented Apr 20, 2026

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

Two focused changes enhance Sanity client configuration and News component rendering logic. A perspective option is added to the Sanity client, sourced from environment variables with a 'published' default. The News component updates its empty-state condition to account for featured articles alongside standard articles.

Changes

Cohort / File(s) Summary
Sanity Client Configuration
src/lib/sanity/client.ts
Added perspective configuration parameter derived from VITE_SANITY_PERSPECTIVE environment variable (defaults to 'published'), now passed to createClient() call.
News Component Rendering Logic
src/pages/News/index.tsx
Introduced hasRenderablePosts constant to determine empty-state visibility based on presence of articles or featured article, replacing the previous check for articles.length === 0.

Estimated Code Review Effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly Related PRs

Suggested Reviewers

  • rongquan1

Poem

🐰✨ A rabbit hops through code so clean,
With perspectives now seen,
And posts that render bright—
Featured or plain, a joyful sight!
No empty states to fright! 🎉

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately reflects the main changes: adding perspective configuration to the Sanity client and improving the News component's rendering logic based on featured article presence.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch sanity/perspective-configuration

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@manishdex25
manishdex25 requested a review from RishabhS7 April 20, 2026 06:51

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

📥 Commits

Reviewing files that changed from the base of the PR and between a90431c and ec65744.

📒 Files selected for processing (2)
  • src/lib/sanity/client.ts
  • src/pages/News/index.tsx

Comment thread src/lib/sanity/client.ts
Comment on lines +10 to +17
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,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

🧩 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:


🏁 Script executed:

cat src/lib/sanity/client.ts

Repository: 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 tsx

Repository: TrustVC/trustvc-website

Length of output: 94


🏁 Script executed:

# Search for fetchNewsArticle functions and check types
rg "fetchNewsArticle|ClientPerspective" --type ts

Repository: TrustVC/trustvc-website

Length of output: 4222


🏁 Script executed:

cat src/lib/sanity/news.ts

Repository: 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.

@manishdex25
manishdex25 merged commit 7329472 into main Apr 20, 2026
12 checks passed
@manishdex25
manishdex25 deleted the sanity/perspective-configuration branch April 20, 2026 07:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants