Including Redirects in a Sanity Schema

14 replies
Last updated: Aug 8, 2022
Hi all! We’re trying to figure out a way to include redirects as part of our schema. My initial thought is to reference a redirect document type, one of the fields being a reference to the current document. Is there a way to set the initial value from the current document?
Aug 8, 2022, 6:48 PM
Hey
user Q
! To clarify, are you trying to set a redirect reference initial value on the document?
Aug 8, 2022, 6:50 PM
Yes.
Aug 8, 2022, 6:51 PM
Got it. Then, if you're setting it on the document level:
export default {
  name: 'yourDocument',
  title: 'Your Document',
  type: 'document',
  initialValue: {
    redirect: {
      _type: 'reference',
      _ref: <_id-of-redirect-document>
    }
  }
  //...
}
Aug 8, 2022, 6:55 PM
Excellent! Thank you. Is there anyway to get the current document _ref ?
Aug 8, 2022, 6:56 PM
The current document _ref would be the _id of the current document. Or am I misunderstanding your question?
Aug 8, 2022, 6:59 PM
For example I can’t get access to the current document like this:
Aug 8, 2022, 6:59 PM
initialValue: ({ document }) =&gt; { console.log(document);
},
Aug 8, 2022, 6:59 PM
We want to view the redirects for the article on the article document, so we added an array field to reference the redirect document type.
Aug 8, 2022, 7:02 PM
Ah, I see. You won't be able to get the document Id in an initial value, because a document does not have an id when it's first created. An Id will be assigned after an edit is made.
Aug 8, 2022, 7:03 PM
Ahh I see. How would one do it after it is created? Could you do it if you were to edit the already created doc?
Aug 8, 2022, 7:03 PM
I think you'd need to create a custom input component. I can put an example together in about 30 mins for you!
Aug 8, 2022, 7:07 PM
wow thanks!
Aug 8, 2022, 7:07 PM
This is just an example and doesn't have any functionality for editing the incoming redirects, but it's an example of how you'd begin putting together the component:
import React, { useState, useEffect } from 'react';
import { withDocument } from 'part:@sanity/form-builder';
import { FormField } from '@sanity/base/components';
import client from 'part:@sanity/base/client';

const studioClient = client.withConfig({ apiVersion: '2021-10-21' });

const Redirects = React.forwardRef((props, ref) => {
  const [redirects, setRedirects] = useState([]);

  const {
    type, // Schema information
    value, // Current field value
    readOnly, // Boolean if field is not editable
    markers, // Markers including validation rules
    presence, // Presence information for collaborative avatars
    compareValue, // Value to check for "edited" functionality
    onFocus, // Method to handle focus state
    onBlur, // Method to handle blur state
    onChange, // Method to handle patch events,
    parent,
    document,
  } = props;

  // Creates a change handler for patching data

  const inputId = useId();

  useEffect(() => {
    const getRedirects = async () => {
      if (!document._id) return;
      await studioClient
        .fetch(`*[_type =='redirect' && references($id)]`, { id: document._id })
        .then(setRedirects);
    };

    getRedirects();
  }, [document._id]);

  return (
    <FormField
      description={type.description} // Creates description from schema
      title={type.title} // Creates label from schema title
      __unstable_markers={markers} // Handles all markers including validation
      __unstable_presence={presence} // Handles presence avatars
      compareValue={compareValue} // Handles "edited" status
      inputId={inputId} // Allows the label to connect to the input field
    >
      {redirects && redirects.map(redirect => <p>{redirect.title}</p>)}
    </FormField>
  );
});

export default withDocument(Redirects);
Aug 8, 2022, 7:34 PM
Perfect. Thanks so much!
Aug 8, 2022, 9:29 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?