• Resolved aheinzel

    (@aheinzel)


    I would like to understand how w3-total-cache handles cookies, specifically Set-Cookie headers coming with a response. Based on some preliminary tests (see below) I now believe to understand that Set-Cookie response headers are stripped and not delivered from cache. Is my conclusion correct and is this the expected behaviour for W3 Total Cache page cache?

    Test setup & Tests
    Environment
    WordPress version: 6.4.2
    PHP version: 8.1
    Active plugins:

    • Code Snippets
    • Jetpack
    • WooCommerce
    • W3 Total Cache version 2.6.1

    A test page under the url /page2/
    An init hook to place a test cookie:

    add_action('init', function() {
       setcookie('test_cookie', '1', strtotime('+1 day'));
    });

    Relevant config
    W3 Total Cache enabled

    • Page cache enabled
    • One cookie-based cache group
      • cookie: no-cache

    Observed behaviour
    state: all caches have been purged – first request after cache purge

    curl  -s -c cookies.txt -D headers.txt https://w3dk.test.local/test2/ | \
       awk 'BEGIN{dump=0}{if(dump == 1 || $0 ~ /Performance optimized.*/){dump = 1; print $0}}' && \
       { echo "-------HEADERS---------"; cat headers.txt; }
    
    
    Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/
    
    Page Caching using Disk: Enhanced
    
    Page cache debug info:
    Engine:             Disk: Enhanced
    Cache key:          w3dk.test.local/test2/_index_slash.html
    Creation Time:      1705114168.000s
    Header info:
    Set-Cookie:         test_cookie=1; expires=Sun, 14-Jan-2024 02:49:28 GMT; Max-Age=86400
    Content-Type:       text/html; charset=UTF-8
    Link:               <https://w3dk.test.local/wp-json/>; rel="https://api.w.org/"
    Link:               <https://w3dk.test.local/wp-json/wp/v2/pages/15>; rel="alternate"; type="application/json"
    Link:               <https://w3dk.test.local/?p=15>; rel=shortlink
    
    
    Served from: w3dk.test.local @ 2024-01-13 03:49:28 by W3 Total Cache
    -->
    -------HEADERS---------
    HTTP/1.1 200 OK
    Date: Sat, 13 Jan 2024 02:49:28 GMT
    Server: Apache
    Link: <https://w3dk.test.local/wp-json/>; rel="https://api.w.org/", <https://w3dk.test.local/wp-json/wp/v2/pages/15>; rel="alternate"; type="application/json", <https://w3dk.test.local/?p=15>; rel=shortlink
    Set-Cookie: test_cookie=1; expires=Sun, 14-Jan-2024 02:49:28 GMT; Max-Age=86400
    Upgrade: h2,h2c
    Connection: Upgrade
    Last-Modified: Sat, 13 Jan 2024 02:49:28 GMT
    ETag: "6e424e9cfeb64252651c65b7eb79fbde"
    Vary: Accept-Encoding
    Referrer-Policy: no-referrer-when-downgrade
    Transfer-Encoding: chunked
    Content-Type: text/html; charset=UTF-8

    state: subsequent request – page is already cached

    curl  -s -c cookies.txt -D headers.txt https://w3dk.test.local/test2/ | \
       awk 'BEGIN{dump=0}{if(dump == 1 || $0 ~ /Performance optimized.*/){dump = 1; print $0}}' && \
       { echo "-------HEADERS---------"; cat headers.txt; }
    
    Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/
    
    Page Caching using Disk: Enhanced
    
    Page cache debug info:
    Engine:             Disk: Enhanced
    Cache key:          w3dk.test.local/test2/_index_slash.html
    Creation Time:      1705114168.000s
    Header info:
    Set-Cookie:         test_cookie=1; expires=Sun, 14-Jan-2024 02:49:28 GMT; Max-Age=86400
    Content-Type:       text/html; charset=UTF-8
    Link:               <https://w3dk.test.local/wp-json/>; rel="https://api.w.org/"
    Link:               <https://w3dk.test.local/wp-json/wp/v2/pages/15>; rel="alternate"; type="application/json"
    Link:               <https://w3dk.test.local/?p=15>; rel=shortlink
    
    
    Served from: w3dk.test.local @ 2024-01-13 03:49:28 by W3 Total Cache
    -->
    -------HEADERS---------
    HTTP/1.1 200 OK
    Date: Sat, 13 Jan 2024 02:49:42 GMT
    Server: Apache
    Upgrade: h2,h2c
    Connection: Upgrade
    Last-Modified: Sat, 13 Jan 2024 02:49:28 GMT
    ETag: "ee0a-60ecad4525a75"
    Accept-Ranges: bytes
    Content-Length: 60938
    Vary: Accept-Encoding
    Referrer-Policy: no-referrer-when-downgrade
    Content-Type: text/html; charset=UTF-8

    While the Set-Cookie: test_cookie=1; … header is present in the initial request (cache miss) the same cookie is absent when the request is served from cache.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Contributor Marko Vasiljevic

    (@vmarko)

    Hello @aheinzel

    Thank you for reaching out!

    Relevant config
    W3 Total Cache enabled

    • Page cache enabled
    • One cookie-based cache group
    • cookie: no-cache

    Can you please expand on the above-mentioned settings like One cookie-based cache group and cookie: no-cache?
    Are you testing headers for static files? is the option Don’t set cookies for static files enabled in Browser Cache settings?

    Thanks!

    Thread Starter aheinzel

    (@aheinzel)

    Hello @vmarko,

    thanks for getting back at me so quickly. Sorry for the confusion the cookie based cache-group was just mentioned for completeness but is not relevant in the given scenario.

    Regarding what I try to cache. My question refers to the page cache (enhanced) feature for caching dynamic pages. In this specific setting /test2/ is a WordPress page. The specific behavior I was interested in was if cookies set (set-cookie haeder in the response) when accessing a dynamic page are also set when the same page is served from cache. Let me please again include the headers from the two example requests provided above and try to highlight the relevant differences.

    #state cache empty - page is served from WordPress (set-cookie header is present)
    HTTP/1.1 200 OK
    Date: Tue, 16 Jan 2024 03:45:51 GMT
    Server: Apache
    Link: <https://w3dk.test.local/wp-json/>; rel="https://api.w.org/", <https://w3dk.test.local/wp-json/wp/v2/pages/15>; rel="alternate"; type="application/json", <https://w3dk.test.local/?p=15>; rel=shortlink
    X-Powered-By: W3 Total Cache/2.6.1
    Set-Cookie: test_cookie=1; expires=Wed, 17-Jan-2024 03:45:51 GMT; Max-Age=86400
    Upgrade: h2,h2c
    Connection: Upgrade
    Last-Modified: Tue, 16 Jan 2024 03:45:51 GMT
    ETag: "bdee7f0147cd5baa40bed5648ee6d9bc"
    Vary: Accept-Encoding
    Referrer-Policy: no-referrer-when-downgrade
    Transfer-Encoding: chunked
    Content-Type: text/html; charset=UTF-8
    
    #subsequent request - page is served from cache (set-cookie header is absent)
    HTTP/1.1 200 OK
    Date: Tue, 16 Jan 2024 03:47:09 GMT
    Server: Apache
    Upgrade: h2,h2c
    Connection: Upgrade
    Last-Modified: Tue, 16 Jan 2024 03:45:51 GMT
    ETag: "ee52-60f07f77dabfb"
    Accept-Ranges: bytes
    Content-Length: 61010
    Vary: Accept-Encoding
    Referrer-Policy: no-referrer-when-downgrade
    X-Powered-By: W3 Total Cache/2.6.1
    Content-Type: text/html; charset=UTF-8

    As far as I understand the inner workings of W3 Total Cache the page cache grabs the html and saves it to file in the cache dir. Subsequent requests are then directly redirected to the saved file instead of WordPress. Thus any http headers that were added by WordPress to the initial request are missing when the same content is served from cache (see Set-Cookie or Link headers in the example above).

    • This reply was modified 10 months, 2 weeks ago by aheinzel. Reason: span added was added to code output when post was submitted
    Plugin Contributor Marko Vasiljevic

    (@vmarko)

    Hello @aheinzel

    Thank you for your feedback.
    Let me make some tests and I’ll get back to you.

    Thanks!

    Plugin Contributor Marko Vasiljevic

    (@vmarko)

    Hello @aheinzel

    Sorry for the late reply. I was not able to replicate this.
    Is this also in the Github issues you posted?

    Thanks!

    Thread Starter aheinzel

    (@aheinzel)

    Hi @vmarko,

    sorry for the late reply. No I didn’t post this on github as I felt it is more a general usage/funciton of w3tc question. Shall I bring it over to github?

    best,

    Andreas

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Are Set-Cookie headers removed from page cache’ is closed to new replies.