1

I am trying to play around with cookies in SSJS and run into some problems. First of all, I have a few possible solutions for this problem but this question is more about getting an understanding of why it happens in the first place.

Logic:

If a cookie is not set, we set a new cookie.

If the given cookie has a wrong value, we expire the cookie and set it new.


Problem:

The problem happens when I try to expire the cookie and check upon the same cookie again during the same page load. This results in an infinity loop


My hypothesis:

SSJS cookies need to be transmitted to the browser after the script has been fully executed. During the execution, the cookie value will stay untouched even if a cookie will be modified.

This behaviour is different in plain javascript as it is executed client side.


Question:

Is my hypothesis correct? Is this an issue because of server side execution while checking client side data?


Test (plain javascript):

Here is a simple javascript cookie script which will reset a cookie when the value is not 123 and not "":

<script type="text/javascript">

    load();

    function load() {

        var cookie = document.cookie.replace(/(?:(?:^|.*;\s*)MySampleCookie\s*\=\s*([^;]*).*$)|^.*$/, "$1");
        console.log('get cookie: '+cookie);

        if( !cookie || cookie == "" ) {
            console.log("cookie empty");
            set();
        } else if( cookie != "123" ) {
            console.log("cookie wrong");
            expire();
        }
    }

    function set() {
        console.log('set new cookie');
        document.cookie = "MySampleCookie=123; expires=Fri, 31 Dec 9999 23:59:59 GMT";
    }

    function expire() {
        console.log('expire cookie');
        document.cookie = "MySampleCookie=; expires=Thu, 01 Jan 1970 00:00:00 GMT";
        load();
    }
</script>

Result:

  • Cookie value set to 1234 hence the cookie would need to be expired and reset.

get cookie: 1234

cookie wrong

expire cookie

get cookie:

cookie empty

set new cookie


Test (ssjs):

Same code in SSJS:

<script runat=server>
    Platform.Load("Core", "1.1.1");


    load();


    function load() {

        var cookie = Platform.Request.GetCookieValue("MySampleCookie");
        Write(Stringify('get cookie: '+cookie));

        if( !cookie || cookie == "" ) {
            Write(Stringify("cookie empty"));
            set();
        } else if( cookie != "123" ) {
            Write(Stringify("cookie wrong"));
            expire();
        }


    }

    function set() {
        Write(Stringify('set new cookie'));
        var d = new Date("Fri, 31 Dec 9999 23:59:59 GMT");
        Platform.Response.SetCookie("MySampleCookie","123",d);
    }


    function expire() {
        Write(Stringify('expire cookie'));
        var d = new Date("Thu, 01 Jan 1970 00:00:00 GMT");
        Platform.Response.SetCookie("MySampleCookie","",d);
        load();
    }
</script>

Result:

  • Cookie value set to 1234 hence the cookie would need to be expired and reset.

infinity loop

2

Cookies are maintained through request headers sent by the browser and response headers sent by the server (SSJS). There is only one request and one response going on between the browser and the server. Your load() function inspects the cookie header of the request, finds the value is "1234" and decides to run your expire() function. Once done, the expire() function calls load() and the properties of the request have not changed... the cookie in the request still equals "1234" and so your infinite loop begins. The properties of the request are immutable - they're just facts about what was sent by the client.

The difference between your client-side and server-side JavaScript is that on the client side you're inspecting the value of the actual cookie on the client machine. You set it or expire it and its value changes. On the next iteration your cookie's expired and load() sets its value to "123".

With respect to your question around the order of execution, the client makes a request sending a cookie header if present, the server inspects the cookie header (GetCookie), does it's processing (SetCookie) and then sends the response back to the client including the cookie header set during processing.

1
  • Thank you for the detailed explanation. That was helpful. – shd.lux Apr 26 '20 at 12:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.