See Sanity in action 👀 Join us for a live product demo + Q&A →

Decoupling content from layout in headless CMS, challenges with interactive articles & live preview solutions

45 replies
Last updated: Feb 14, 2023
It seems that if I create schema with array of reference to multiple types and press "create new" button, sanity won't give you a choice which type to create.Has anyone come up with a solution to this?
Feb 14, 2023, 4:39 PM
Can you share the schema entry?
Feb 14, 2023, 4:40 PM
 {
            title: 'Sections',
            name: 'sections',
            type: 'array',
            sortable: true,
            of: [
                {
                    type: 'reference',
                    to: [
                        { type: 'contentblock' },
                        { type: 'twocolumns' }
                    ]
                }]
        }
Feb 14, 2023, 4:41 PM
Are both contentBlock and twocolumns 'document' types?
Feb 14, 2023, 4:42 PM
Here's a working example from my schema...
Feb 14, 2023, 4:42 PM
defineField({
      name: "sectionBlocks",
      type: "array",
      title: "Content Blocks",
      description: "Add, and edit section blocks",
      of: [
        {
          type: "reference",
          to: [
            { title: "Section Block", type: "sectionBlock" },
            { title: "Form", type: "form" },
            { title: "Tag Index", type: "tagIndex" }
          ],
        },
      ],
      hidden: ({ document }: any) => {
        if (
          document.sectionType != "Player" &&
          document.sectionType != "Article"
        ) {
          return false;
        } else {
          return true;
        }
      },
      group: 'blocks',
    }),
Feb 14, 2023, 4:42 PM
two column sis an object
Feb 14, 2023, 4:42 PM
Thats your problem I think. I don't think you can mix references like that to both objects and documents.
Feb 14, 2023, 4:43 PM
ok, that was it. Changing to document solved that. Reason to use object was to hide it from the list of items
Feb 14, 2023, 4:43 PM
BTW... is that a Finnish name?
Feb 14, 2023, 4:44 PM
Which?
Feb 14, 2023, 4:44 PM
My name?
Feb 14, 2023, 4:44 PM
yeah... I spent a lot of time in Finland.
Feb 14, 2023, 4:44 PM
Yes
Feb 14, 2023, 4:44 PM
Huva... I can't say much more 🙂
Feb 14, 2023, 4:45 PM
almost 🙂
Feb 14, 2023, 4:45 PM
Hyvä
Feb 14, 2023, 4:45 PM
right... I'm sure I spelled it wrong. LOL
Feb 14, 2023, 4:45 PM
I'm also mixed how to use sanity models vs. components and where to draw the line on how much to expose to CMS on layout specifics
Feb 14, 2023, 4:47 PM
I have re-written the app already 3 times since I have not been happy on how it handles decoupling of content vs. layout
Feb 14, 2023, 4:48 PM
reading and thinking...
Feb 14, 2023, 4:49 PM
e.g. having document in sanity that's named two columns is already wrong...
Feb 14, 2023, 4:49 PM
The starting principle for me is that the value of using a headless CMS is to de-couple the content from the layout. The benefit is that you can leverage the content in multilple ways. For example, write an article and then not only publish it, but also automate other processes such as workflows and social marketing, etc.
Feb 14, 2023, 4:51 PM
That manifests itself like this in my case...
Feb 14, 2023, 4:52 PM
I have editors writing articles. I have one string field that is a title, one text area that is a summary and a cover image. Then I have block content for the main body of the article.
Feb 14, 2023, 4:53 PM
Whenever an article is published automation uses the more deterministic fields for social media posts. It is also used in the article as the H1, summary and cover image.
Feb 14, 2023, 4:53 PM
In the block content, I use serializers to and custom marks and serializers to create a data structure that allows me to create many different layouts and templates.
Feb 14, 2023, 4:54 PM
Yes, agree on those completely. My problems arised when we started writing interactive article with editor, graphic artist and coder together and store that in to CMS.
Feb 14, 2023, 4:55 PM
To me it seems that without the preview, writers don't want to write. But you cannot have preview before it's written 🙂
Feb 14, 2023, 4:56 PM
It's hard to rely on the imagination when writing the article 🙂
Feb 14, 2023, 4:56 PM
The solution I'm working on is to create a live preview in the CMS.
Feb 14, 2023, 4:57 PM
From what I can tell, this is going to require spinning up a react app that will render the page and then iFraming it in.
Feb 14, 2023, 4:58 PM
yes, I'm working on same with sveltekit. Currently it works only with iframe and reloading whole page.
Feb 14, 2023, 4:58 PM
My ultimate solution will actually be to build Sanity V3 components directly into my site. I've already converted my entire data model into an installable node module for tis purpose.
Feb 14, 2023, 4:59 PM
That way my editors can just click the article and edit it right there.
Feb 14, 2023, 5:00 PM
That should be doable, but for me I prefer sveltekit over react and wan't to stay as far away from react as possible 😉
Feb 14, 2023, 5:01 PM
I'm torturing myself with React and Gatsby LOL
Feb 14, 2023, 5:01 PM
yes, react is like "unix airlines"
Feb 14, 2023, 5:02 PM
HAHAHAHA
Feb 14, 2023, 5:02 PM
Is Svelte Linux airlines or windows airlines?
Feb 14, 2023, 5:02 PM
I would call it just a good tool
Feb 14, 2023, 5:03 PM
based on modern standards
Feb 14, 2023, 5:03 PM
with react airlines, every passenger brings one piece of plane with them and then they start to try to assemble them into a airplane...
with sveltekit you just read html,css, {java,type}script documentation and write you app
Feb 14, 2023, 5:04 PM
but maybe it's windows airlanes that hasn't exploded yet 🙂
Feb 14, 2023, 5:05 PM
They announced 1.0, so it's only matter of time....
Feb 14, 2023, 5:05 PM
I'm using react and gatsby for economic reasons. Generating the site means I don't blow up my API usage. With hundreds of sites, those costs can stack up. That said, my sites are mainly brochure ware for small businesses.
Feb 14, 2023, 5:08 PM

Sanity.io – build remarkable experiences at scale

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

Categorized in