LSCache Mediawiki Plugin

#81
I have now posted to Github. Hopefully I described things sufficiently to be helpful!

@AndreyPopov

Bear in mind that at wiki-test.westeros.org, I have this in LocalSettings:
PHP:
$wgHooks['BeforePageDisplay'][] = 'onBeforePageDisplay';
function onBeforePageDisplay(OutputPage & $out, Skin & $skin) {
    $script = '<link href="https://fonts.googleapis.com/css?family=Uncial+Antiqua" rel="preload" as="style">
        <link rel="preconnect" href="https://fonts.gstatic.com/" crossorigin>
    <link href="https://fonts.googleapis.com/css?family=Uncial+Antiqua" rel="stylesheet">
<link rel="preconnect" href="https://pagead2.googlesyndication.com/" crossorigin>
<link rel="preconnect" href="https://stats.g.doubleclick.net" crossorigin>
<link rel="preconnect" href="https://images-na.ssl-images-amazon.com" crossorigin>
<link rel="preconnect" href="https://s3.amazonaws.com" crossorigin>
    <meta property="fb:app_id" content="187755409648">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="twitter:card" content="summary" />
    <script type="application/ld+json">
{ "@context" : "http://schema.org",
  "@type" : "Organization",
  "name" : "Westeros",
  "url" : "http://westeros.org",
  "sameAs" : [ "http://www.facebook.com/Westeros",
    "http://www.twitter.com/westerosorg",
    "http://www.youtube.com/Balerion300"]
}
    }
</script>
<script type="text/javascript" src="//cdn.geni.us/snippet.min.js" defer></script>
<script type="text/javascript">
document.addEventListener("DOMContentLoaded", function() {
var tsid =9701;
Genius.amazon.convertLinks(tsid, true, "http://buy.geni.us"); });
</script>';
## <script type="text/javascript" src="https://lngtd.com/westeros_wiki.js"></script>
    global $wgContentNamespaces;
    $title = $skin->getRelevantTitle();
    $namespaceNumber = $title->getNamespace();
    if($namespaceNumber == 0) {
    $out->addHeadItem("organization", $script);
    };
    return true;
};
None of that code (except the ad code that sets cookies that I have commented out) causes a problem with the caching. So I think it's not the hook or how it inserts the code, because I don't think OLS sees that page until after the hook has run and the page is rendered and moved to be sent to the requester.
 
#84
PHP:
$wgHooks['BeforePageDisplay'][] = 'onBeforePageDisplay';
function onBeforePageDisplay(OutputPage & $out, Skin & $skin) {
    $script = '<link href="https://fonts.googleapis.com/css?family=Uncial+Antiqua" rel="preload" as="style">
        <link rel="preconnect" href="https://fonts.gstatic.com/" crossorigin>
    <link href="https://fonts.googleapis.com/css?family=Uncial+Antiqua" rel="stylesheet">
<link rel="preconnect" href="https://pagead2.googlesyndication.com/" crossorigin>
<link rel="preconnect" href="https://stats.g.doubleclick.net" crossorigin>
<link rel="preconnect" href="https://images-na.ssl-images-amazon.com" crossorigin>
<link rel="preconnect" href="https://s3.amazonaws.com" crossorigin>
    <meta property="fb:app_id" content="187755409648">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="twitter:card" content="summary" />
    <script type="application/ld+json">
{ "@context" : "http://schema.org",
  "@type" : "Organization",
  "name" : "Westeros",
  "url" : "http://westeros.org",
  "sameAs" : [ "http://www.facebook.com/Westeros",
    "http://www.twitter.com/westerosorg",
    "http://www.youtube.com/Balerion300"]
}
    }
</script>
<script type="text/javascript" src="//cdn.geni.us/snippet.min.js" defer></script>
<script type="text/javascript">
document.addEventListener("DOMContentLoaded", function() {
var tsid =9701;
Genius.amazon.convertLinks(tsid, true, "http://buy.geni.us"); });
</script>';
## <script type="text/javascript" src="https://lngtd.com/westeros_wiki.js"></script>
    global $wgContentNamespaces;
    $title = $skin->getRelevantTitle();
    $namespaceNumber = $title->getNamespace();
    if($namespaceNumber == 0) {
    $out->addHeadItem("organization", $script);
    };
    return true;
};

try add GA script to this function


