Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Tito 314 posts 623 karma points
    May 06, 2021 @ 14:51
    Tito
    0

    Making Umbraco CMS v8 custom headless: Preview mode?

    I need a custom Umbraco 8 Headless for a project which i am using to query from a nextjs web site. I have successfully achieved it using route hijacking to return json using: https://our.umbraco.com/documentation/reference/routing/custom-controllers

    The nextjs web can query and navigate the umbraco content, using the umbraco url system, and works great!

    Now i would like to get preview mode for the nextjs web.

    Right now i have a template that redirects to the exact url on nextjs web so when you click on the url from the Info tab on Umbraco it gets to the web site.

    My idea is when preview, redirect to the nextjs web with a flag param that makes the nextjs query content to umbraco and get non published content if the controller gets some kind of flag.

    I know i can return non published using the content service, but i want to use the Umbraco helper.

    I would like to know how Umbraco does it internally when you click preview button on the backoffice, because it uses the same code of the templates to render non published content.

    How it works? how could i trick Umbraco from a controller to do it?

  • John C Scott 473 posts 1183 karma points
    Apr 06, 2022 @ 09:33
    John C Scott
    0

    Hey Tito - sorry to see you didn't get any help with this. Did you ever get this to work. I am facing a similar problem and not sure where to start.

  • Tito 314 posts 623 karma points
    Apr 06, 2022 @ 11:10
    Tito
    0

    No, what i have done is removing the preview button in umbraco. You can do it in Umbraco 8 subscribing to the SendingContentModel event of the EditorModelEventManager and then setting AllowPreview=false

    In Umbraco 9 you can subscribe to a notification handler this way:

    public class SubscribeToEditorModelEventsHandler : INotificationHandler<SendingContentNotification>
    {
        public void Handle(SendingContentNotification notification)
        {
            notification.Content.AllowPreview = false;
        }
    }
    
  • Tito 314 posts 623 karma points
    Nov 07, 2022 @ 09:38
    Tito
    1

    I have solved this issue. Using preview mode in Nextjs i can send to Umbraco controller a preview flag so Umbraco can send JSON of the unpublished content. To get an IPublishedContent from an unpublished id you can use IUmbracoContextFactory in Umbraco 10:

    using (var umbracoContextReference = _umbracoContextFactory.EnsureUmbracoContext())
                {
                    var contentCache = umbracoContextReference.UmbracoContext.Content;
                    if (contentCache != null)
                    {
                         return contentCache.GetById(true, id);
                    }
                }
    

    I dont know if this way is available in V8, but may be you could query IContent using the content service and convert it to IPublishedContent using this solution:

    https://dev.to/mattbrailsford/converting-icontent-to-ipublishedcontent-in-umbraco-v8-3m7i

Please Sign in or register to post replies

Write your reply to:

Draft