0

I am implementing this article so I can switch themes based on the a cookie or query string. My code is as follows.

protected override Item DoGetThemeItem(Item item, DeviceItem device)
    {
        Item themeItem = base.DoGetThemeItem(item, device);
        Item newThemeItem;
        string currentUrl = HttpContext.Current.Request.Url.ToString();
        var queryString = HttpUtility.ParseQueryString(HttpContext.Current.Request.Url.Query);

        if (HttpContext.Current.Request.Cookies["ThemeCookie"] != null)
        {
            string themeValue = HttpContext.Current.Request.Cookies["ThemeCookie"].Value;

            if (themeValue == "something")
            {
                newThemeItem = global::Sitecore.Context.Database.GetItem(new ID(Foundation.Sitecore.Constants.MyThemeID));                    
            }else
            {
                newThemeItem = themeItem;
            }
            return newThemeItem;
        }
        else if (!string.IsNullOrEmpty(queryString["theme"]))
        {
            string themeValue = queryString["theme"];

            if (themeValue == "something")
            {
                newThemeItem = global::Sitecore.Context.Database.GetItem(new ID(Foundation.Sitecore.Constants.MyThemeID));
            }
            else
            {
                newThemeItem = themeItem; 
            }
            return newThemeItem;
        }          
        else
        {
            return themeItem; 
        }
    }

    private void SetThemeCookie(string themeName)
    {
        HttpCookie cookie = new HttpCookie("ThemeCookie", themeName);
        cookie.Expires = DateTime.Now.AddDays(7);
        cookie.SameSite = System.Web.SameSiteMode.None;
        cookie.Path = "/";
        cookie.Domain = "dev.local";
        HttpContext.Current.Response.Cookies.Add(cookie);
    }

The problem it only switches themes after I run "sxa rebuild All". Any idea why?

1
  • Most probably you haven't replaced original ThemeContext with your custom one - the article mentions configuration change which does that
    – Marek Musielak
    Oct 23, 2023 at 5:49

1 Answer 1

4

I have a few suggestions as mentioned below, could you please try them if not done?

  1. You can locate the App_Config/Sitecore/Mvc/Sitecore.Mvc.config file, there is a setting called Mvc.UsePhysicalViewsIfNewer, change its default value false to true.
  2. As per the blog post, please ensure this custom class [CustomAssetLinksGenerator] is also added to your implementation which has a method GenerateAssetLinks. You can take this class from this. Git Repo https://github.com/jitendrachilate/ThemeSwitcher.git https://github.com/jitendrachilate/ThemeSwitcher/blob/master/Pipeline/CustomThemingContext.cs

Also, please refer to the below git link which has CustomSxaLayout.cshtml file. Please check line no 10 wherein the CustomThemingContext.cs file is referred to. Please ensure the same in your implementation.

https://github.com/jitendrachilate/ThemeSwitcher/blob/master/Views/SxaLayout/CustomSxaLayout.cshtml  

Blog Post: https://blogs.perficient.com/2023/09/14/switch-sxa-themes-based-on-cookie-value/

Please let me know if you still have any queries. Thank you.

1
  • Thanks, it was number (2). I forgot to implement CustomAssetLinkGenerator.
    – William
    Oct 24, 2023 at 22:12

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.