buzzingpixel / craft-static

Static file caching for Craft 3
MIT License
15 stars 2 forks source link

Multi-site support #1

Open croxton opened 6 years ago

croxton commented 6 years ago

I have the code below working, but it would be preferable if edits only cleared the site currently selected in the CP. Since an editor can switch between sites regardless of the domain they logged into, we can't rely on the host name to determine which site cache to clear. Any plans to add multi-site support? Maybe a simple placeholder that gets replaced into the cache path?


Let's say you have 3 sites, admin.website.com, red-pill.website.com, blue-pill.website.com. Admin will only be used to access the control panel. The other two sites need to be static cached.

The CRAFT_SITE constant is defined in a .env file depending on the value of $_SERVER['HTTP_HOST'].

In the craft-static.php config file:

$cachePath = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'cache';

switch (CRAFT_SITE) {
    case 'redpill' : 
    case 'bluepill' : 
        $cachePath .= '/_sites/' . CRAFT_SITE;
        break;
}

return [
    'cachePath'     => $cachePath,
    'nixBasedClearCache'  => true,
];

When logged in to the admin site, the cache path is the parent of the two other sites. Thus any edits clear the cache for all sites.

Then to rewrite to the static cache files, bypassing PHP:

.htaccess:

# red-pill.website.com
RewriteCond %{HTTP_HOST} ^red-pill.website.com$

# Exclude image files
RewriteCond $1 !\.(gif|jpe?g|png|css|js|ico)$ [NC]

# We only want GET requests
RewriteCond %{REQUEST_METHOD} ^GET

# Exclude AJAX
RewriteCond %{HTTP:X-Requested-With} !=XMLHttpRequest

# Disable static caching for logged-in users
RewriteCond %{HTTP_COOKIE} !CraftSessionId [NC]

# Remove index.php from conditions
RewriteCond $1 ^(index.php/)*(.*)(/*)$

# Check if cached index.html exists
RewriteCond %{DOCUMENT_ROOT}/cache/_sites/redpill/$2/index.html (.*\.(.*))$
RewriteCond %1 -f

# Rewrite to the cached page
RewriteRule ^(index.php/*)*(.*)(/*) /cache/_sites/redpill/$2/index.%2 [L]

# Repeat for blue-pill.website.com ...
tjdraper commented 6 years ago

Sorry, I'm not trying to ignore this, I'm just really swamped. I imagine the next time I work on this will be when I do another Craft 3 site. I'm not sure when that will be.

Since Craft 3 has multi-site support, it does seem like a good idea for this plugin to have it too.

croxton commented 6 years ago

No worries. I'm not sure I will actually need it yet, but if so I may fork and submit a PR, if that's OK?

tjdraper commented 6 years ago

You can certainly submit a PR. The worst I can do is reject it. I like to keep things super simple and I wonder if possibly even splitting up the functionality into another add-on might be the ticket. I recently built an add-on for EE that only clears static cache. https://github.com/buzzingpixel/static-cache-clear-ee

thisisablock commented 5 years ago

addded an PR for that https://github.com/buzzingpixel/craft-static/pull/6