2

Our requirement is to show site specific error page if the requested page is not found.

We have handled item not found using HttpRequestBegin pipeline Item resolver. If the context item is null we are setting the context item as page not found item.

But in headless mode always getting context item as sitecore hence not able to set the page not found item to context item.

Please let me know how to implement the 404 errors in headless mode.

Thanks

2
  • Why don't you use the out of the box "page not found" process? why have you implemented your own HttpRequestBegin pipeline? Sep 1, 2020 at 7:54
  • 1
    We have to display the site specific error page hence we have implemented Sep 1, 2020 at 12:50

1 Answer 1

2

There are two types of Item resolver in JSS:

First and which you already implemented:

public class ResolveItem : HttpRequestProcessor
    {
        public override void Process(HttpRequestArgs args)
        {
            var item = ANY YOUR LOGIC TO GET 404 item.
            Context.Item = item;
        }
    }

<configuration
    xmlns:patch="http://www.sitecore.net/xmlconfig/"
    xmlns:set="http://www.sitecore.net/xmlconfig/set/">
  <sitecore>
    <pipelines>
      <httpRequestBegin>
        <processor type="{your-namespace}.ResolveItem, {your-assembly-name}"
                   patch:after="*[@type='Sitecore.Pipelines.HttpRequest.ItemResolver, Sitecore.Kernel']">
        </processor>
      </httpRequestBegin>
    </pipelines>
  </sitecore>
</configuration>

And Second one Item resolver is for Layout Service:

public class LayoutServiceItemResolver
    {
            
        public void Process(RequestBeginArgs args)
        {
                var item = ANY YOUR LOGIC TO GET 404 item.
                Context.Item = item;           
        }
    }



<configuration
    xmlns:patch="http://www.sitecore.net/xmlconfig/"
    xmlns:set="http://www.sitecore.net/xmlconfig/set/">
  <sitecore>
    <pipelines>
      <mvc.requestBegin>
        <processor type="{your-namespace}.LayoutServiceItemResolver, {your-assembly-name}" resolve="true">
        </processor>
      </mvc.requestBegin>
    </pipelines>
  </sitecore>
</configuration>

Both of them are required. First one is used when you request the page by URL, second one - when you navigate on you browser (which means utilizes a jss routing)

8
  • Thanks for the detail explanation. with first approach it’s working fine in integrated mode. The issue is with the headless implementation. Do we need to change anything for the headless implementation? Sep 1, 2020 at 17:24
  • Have you implemented LayoutServiceItemResolver as on example above? please note, that it is another pipeline as you have. This is used to resolve item exactly for headless. Sep 1, 2020 at 17:31
  • Not yet implemented, will try this and IContentSearcher, IUrlService are in which assembly i am not able to get these. Sep 2, 2020 at 12:48
  • @NirupamaReddy am sorry, I forgot to remove it from example. You actually don't need this constructor. I have updated the answer Sep 2, 2020 at 12:49
  • Thanks for the update @Artsem Prashkovich. I have tried this and here as well the context item by default coming as sitecore, end up with same issue Sep 2, 2020 at 14:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.