Skip to content

feat: introduce reading resources in app template#255

Draft
cngonzalez wants to merge 12 commits intomainfrom
feat/sdk-711/use-rendersanityapp
Draft

feat: introduce reading resources in app template#255
cngonzalez wants to merge 12 commits intomainfrom
feat/sdk-711/use-rendersanityapp

Conversation

@cngonzalez
Copy link
Member

@cngonzalez cngonzalez commented Jan 6, 2026

Description

This PR adds support for configuring data sources in Sanity apps. It introduces a new sources property in the CLI config that allows apps to specify project IDs and datasets they need to access. The runtime now automatically passes these sources to the app, eliminating the need for manual configuration in the App component.

What to review

  • The new sources schema in the CLI config
  • The updated app entry module that passes sources to renderSanityApp
  • The removal of manual SanityApp configuration in the basic-app example
  • The updated CLI build process that extracts sources from the config

Testing

The changes have been tested with the basic-app example, which now uses a real organization ID and project/dataset configuration. The example app has been updated to use the latest SDK packages from PR #704.

@linear
Copy link

linear bot commented Jan 6, 2026

@claude
Copy link

claude bot commented Jan 6, 2026

Claude finished @ryanbonial's task in 2m 35s —— View job


Code Review

  • Gather context
  • Read changed files
  • Provide review feedback

1. RC version pinning — blocker

packages/@sanity/cli/src/actions/init/sdkAppDependencies.ts:4-5

@sanity/sdk and @sanity/sdk-react are pinned to ^3.0.0-rc.0. The TODO comment acknowledges this. Already flagged by @ryanbonial — do not merge until SDK v3 is officially published and these are updated to latest.


2. Silent error swallowing for getCliConfig

packages/@sanity/cli/src/actions/build/writeSanityRuntime.ts:86-91

try {
  const cliConfig = await getCliConfig(cwd)
  appResources = cliConfig.app?.resources
} catch (err) {
  buildDebug('Failed to load CLI config for app resources: %s', err)
}

If getCliConfig fails (e.g., malformed sanity.cli.ts), the error is only logged at debug level. The app will silently build with an empty resources object and users will have no indication why their data sources aren't available. At minimum, use output?.warn(...) here if output is available.


3. getCliConfig called redundantly alongside findAppConfigPath

packages/@sanity/cli/src/actions/build/writeSanityRuntime.ts:81-91

Both findAppConfigPath(cwd) (to resolve the config file path for the runtime module) and getCliConfig(cwd) (to extract resources) are called independently. This means the config file is parsed twice per build/dev start. The result from getCliConfig should be reused to derive the config path rather than making two separate calls.


4. createAppCliConfig test mocks the entire processTemplate

packages/@sanity/cli/src/actions/init/__tests__/createAppCliConfig.test.ts:3-7

processTemplate is fully mocked, so neither test verifies the actual generated config output. If projectId or dataset are undefined (as in the first test case), the real processTemplate would produce a config with empty strings for those fields — valid TypeScript but incorrect runtime behavior. An integration test verifying the real template output would catch regressions here.

Fix this →


5. fixtures/basic-app/sanity.cli.ts contains a real-looking project ID

fixtures/basic-app/sanity.cli.ts:9

projectId: 'ppsg7ml5' looks like a real Sanity project ID. Even in a fixture, hardcoding real project identifiers in a public repo is worth double-checking — confirm this is intentional and the project has appropriate access controls.

@github-actions
Copy link

github-actions bot commented Jan 6, 2026

Coverage Delta

File Statements
packages/@sanity/cli-core/src/config/cli/schemas.ts 100.0% (±0%)
packages/@sanity/cli/src/actions/build/buildApp.ts 94.9% (±0%)
packages/@sanity/cli/src/actions/build/buildStaticFiles.ts 96.5% (±0%)
packages/@sanity/cli/src/actions/build/buildStudio.ts 97.5% (±0%)
packages/@sanity/cli/src/actions/build/detectLegacySanityApp.ts 95.2% (new)
packages/@sanity/cli/src/actions/build/getEntryModule.ts 100.0% (±0%)
packages/@sanity/cli/src/actions/build/writeSanityRuntime.ts 96.9% (+ 1.6%)
packages/@sanity/cli/src/actions/dev/startAppDevServer.ts 86.4% (±0%)
packages/@sanity/cli/src/actions/dev/startStudioDevServer.ts 95.0% (±0%)
packages/@sanity/cli/src/actions/init/bootstrapLocalTemplate.ts 1.9% (±0%)
packages/@sanity/cli/src/actions/init/createAppCliConfig.ts 100.0% (+ 50.0%)
packages/@sanity/cli/src/actions/init/sdkAppDependencies.ts 100.0% (±0%)
packages/@sanity/cli/src/server/devServer.ts 94.1% (±0%)

