Schema changes not reflecting in output despite showing in Studio

8 replies
Last updated: Jul 7, 2021
for some reason, when I change schema it is not updating the output. I have even added new fields and can see them in the studio.
AI Update

This is a common issue with TypeScript types not updating after schema changes! When you modify your schema and can see the new fields in the Studio, but they're not showing up in your TypeScript types or query results, you need to regenerate your types.

Here's what you need to do:

1. Extract your updated schema:

npx sanity@latest schema extract

This creates/updates a schema.json file with your current schema structure.

2. Generate the TypeScript types:

npx sanity@latest typegen generate

This will create or update your sanity.types.ts file (or wherever your types are configured to be generated) with types for all your schema changes, including those new fields.

Why this happens:

The TypeScript types used in your frontend/queries are generated from a static snapshot of your schema, not directly from your Studio. When you change your schema, the Studio immediately reflects those changes, but your TypeScript definitions remain outdated until you regenerate them using the commands above.

Pro tip for development:

If you're frequently updating your schema during development, you might want to run these commands regularly or set up a watch process. Some developers add a script to their package.json:

{
  "scripts": {
    "typegen": "sanity schema extract && sanity typegen generate"
  }
}

Then you can just run npm run typegen whenever you make schema changes.

If you're working in a multi-folder project (like a separate frontend), make sure your sanity-typegen.json configuration file points to the correct paths for your schema and where the types should be generated.

You can learn more about this workflow in the Sanity TypeGen documentation or take the Typed content with Sanity TypeGen course.

Show original thread
8 replies
Hey
user M
, I have tried that countless times and unfortunately it still seems to be retrieving old data which has been updated several times since. Even upon deleting the fields in the schema and adding in new fields with new names etc still nothing new happens.
Is it possible that the dataset has somehow got cached or similar?
So it turns out the GROQ query was wrong.
The
[0]
was making it return zero results so I have removed but now it is returning an array with 2 entries where the first object is
null
and the second entry the field data as follows:

[
    {
        "titleV2": null
    },
    {
        "titleV2": "Welcome to Sanity"
    }
]
Why is it returning me two entries for the 1 field being queried like so?


*[_type == "homepage"]{
  titleV2
}
Ah, so it would seem that you have two
homepage
documents, since GROQ will return an array of all of the documents that match the
*[_type == "homepage"]
filter.
If you only need to have one of that type, you could delete the one that you don’t need and convert the type to a
singleton so that you don’t end up with a duplicate messing up your queries again.
I only have one homepage schema file in the /schemas folder
there isn't any other files apart from the schema.js file in there
The schema describes the structure of a reusable document form for the Studio, not a single document. You can create many documents using a one schema. It can be confusing when first encountering it, I know. This bit of the docs goes through some key aspects of this.As I mentioned previously, you can make it so that you can’t create more than one document using a schema by converting it into a
singleton (or single edit page) , but that requires you to mess around with your Studio’s structure and add some experimental actions to your document.
ok thanks, will take a look
For sure. Let me know if you’re still having trouble.

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?