How to create working document edit links in custom Sanity Studio v2 desk structure views?

12 replies
Last updated: Aug 27, 2022
If I have a custom document view in my desk structure, what's the best way to generate a working link from the depicted markup to the actual in-Studio edit screen in v2?
I have the ID and just leave it a relative address, and it looks
just like the real address if I were to get there by browsing panes. But if I click it, it either gets there after a huge refresh of the whole Studio page's tab, or it becomes unresponsive.
I did search and read through things first before asking but perhaps I've overlooked something. I saw
user H
do something like that in his pretend cake shop at the conference but I'm not sure how that works.

Note: EDITED because I accidentally called it JSON view because the documentation example was saturated into my brain
AI Update

For creating working links to document edit screens in Sanity Studio v2 from custom document views, you'll want to use the IntentLink component rather than plain relative links. This is the proper way to navigate within the Studio without causing full page refreshes or unresponsive behavior.

The IntentLink component from part:@sanity/base/router handles Studio navigation properly by working with the internal state management system. Here's how to use it:

import { IntentLink } from 'part:@sanity/base/router'

// In your component
<IntentLink
  intent="edit"
  params={{ id: documentId, type: documentType }}
>
  Click to edit
</IntentLink>

This generates URLs that look like:

http://localhost:3333/intent/edit/id=4497efe3-8ab6-4e55-872e-b1ef9577cc07;type=paletteColor/

When you use regular <a> tags with relative paths like /desk/paletteColor-unsorted;d27d5d4e-5faf-4a4c-b25f-0213e2ea492c, the browser treats it as a full page navigation. This causes:

  • Complete Studio page reloads (that "huge refresh" you're experiencing)
  • Loss of internal state tracking
  • Potential unresponsiveness as the Studio tries to reinitialize

The Studio maintains complex state for the desk structure, panes, and document editing sessions. IntentLink preserves all of this by using the internal router.

Complete Example

Here's a more complete example showing IntentLink in context:

import React from 'react'
import { IntentLink } from 'part:@sanity/base/router'
import Preview from 'part:@sanity/base/preview'
import schema from 'part:@sanity/base/schema'

const MyCustomView = ({ documents }) => {
  return (
    <div>
      {documents.map((doc) => {
        const schemaType = schema.get(doc._type)
        return (
          <IntentLink
            key={doc._id}
            intent="edit"
            params={{ id: doc._id, type: doc._type }}
          >
            <Preview value={doc} type={schemaType} />
          </IntentLink>
        )
      })}
    </div>
  )
}

This pattern is used throughout Sanity's own components (like in the backlinks recipe) and provides smooth, stateful navigation within the Studio.

Note: If you've migrated to Studio v3, the approach has changed - you'd use the router from sanity/router instead. But for v2, IntentLink is the way to go!

Show original thread
12 replies
By JSON view, do you mean Inspect?
Or do you mean in t custom document view?
Appending
,inspect=on
to your URL will show the Inspect view.
Oh sorry, I can show you, just a sec
I believe I mean just document view
I am trying to make these color swatches clickable to their respective edit screens
The href winds up being something like

http://localhost:3333/desk/paletteColor-unsorted;d27d5d4e-5faf-4a4c-b25f-0213e2ea492c
Which is identical to going there the natural way, but the effect is way different.
Got it. I think you want to use either an Intent Link or the router . I can't find the intent link docs though...
Oooh, schmancy. I saw something called usePaneRouter too. I'll take a look at your thing and report back. Thanks for the quick response!
user M
I crashed and burned trying the ways over at the NPM link, but it turns out that today you were a double-savior because Past You came in for the alley-oop assist: https://sanity-io-land.slack.com/archives/C9Z7RC3V1/p1659975590648499?thread_ts=1659949651.348759&amp;cid=C9Z7RC3V1
It works
perfectly and so smoothly. I wasn't really thinking that it probably can't just be a straight link because of all the state being tracked, but if anyone else is curious the format that the component arrives at looks more like this:

http://localhost:3333/intent/edit/id=4497efe3-8ab6-4e55-872e-b1ef9577cc07;type=paletteColor/
Yay! Sorry I initially lead you astray with the router 😅
That just made it more dramatically compelling when you eventually saved me! Makes for a better story 😃
It's amazing to me that I've been around for over half a year and I keep finding new stuff in Sanity. I realize you all have been around for years, but you look at the Studio and it's practically a blank screen, yet
goodness there's so much possibility, so much that it rises to meet when you come up with a challenge for it.
In other platforms you're stuck wondering "How could I possibly make this work?
Can I make this work?" But with Sanity it's more like "99% sure this is a covered base, I just gotta see what the specific way is."
It really elevates one's confidence that the work can be only about the work, and that you can just burn through all the concepts in your brain to make cool things.

It's the near-absence of bottlenecks. It's just a weird, rounded glass cylinder bottle. I guess maybe it's a magic straw, or there's a small circular hole cut in the top that doesn't count as a neck because it's just an opening...

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?