Skip to content

Instantly share code, notes, and snippets.

@VesselinVassilev
Last active July 10, 2018 04:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save VesselinVassilev/3c675b2c3e87d207014b88da827105d0 to your computer and use it in GitHub Desktop.
Save VesselinVassilev/3c675b2c3e87d207014b88da827105d0 to your computer and use it in GitHub Desktop.
Remove the sf_appPath javascript variable from Sitefinity pages
private void SystemManager_ApplicationStart(object sender, EventArgs e)
{
// remove the sf_appPath script from the header
EventHub.Subscribe<IPagePreRenderCompleteEvent>(evt => PagePreRenderCompleteEvent(evt));
}
private void PagePreRenderCompleteEvent(IPagePreRenderCompleteEvent evt)
{
if (!SystemManager.IsDesignMode)
{
if (evt.PageSiteNode.Url.Contains("/amp/"))
{
try
{
// we are in an amp page so
// we should remove the sf_appPath from the head
var page = evt.Page;
var sfAppPathScript = (from ctrls in page.Header.Controls.OfType<LiteralControl>()
where ctrls.Text.Contains("var sf_appPath=")
select ctrls).FirstOrDefault();
if (sfAppPathScript != null)
{
page.Header.Controls.Remove(sfAppPathScript);
}
// fix the canonnical url of the amp page as well
// by default it would point to the same amp page
// e.g. <link rel="canonical" href="https://site.com/en/amp/home">
// we want it to point to the main page
var canonical = (from ctrls in page.Header.Controls.OfType<HtmlLink>()
where ctrls.Attributes["rel"].Equals("canonical", StringComparison.CurrentCultureIgnoreCase)
select ctrls).FirstOrDefault();
if (canonical != null)
{
page.Header.Controls.Remove(canonical);
}
canonical = new HtmlLink();
canonical.Attributes.Add("rel", "canonical");
// remove the /amp/ path from the URL
var href = evt.PageSiteNode.Url.Replace("/amp/", "/");
// resolve the url as absolute
href = Telerik.Sitefinity.Web.RouteHelper.ResolveUrl(href, Telerik.Sitefinity.Web.UrlResolveOptions.Absolute);
canonical.Href = href;
page.Header.Controls.Add(canonical);
}
catch (Exception e)
{
Log.Write(e);
// continue, do not break the page
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment