Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multi-site support #1

Open
croxton opened this issue Jan 23, 2018 · 4 comments
Open

Multi-site support #1

croxton opened this issue Jan 23, 2018 · 4 comments

Comments

@croxton
Copy link

croxton commented Jan 23, 2018

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
Copy link
Contributor

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
Copy link
Author

croxton commented Jan 30, 2018

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
Copy link
Contributor

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
Copy link

addded an PR for that
#6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants