Imported documents not showing without token, but manually created ones work
For some reason none of these are showing up on our site without using a token. What's odd is that when I manually create a new document, it shows up just fine. I
have checked several imported documents to see they meet the required fields and are published - but to no avail.
I have no idea why imported documents are not showing, but newly created ones are.
These documents are hidden because their IDs contain a dot. In Sanity, any _id with a . in it (for example post.some-id or author.some-id) is treated as a path, and path IDs are always private. Only a logged-in user or a token with at least read access can see them, even in a public dataset. Documents created in the Studio get random IDs with no dot, so they're public. That's why those show up and the imported ones only appear when you send a token. Publishing status and validation don't affect this.
Confirm it
Run this query with a token and check which IDs come back:
*[_id in path("*.**") && !(_id in path("drafts.**")) && !(_id in path("versions.**"))]{_id, _type}* matches up to the next period and ** also matches across periods. Drafts (drafts.) and release versions (versions.) are meant to be private, so the query leaves them out.
Fix it
You can't edit an _id once it's set, so you have to create new documents with IDs that have no dots. The simplest way for a large dataset is to export, rewrite the file and import again:
Export the published documents. The archive contains a
data.ndjsonfile with one document per line (export docs):npx sanity@latest datasets export production production.tar.gz --no-draftsRewrite the IDs and any references to them. Extract the archive and run a script over
data.ndjson. Any_refthat points to a renamed document must change too, or the reference will break:// rewrite-ids.mjs import fs from 'node:fs' const PREFIXES = ['post.', 'author.'] // the prefixes you used const lines = fs.readFileSync('data.ndjson', 'utf8').split('\n').filter(Boolean) const docs = lines.map((l) => JSON.parse(l)) const newId = (id) => { const p = PREFIXES.find((prefix) => id.startsWith(prefix)) return p ? `${p.slice(0, -1)}-${id.slice(p.length)}` : id // post.abc -> post-abc } const fixRefs = (value) => { if (Array.isArray(value)) return value.map(fixRefs) if (value && typeof value === 'object') { const out = {} for (const [k, v] of Object.entries(value)) { out[k] = k === '_ref' && typeof v === 'string' ? newId(v) : fixRefs(v) } return out } return value } const output = docs.map((doc) => ({...fixRefs(doc), _id: newId(doc._id)})) fs.writeFileSync('data.fixed.ndjson', output.map((d) => JSON.stringify(d)).join('\n') + '\n')Check the new IDs against the ID rules: at most 128 characters from
a-zA-Z0-9._-, and they can't start with-.Import into a new dataset. The new IDs don't match the old ones, so importing into the same dataset would leave the old private documents in place next to the new ones:
npx sanity@latest datasets create production-v2 --visibility public npx sanity@latest datasets import data.fixed.ndjson -d production-v2See the datasets CLI reference for flags such as
--replaceand--missing. If you'd rather keep the current dataset, import into it and then delete the old dotted documents.Point your Studio config and front end at the new dataset. Then query without a token to confirm the documents are returned.
Note: exports and imports count against your API quota. Exporting once and importing once is much cheaper than changing documents one at a time.
Going forward
Don't put dots in your own document IDs. If you want a readable or source-system identifier, store it in a field such as customId, or use dashes (post-123). To generate compatible IDs, use uuid() from @sanity/uuid (IDs and paths).
Show original thread13 replies
Was this answer helpful?
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.