Skip to content

feat: introduce reading resources in app template#255

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

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

Conversation

@cngonzalez
Copy link
Copy Markdown
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
Copy Markdown

linear Bot commented Jan 6, 2026

@claude
Copy link
Copy Markdown

claude Bot commented Jan 6, 2026

Claude finished @ryanbonial's task in 1m 48s —— View job


Review

  • Read CLAUDE.md
  • Review changed files
  • Post findings

detectLegacySanityApp.ts:48 — Multiline import regex false negative

The regex /import\s+{[^}]*\bSanityApp\b[^}]*}\s+from\s+['"]@sanity\/sdk-react['"]/ uses [^}]* which doesn't match newlines. Users with multiline imports (common with formatters like Prettier) won't get the deprecation warning:

import {
  SanityApp,
  useSomething,
} from '@sanity/sdk-react'

Fix: add the s (dotAll) flag or use [\s\S]*? instead of [^}]*, and add a corresponding test case.


writeSanityRuntime.ts:78-83relativeConfigLocation computed but never used for app builds

When isApp is true, relativeConfigLocation is set from findAppConfigPath, but the appEntryModule template in getEntryModule.ts doesn't use relativeConfigLocation at all (only ENTRY, RESOURCES, and REACT_STRICT_MODE are substituted). This results in a needless filesystem call. Fix this →


sdkAppDependencies.ts:4-5 — RC versions pinned in production scaffold

'^3.0.0-rc.0' is a pre-release range. npm install will resolve this to the latest 3.0.0-rc.* but won't pick up the stable 3.0.0 once published (semver pre-release rules). This was flagged by @ryanbonial and should be addressed before merging — change to 'latest' or '^3' once SDK v3 is released.


createAppCliConfig.ts:10-14 — Template always scaffolds resources with potentially empty project ID/dataset

If projectId or dataset are not passed (or are undefined), processTemplate outputs empty strings — users get projectId: '' in their sanity.cli.ts. If the app init flow doesn't always collect these values, newly scaffolded apps will have an invalid config silently. Consider omitting the resources block from the template when no project/dataset is provided.

@github-actions
Copy link
Copy Markdown
Contributor

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
Copy Markdown
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
Copy Markdown

socket-security Bot commented Mar 11, 2026

// 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
Copy Markdown
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

Copy link
Copy Markdown
Member Author

@cngonzalez cngonzalez left a comment

Choose a reason for hiding this comment

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

Mostly looks good! I had a couple of comments / architectural questions I'd like to hash out.

* A named canvas resource that the app will access.
* @public
*/
export interface AppCanvasResource {
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I'm on the fence about whether we should accept these as resources at the start, when we roll out this feature. We got a request in the v3 doc KJ shared with us to infer these from the org, and there's a PR for it here: sanity-io/sdk#779

I also think that it should be opt-in behavior, which is a larger architectural issue to discuss later in this review since I'm not sure how this param would be passed down.

reactStrictMode: %REACT_STRICT_MODE%
}

renderSanityApp(
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This is a larger issue than this repo, but I'm realizing that we haven't provided a way for users to add any other configuration parameters.

Right now that's just auth (for example, setting a different API host) and possibly the new orgId inference parameter (we could pass it here, but I'd like it to be opt-in to prevent surprises) so I do feel there might be some merit to another layer /context provider that separates resource configuration and other configuration.

I'm not sure what the best path is, but I'm concerned we may be limiting ourselves down the line.

app: {
organizationId: '%organizationId%',
entry: '%entry%',
resources: {
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We now have a couple of implementations of this, and I worry that this will cause a race condition / merge conflict when we release v3. I suggest we create a stack with just the new CLI config type changes and config template as the base, and merge our PRs into it when the time comes).

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 2, 2026

📦 Bundle Stats — @sanity/cli

Compared against main (7b98e0be)

@sanity/cli

Metric Value vs main (7b98e0b)
Internal (raw) 2.1 KB -
Internal (gzip) 799 B -
Bundled (raw) 10.95 MB +8.9 KB, +0.1%
Bundled (gzip) 2.05 MB +1.6 KB, +0.1%
Import time 843ms +4ms, +0.5%

bin:sanity

Metric Value vs main (7b98e0b)
Internal (raw) 975 B -
Internal (gzip) 460 B -
Bundled (raw) 9.84 MB -
Bundled (gzip) 1.77 MB -
Import time 1.96s -3ms, -0.1%

🗺️ View treemap · Artifacts

Details
  • Import time regressions over 10% are flagged with ⚠️
  • Sizes shown as raw / gzip 🗜️. Internal bytes = own code only. Total bytes = with all dependencies. Import time = Node.js cold-start median.

📦 Bundle Stats — @sanity/cli-core

Compared against main (7b98e0be)

Metric Value vs main (7b98e0b)
Internal (raw) 92.7 KB +399 B, +0.4%
Internal (gzip) 21.7 KB +62 B, +0.3%
Bundled (raw) 21.60 MB +353 B, +0.0%
Bundled (gzip) 3.43 MB +18 B, +0.0%
Import time 785ms -2ms, -0.3%

🗺️ View treemap · Artifacts

Details
  • Import time regressions over 10% are flagged with ⚠️
  • Sizes shown as raw / gzip 🗜️. Internal bytes = own code only. Total bytes = with all dependencies. Import time = Node.js cold-start median.

📦 Bundle Stats — create-sanity

Compared against main (7b98e0be)

Metric Value vs main (7b98e0b)
Internal (raw) 976 B -
Internal (gzip) 507 B -
Bundled (raw) 50.7 KB -
Bundled (gzip) 12.6 KB -
Import time ❌ ChildProcess denied: node -
Details
  • Import time regressions over 10% are flagged with ⚠️
  • Sizes shown as raw / gzip 🗜️. Internal bytes = own code only. Total bytes = with all dependencies. Import time = Node.js cold-start median.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 2, 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 87.0% (±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 @ 7b98e0be69544a0a89ddf871219b8025128e622b

Overall Coverage

Metric Coverage
Statements 83.1% (+ 0.0%)
Branches 73.0% (+ 0.1%)
Functions 83.4% (+ 0.1%)
Lines 83.6% (+ 0.0%)

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