We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 19328
    • 433 Posts
    Is it possible to get the value of a cookie in a dynamic snippet with xFPC?

    In the template we've got:
    [[xFPCAjax? &resource=`190`]]


    In resource 190 we've got:
    [[!getCookie]]


    The snippet getCookie:
    return 'cookie:'.$_COOKIE['STYLE'];


    Depending on the cookie different chunks are shown. This is used for a style switcher.

    In combination with xFPC the cookie value is not retrieved. What should we do to make it work?

    Environment: MODX 2.2.5 & xFPC 2.1.0

    Thanks for any help!

    This question has been answered by b03tz. See the first response.

    [ed. note: michelle84 last edited this post 11 years, 4 months ago.]
      • 2611
      • 394 Posts
      I am betting that the cookie did not get set well. Look at your developers console to see if the cookie value is there and make sure you make the ajax request to the same domain. Also be sure touse the same capitals in the cookie name for setting and getting.

      CCan't imagine that this is an xFPX issue.
        Follow me on twitter: @b03tz
        Follow SCHERP Ontwikkeling on twitter: @scherpontwikkel
        CodeMaster
        • 19328
        • 433 Posts
        We checked, the cookie does get set properly. It worked before we added xFPC.

        We narrowed it down some more. The problem we have is with an xFPCAjax snippet in the html head section of the document. Based on the cookie this is where the right CSS file is included. We found out after a lot of trial and error that the cookie value is only refreshed when the SAME xFPCAjax snippet is also included elsewhere on the page (in the body). Strange isn't it! Could it be that the <span> that is included is a problem (because it's in the head section)?

        We'll continue testing but if this new discovery gives you new ideas of what might be the cause we would be glad to hear!
          • 2611
          • 394 Posts
          A span in the head is illegal. Can you show me your code that surrounds the tag? I assume you use some kind of dynamic css injection because since it is an ajax request you surely can not do this:

          href="css/[[xFPCAjax]].css
            Follow me on twitter: @b03tz
            Follow SCHERP Ontwikkeling on twitter: @scherpontwikkel
            CodeMaster
            • 19328
            • 433 Posts
            The snippet we have in the head section of the html document, and now in the resource that is called with the xFPCAjax snippet contains the following:

            // cookie 0 = high contrast, cookie 1 = standard
            if(isset($_COOKIE['STYLE'])) {
              if ($_COOKIE['STYLE'] == 0) {
            	  $output = '<link href="assets/templates/styles/screenHigh.css" rel="stylesheet" type="text/css" />';
              } else {
            	  $output = '<link href="assets/templates/styles/screen.css" rel="stylesheet" type="text/css" />';
              }
            } else {
            	$output = '<link href="assets/templates/styles/screen.css" rel="stylesheet" type="text/css" />';
            }
            
            return $output;


            So basically it puts the correct link to the external css file in the head section.
            We thought that the problem could be with the fact that css gets processed before javascript ?

            • discuss.answer
              • 2611
              • 394 Posts
              I don't know if you have solved this already...but I'd solve this using custom javascript. Statically always add the screen.css and with javascript determine the right conditions, remove the screen.css when neccesary and add the screenHigh.css

              The AJAX snippet always adds a container element around the content so it has a reference of where to put it. And adding container elements in the head is illegal, so it will never work...and even if it does work it breaks validation and probably browser compatibility.
                Follow me on twitter: @b03tz
                Follow SCHERP Ontwikkeling on twitter: @scherpontwikkel
                CodeMaster
                • 19328
                • 433 Posts
                Yeah, we also figured it wouldn't work this way because the container element is of course illegal in the head section. We ended up not using xFPC for this project, because the site has to work without javascript (but we do use it for other projects, it's great!). Otherwise your solution for removing/adding the particular css with javascript would be a good idea. Thanks for your help and your great work on this addon!

                Anyway, for other readers, this problem was not related to xFPC and cookies after all.
                  • 2611
                  • 394 Posts
                  Very good, thanks for the follow up!
                    Follow me on twitter: @b03tz
                    Follow SCHERP Ontwikkeling on twitter: @scherpontwikkel
                    CodeMaster
                    • 5904
                    • 58 Posts
                    Quote from: b03tz at Jan 09, 2013, 10:22 AM
                    Very good, thanks for the follow up!

                    Installed xFPC a few days ago and, as others have commented, it's giving amazingly quick page loads. Thank you!

                    I've been playing with the options in System Settings and notice that when I enable minifycss and minifyjs there's no apparent change to the way the CSS and JS files appear in cached pages. I was expecting similar output to Google Minify (http://code.google.com/p/minify/).

                    In the Error Log on each page load:

                    [2015-06-10 06:10:32] (ERROR @ core/cache/includes/elements/modplugin/18.include.cache.php : 101) PHP warning: Cannot modify header information - headers already sent by (output started at core/model/modx/modresponse.class.php:189)
                    [2015-06-10 06:10:39] (ERROR @ core/cache/includes/elements/modplugin/18.include.cache.php : 101) PHP warning: Cannot modify header information - headers already sent by (output started at core/model/modx/modresponse.class.php:189)
                    [2015-06-10 06:10:39] (ERROR @ core/cache/includes/elements/modplugin/18.include.cache.php : 115) PHP warning: Cannot modify header information - headers already sent by (output started at core/model/modx/modresponse.class.php:189


                    where plugin 18 is xFPC.

                    Looked in 18.include.cache.php and the two lines referenced are:

                    101:  header ('X-XFPC-Cache-Active: Yes');
                    115:  header ('X-XFPC-Cache: Miss');
                    


                    Deleted all in the core/cache directory and these errors persist.

                    Any suggestions gratefully received.

                    PS MODX Revolution 2.3.2-pl (traditional)

                    PPS I'm posting to this old thread as I've yet to discover a way to create a new thread here. Any tips welcome smiley [ed. note: craigphiz last edited this post 8 years, 10 months ago.]