PHP:
$wgHooks['BeforePageDisplay'][] = 'onBeforePageDisplay';
function onBeforePageDisplay(OutputPage & $out, Skin & $skin) {
    $script = '<link href="https://fonts.googleapis.com/css?family=Uncial+Antiqua" rel="preload" as="style">
        <link rel="preconnect" href="https://fonts.gstatic.com/" crossorigin>
    <link href="https://fonts.googleapis.com/css?family=Uncial+Antiqua" rel="stylesheet">
<link rel="preconnect" href="https://pagead2.googlesyndication.com/" crossorigin>
<link rel="preconnect" href="https://stats.g.doubleclick.net" crossorigin>
<link rel="preconnect" href="https://images-na.ssl-images-amazon.com" crossorigin>
<link rel="preconnect" href="https://s3.amazonaws.com" crossorigin>
    <meta property="fb:app_id" content="187755409648">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="twitter:card" content="summary" />
    <script type="application/ld+json">
{ "@context" : "http://schema.org",
  "@type" : "Organization",
  "name" : "Westeros",
  "url" : "http://westeros.org",
  "sameAs" : [ "http://www.facebook.com/Westeros",
    "http://www.twitter.com/westerosorg",
    "http://www.youtube.com/Balerion300"]
}
    }
</script>

<script type="text/javascript" async src="https://www.googletagmanager.com/gtag/js?id=G-xxx"></script>
<script type="text/javascript">
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());

gtag('config', 'G-XXXX');
</script>

<script type="text/javascript" src="//cdn.geni.us/snippet.min.js" defer></script>
<script type="text/javascript">
document.addEventListener("DOMContentLoaded", function() {
var tsid =9701;
Genius.amazon.convertLinks(tsid, true, "http://buy.geni.us"); });
</script>';
## <script type="text/javascript" src="https://lngtd.com/westeros_wiki.js"></script>
    global $wgContentNamespaces;
    $title = $skin->getRelevantTitle();
    $namespaceNumber = $title->getNamespace();
    if($namespaceNumber == 0) {
    $out->addHeadItem("organization", $script);
    };
    return true;
};
 
#87
So, after applying to bugs@litespeed with my Github report, I got this response:
'
I looked into the Gtag extension code, it should work with litespeed cache plugin.

but if Gtag set cookie in javascript, it should not work, since OLS/LSWS will drop all set-cookie headers as show in the bottom of page: https://docs.litespeedtech.com/lscache/devguide/controls/

LSC-Cookie¶
Generally speaking, cookies will contain private information, so by default, all pages that are cached in LiteSpeed Cache will have the Set-Cookie header dropped. The LSC-cookie header is used instead to send out a Set-Cookie header for the pages served from cache. The LSC-Cookie header follows the same syntax as a Set-Cookie header, but will be sent to the client along with the cached page.

In other words, LSC-Cookie is a cacheable Set-Cookie, which will be converted and sent out as a Set-Cookie.

You may solve this problem by adding specific headers (OLS) or .htaccess directives, if you know the cookie name which Gtag uses.
add header(OLS) X-LiteSpeed-Vary: cookie=Gtag_cookie_name
add .htaccess directive(LSWS) RewriteRule .? - [E=Cache-Vary:Gtag_cookie_name]

Please refer https://docs.litespeedtech.com/lscache/devguide/advanced/ for more information.
Now, two things make me wonder: first, @LiteCache, didn't Gtag and caching work without needing to fiddle with .htaccess or header adding in LSWS? Second, I've gone ahead and tried to add the described X-Litespeed-Vary header at wiki-test.westeros.org, but it seems to do nothing as far as caching goes... Should I follow up with them on these points?
 

LiteCache

Active Member
#88
@Balerion

The answer from the Github Report doesn't give me a coherent explanation that can be reproduced. If this answer were logically correct, then the same malfunction could also be reproduced with LSWS, but it is not. MediaWiki works as expected on LSWS with or without Gtag. Whether the Gtag cookie is set by Javascript or by any other function is completely irrelevant. A cookie only plays a role if a different cache vary or an additional cache copy is required based on a cookie. Otherwise any cookie is ignored for LScache. In the case of Gtag, however, this is not necessary.

The cache malfunction in relation to the cache status "miss" can be recreated with LSWS in which the cache control is activated via PHP, but there is a no-cache statement in the .htaccess. In such a case, the cache status would always be miss instead of no-cache, which would correspond to the cache behavior in your MediaWiki installation.