Comparing 13 changed files against main @ 2b251ff9612410efce4c13e3cc1c3f37f3a6a498

Overall Coverage

Metric Coverage
Statements 81.4% (+ 0.1%)
Branches 69.8% (+ 0.1%)
Functions 79.4% (+ 0.1%)
Lines 81.8% (+ 0.1%)

@ryanbonial ryanbonial marked this pull request as ready for review January 12, 2026 17:36
@ryanbonial ryanbonial requested a review from a team as a code owner January 12, 2026 17:36
@ryanbonial ryanbonial requested review from mttdnt and removed request for a team January 12, 2026 17:36
@binoy14
Copy link
Contributor

binoy14 commented Feb 2, 2026

@cngonzalez what's the status of this? Could this be converted to draft if not ready?

@ryanbonial ryanbonial changed the title feat: introduce reading sources in app template feat: introduce reading resources in app template Mar 9, 2026
The CLI now uses renderSanityApp from @sanity/sdk-react in the
auto-generated app.js entry module, instead of relying on the user's
App.tsx to contain a SanityApp context provider.

Named resources (project/dataset pairs) are read from sanity.cli.ts and
passed to renderSanityApp at build/dev time.

Renames the CLI config property sources → resources (and AppSource →
AppResource) to align with SDK v3 terminology. Also exports
findAppConfigPath from @sanity/cli-core for use in the build pipeline.

Made-with: Cursor
Adds static analysis to detect when a user's App.tsx still uses the
legacy <SanityApp> component pattern from @sanity/sdk-react v2. If
detected, the CLI emits a deprecation warning with migration
instructions during build and dev.

The output object is threaded through the build and dev server call
chains (buildApp, buildStudio, buildStaticFiles, devServer,
startAppDevServer, startStudioDevServer) so the warning uses the
CLI's standard output.warn() mechanism rather than console.warn.

Made-with: Cursor
App templates no longer wrap their root component with <SanityApp>.
The CLI's generated entry module now handles this via renderSanityApp,
so user code stays clean and framework-agnostic.

Made-with: Cursor
@ryanbonial ryanbonial force-pushed the feat/sdk-711/use-rendersanityapp branch 3 times, most recently from 7b4cda9 to f304343 Compare March 11, 2026 15:48
@ryanbonial ryanbonial marked this pull request as draft March 11, 2026 15:51
- Upgrade @sanity/sdk-react to ^3.0.0-rc.0
- Rename sources → resources in sanity.cli.ts
- Remove stale SanityApp import from App.tsx
- Set organizationId to match dev.test.ts expectation
- Ignore test fixture files from knip

Made-with: Cursor
@ryanbonial ryanbonial force-pushed the feat/sdk-711/use-rendersanityapp branch from f304343 to d423392 Compare March 11, 2026 17:52
@socket-security
Copy link

socket-security bot commented Mar 11, 2026

Review the following changes in direct dependencies. Learn more about Socket for GitHub.

Diff Package Supply Chain
Security
Vulnerability Quality Maintenance License
Updatednpm/​@​sanity/​sdk@​2.6.0 ⏵ 3.0.0-rc.099 +110081100 +3100
Updatednpm/​@​sanity/​sdk-react@​2.6.0 ⏵ 3.0.0-rc.089 +1100100100 +3100

View full report

// change these to 'latest' as in studioDependencies.ts once SDK v3 is released
'@sanity/sdk': '^2',
'@sanity/sdk-react': '^2',
// TODO: change these to 'latest' once SDK v3 is published
Copy link
Member

@ryanbonial ryanbonial Mar 11, 2026

Choose a reason for hiding this comment

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

Don't merge this PR until SDK v3 is officially out and we change all of the 3.0.0-rc.0 references in this PR

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.

3 participants