Configure URL rewriting
To ensure that relative links in HTML are rendered properly when integrating a rendering host with advanced Sitecore editors, Headless Services rewrites relative URLs in the returned markup to be absolute. The default tags and attributes included in this processing are as follows:
Tag name |
Attribute name |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Disable URL Rewriting
If you do not want Headless Services to rewrite relative URLs in the returned markup into absolute URLs, you can disable URL rewriting.
To disable URL rewriting:
-
For the configured
http
render engine, change the value of the optionEnableRelativeLinkProcessing
tofalse
:RequestResponsexml<javaScriptServices> <renderEngines> <renderEngine name="http"> <instance id="RenderEngineInstance" inherits="defaults"> <EnableRelativeLinkProcessing>false</EnableRelativeLinkProcessing> </instance> </renderEngine> </renderEngines> </javaScriptServices>
Configure rewritten HTML attributes
You can customize URL rewrites by overriding the type Sitecore.JavaScriptServices.ViewEngine.Http.RenderEngineOptionsResolver
.
To configure rewritten HTML attributes:
-
Reference the nuget package
Sitecore.JavaScriptServices.ViewEngine.Http
. -
Create a type that inherits the type
Sitecore.JavaScriptServices.ViewEngine.Http.RenderEngineOptionsResolver
. -
Override the
ResolveAll
andResolveForId
. -
Override the
LinkAttributeMap
field of options returned by the base method:RequestResponsec#public override IEnumerable<HttpRenderEngineOptions> ResolveAll(RenderEngineOptions runtimeOptions = null) { var optionsCollection = base.ResolveAll(runtimeOptions); foreach (var options in optionsCollection) { options.LinkAttributeMap.Add("video", new[] { "poster", "src" }); } return optionsCollection; } public override HttpRenderEngineOptions ResolveForId(string id, RenderEngineOptions runtimeOptions = null) { var options = base.ResolveForId(id, runtimeOptions); options.LinkAttributeMap.Add("video", new[] { "poster", "src" }); return options; }
-
Create a dependency configurator and override the
IRenderEngineOptionsResolver
service registration:RequestResponsec#public void Configure(IServiceCollection serviceCollection) { serviceCollection.AddTransient<IHttpRenderEngineFactory, HttpRenderEngineFactory>(); serviceCollection.AddTransient<IConfigurationReader, XmlConfigurationReader>(); serviceCollection.AddSingleton<IHttpSessionStateResolver, HttpSessionStateResolver>(); serviceCollection.AddTransient<IOverrideOptionsResolver, OverrideOptionsResolver>(); serviceCollection.AddSingleton<IRenderEngineOptionsResolver, CustomRenderEngineOptionResolver>(); serviceCollection.AddTransient<IHttpClientFactory, HttpClientFactory>(); }
-
Create a config patch with the following patch:
RequestResponsexml<services> <configurator patch:instead="configurator[@type='Sitecore.JavaScriptServices.ViewEngine.Http.RegisterDependencies, Sitecore.JavaScriptServices.ViewEngine.Http']" type="<CustomTypeName>, <CustomAssembly>" /> </services>
-
Deploy the custom assembly and configuration patch to your Sitecore instance.
Add a path to ignored paths
Some relative URL paths must not be rewritten by Headless Services. By default, these are:
-
-/media/
-
~/media/
-
-/jssmedia/
-
~/jssmedia/
Adding or removing paths can be done by overriding the type Sitecore.JavaScriptServices.ViewEngine.Http.RenderEngineOptionsResolver
.
To add a URL path to the ignored paths:
-
Reference the nuget package
Sitecore.JavaScriptServices.ViewEngine.Http
. -
Create a type that inherits the type
Sitecore.JavaScriptServices.ViewEngine.Http.RenderEngineOptionsResolver
. -
Override the
ResolveAll
andResolveForId
. -
Override the
LinkAttributeMap
field of options returned by the base method:RequestResponsec#public override IEnumerable<HttpRenderEngineOptions> ResolveAll(RenderEngineOptions runtimeOptions = null) { var optionsCollection = base.ResolveAll(runtimeOptions); foreach (var options in optionsCollection) { options.IgnoredPaths.Add("sitecore/shell/"); } return optionsCollection; } public override HttpRenderEngineOptions ResolveForId(string id, RenderEngineOptions runtimeOptions = null) { var options = base.ResolveForId(id, runtimeOptions); options.IgnoredPaths.Add("sitecore/shell/"); return options; }
-
Create a dependency configurator and override the
IRenderEngineOptionsResolver
service registration:RequestResponsec#public void Configure(IServiceCollection serviceCollection) { serviceCollection.AddTransient<IHttpRenderEngineFactory, HttpRenderEngineFactory>(); serviceCollection.AddTransient<IConfigurationReader, XmlConfigurationReader>(); serviceCollection.AddSingleton<IHttpSessionStateResolver, HttpSessionStateResolver>(); serviceCollection.AddTransient<IOverrideOptionsResolver, OverrideOptionsResolver>(); serviceCollection.AddSingleton<IRenderEngineOptionsResolver, CustomRenderEngineOptionResolver>(); serviceCollection.AddTransient<IHttpClientFactory, HttpClientFactory>(); }
-
Create a config patch with the following patch:
RequestResponsexml<services> <configurator patch:instead="configurator[@type='Sitecore.JavaScriptServices.ViewEngine.Http.RegisterDependencies, Sitecore.JavaScriptServices.ViewEngine.Http']" type="<CustomTypeName>, <CustomAssembly>" /> </services>
-
Deploy the custom assembly and configuration patch to your Sitecore instance.