Copied to clipboard

Flag this post as spam?

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


These support forums are now closed for new topics and comments.
Please head on over to http://eureka.ucommerce.net/ for support.

  • Bjarne Fyrstenborg 1280 posts 3990 karma points MVP 7x c-trib
    Sep 10, 2014 @ 09:44
    Bjarne Fyrstenborg
    0

    Product key as macro CacheParameters value

    I have a razor script for generating SEO meta data, title, canonical, meta description etc.

    I saw this post about how to write SEO friendly titles for product and category pages: http://www.ucommerce.net/en/blog/2014/01/27/i-like-bacon.aspx

    @using DesignersAndFriends.Library.Extensions
    @using umbraco.MacroEngines
    @using UCommerce.Infrastructure
    @using UCommerce.Infrastructure.Globalization
    @using UCommerce.EntitiesV2
    @using UCommerce.Extensions
    @using UCommerce.Runtime
    @using UCommerce.Api
    @inherits DynamicNodeContext
    
    @{
    
        DynamicNode siteNode = Current.GetSiteNode();
    
        // get context variables
        Category category = SiteContext.Current.CatalogContext.CurrentCategory;
        Product product = SiteContext.Current.CatalogContext.CurrentProduct;
        var localizationContext = ObjectFactory.Instance.Resolve<ILocalizationContext>();
    
        string seoTitle = Current.GetPropertyValue("seoTitle",Current.Name);
        string seoDescription = Current.GetPropertyAsString("seoDescription", siteNode.GetPropertyAsString("seoGlobalDescription"));
        string seoCanonical = Current.GetPropertyAsString("seoCanonical");
    
        bool seoNoODP = siteNode.GetPropertyAsBool("seoNoODP");
        bool seoNoYDIR = siteNode.GetPropertyAsBool("seoNoYDIR");
        string seoWebMasterToolAccount = siteNode.GetPropertyAsString("seoWebmasterToolAccount");
    
    
        string robots = string.Empty;
        bool seoNoIndex = Current.GetPropertyAsBool("seoNoIndex");
        bool seoNoFollow = Current.GetPropertyAsBool("seoNoFollow");
    
    
    }
    @if (seoNoIndex)
    {
        robots = "NOINDEX";
    } 
    
    @if (seoNoFollow)
    {
    
        if (!string.IsNullOrEmpty(robots))
        {
            robots += ", NOFOLLOW";
        }
        else
        {
            robots = "NOFOLLOW";
        }
    }
    
    @if (!seoNoODP)
    {
        if (!string.IsNullOrEmpty(robots))
        {
            robots += ", NOODP";
        }
        else
        {
            robots = "NOODP";
        }
    }
    @if (!seoNoYDIR)
    {
        if (!string.IsNullOrEmpty(robots))
        {
            robots += ", NOYDIR";
        }
        else
        {
            robots = "NOYDIR";
        }
    }
    @if (!string.IsNullOrEmpty(robots))
    {
      <meta name="robots" content="@robots" />
    }
    
    
    @if (!string.IsNullOrWhiteSpace(seoWebMasterToolAccount))
    {
      <meta name="google-site-verification" content="@seoWebMasterToolAccount" />
    }
    
    @* template for uCommerce products *@
    @if (product != null)
    {
        var property = product.GetProperty("metaTitle", localizationContext.CurrentCultureCode);
        if (property != null && !string.IsNullOrWhiteSpace(property.GetValue().ToString()))
        {
            seoTitle = property.GetValue().ToString();
        }
        else
        {
            seoTitle = product.DisplayName();
        }
    
    }
    @* template for uCommerce categories *@
    else if (category != null)
    {
        var property = category.GetProperty("metaTitle", localizationContext.CurrentCultureCode);
        if (property != null && !string.IsNullOrWhiteSpace(property.GetValue().ToString()))
        {
            seoTitle = property.GetValue().ToString();
        }
        else
        {
            seoTitle = category.DisplayName();
        }
    }
    
        <meta name="description" content="@seoDescription" />
        <title>@seoTitle - @siteNode.GetPropertyAsString("siteName", siteNode.Name)</title>
    
    @if (!string.IsNullOrWhiteSpace(seoCanonical))
    {
        <link rel="canonical" href="@seoCanonical" />
    }
    
    and the macro inserted in the masterpage template (webforms):

    <umbraco:Macro FileLocation="~/macroScripts/SEO/ncSiteSeo.cshtml" Cache="86400" CacheParameters="[#pageID]" CacheName="ncSiteSeo" runat="server" />
    however because the product node in content is the same and the cache value of 86400, the product title in the browser window doesn't change when going from one product page to another - the first product title is cached.
    I works when disabling the caching when setting the value in Cache to 0, but is there a way to pass a unique value in the CacheParameters besides the pageID, so it's unique for every product and category page?
    /Bjarne
  • Morten Skjoldager 440 posts 1499 karma points
    Sep 15, 2014 @ 10:19
    Morten Skjoldager
    0

    Hi,

    Not sure how it actually works but:

    If it is possible to modify the parameter on the request, you can create a field on product definition and category definition that is a guid, and use that as the cacheparameter? Will that do? You can the just recieve the current product / category from the CatalogLibrary.GetCurrentProduct / GetCurrentCategory

    Regards

    Morten

  • Bjarne Fyrstenborg 1280 posts 3990 karma points MVP 7x c-trib
    Sep 15, 2014 @ 10:46
    Bjarne Fyrstenborg
    100

    We have a querystring parameter for product and category, where the url actually is rewritten for SEO optimization.

    I had first added pageID as the first parameter, but that didn't seem to work well with caching and as in uCommerce the is a Umbraco content node "template" for category and product, with the same id. So when I was browsing to another category or product it kept the old cached value for the previously viewed product/category.

    So instead I now have product and category parameters first, so when it isn't a product or category page I guess it will be empty or null and fallback to pageID (for all other pages). It seems to works as we want it.

    <umbraco:Macro FileLocation="~/macroScripts/SEO/ncSiteSeo.cshtml" Cache="86400" CacheParameters="[@product],[@category],[#pageID]" CacheName="ncSiteSeo" runat="server" />

    /Bjarne

Please Sign in or register to post replies

Write your reply to:

Draft