To further narrow down the cause so that you can rule out either the cache plugin or OLS as the cause, you could set a cookie via JS, which would correspond to the Gtag function. That wouldn't solve the problem in the end, but it would at least give you a better idea of where to look for the cause.
 
Last edited:
#89
@Balerion

To further narrow down the cause so that you can rule out either the cache plugin or OLS as the cause, you could set a cookie via JS, which would correspond to the Gtag function. That wouldn't solve the problem in the end, but it would at least give you a better idea of where to look for the cause.
That was an interesting thought. Put in this simple script in LocalSettings:

Code:
<script>
    // Set the value of the _arb cookie to "01"
    document.cookie = "_arb=01";

    // Optional: You can also log a message to the console to confirm that the cookie is set
    console.log("Cookie _arb has been set to 01");
</script>
And... that seems to work fine. Once it has the cookie, the cache hits. Don't have to add it to X-LiteSpeed-Vary or anything.

But I thought about it and I realized that the Google Analytics cookie is a two-part cookie, and one of those cookies changes its numerical value (basically a date code) with each visit. So I tried this instead to emulate that:

PHP:
<script>
    // Get the current date and time
    var currentDate = new Date();

    // Format the date as YYYY-MM-DD HH:MM:SS (you can adjust the format as needed)
    var formattedDate = currentDate.toISOString().slice(0, 19).replace("T", " ");

    // Set the value of the _arb cookie to the formatted date
    document.cookie = "_arb=" + formattedDate;

    // Optional: You can also log a message to the console to confirm that the cookie is set
    console.log("Cookie _arb has been set to " + formattedDate);
</script>
And... now I don't get hits, because the value of each cookie is changing with each visit, and this seems to be what's causing the problem.

And yet, in LSWS, this doesn't seem to be a problem at all. So it _must_ be OLS that is having a problem here, surely? It should just acknowledge there's a cookie and no care about the value at all (unless told otherwise) as part of deciding if it's a cache hit or not, as LSWS appears to do.
 
Last edited:

LiteCache

Active Member
#90
This means that it can be determined with certainty that it is an OLS problem. I used your code and tested it with LSWS so there is no cache problem. However, you should do another test with a test.php.

Code:
<?php
header('x-litespeed-tag:test');
header('x-litespeed-cache-control: public, max-age=60');
header('cache-control:private,no-cache,max-age=0');

?>
<!DOCTYPE html>
<html>
    <head>
        <title>Test</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script>
            // Get the current date and time
            var currentDate = new Date();

            // Format the date as YYYY-MM-DD HH:MM:SS (you can adjust the format as needed)
            var formattedDate = currentDate.toISOString().slice(0, 19).replace("T", " ");

            // Set the value of the _arb cookie to the formatted date
            document.cookie = "_arb=" + formattedDate + "; SameSite=None; Secure";
            // Optional: You can also log a message to the console to confirm that the cookie is set
            console.log("Cookie _arb has been set to " + formattedDate);
        </script>
    </head>
    <body>
        <div>Test</div>
    </body>
</html>
 
#91
However, you should do another test with a test.php.
Good idea. have done so at https://wiki-test.westeros.org/test.php...

Huh. But this seems to cache, just like GA was caching. So nothing is wrong with the OLS base install, it's something to do with how LSCache is working specifically with how Mediawiki sets cookies that is different from how LSWS is working with Mediawiki. I will try to show the findings to bug report and maybe they will look more deeply at it to figure out the problem.
 
#98
I keep my fingers crossed for you. (y)
Just to update, after some back and forth, the issue has been passed to the cache plugin developer so hopefully there'll be a fix.

In the interim, I'm trying out LSWS and, just as you said, hits don't seem to be a problem.

Now to figure out why ab from a remote server is super slow while on the server itself I can get through tens of thousands of requests in seconds...
 

LiteCache

Active Member
#99
Just to update, after some back and forth, the issue has been passed to the cache plugin developer so hopefully there'll be a fix.
Why did it take almost 4 weeks?

In the interim, I'm trying out LSWS and, just as you said, hits don't seem to be a problem.
In view of the fact that the responsible developer of the Cache Plugin is a LiteSpeed freelancer, I recommend switching to LSWS. This does not solve the problem, but with LSWs you can make sure that LScache works for MediaWiki.

Nevertheless, the problem with OLS cannot be explained logically.
 
Top