statamic / v2-hub

Statamic 2 - Feature Requests and Bug Reports
https://statamic.com
95 stars 5 forks source link

FR: Optionally add domain to static caching path #2061

Open aryehraber opened 6 years ago

aryehraber commented 6 years ago

Is your feature request related to a problem? Please describe. Yes, static caching doesn't currently work in a subdomain or multidomain context (we use a custom addon to change out various content like contact details, etc).

It would be great if static caching had an option to nest rendered html into subfolders inside /static based on the current domain. This would allow static caching to work for more complex sites using subdomains.

Describe the solution you'd like I've managed to get this working by making the following minor changes:

In statamic/core/StaticCaching/FileCacher.php:

Add getCacheDomain():

/**
 * Get the current domain
 *
 * @return string
 */
public function getCacheDomain()
{
    return preg_split('/http(s)?:\/\//', url())[1];
}

Update getFilePath():

/**
 * Get the path to the cached file.
 *
 * @param $url
 * @return string
 */
public function getFilePath($url)
{
    $parts = parse_url($url);

    $path = sprintf('%s/%s/%s_%s.html',
        $this->getCachePath(),
        $this->getCacheDomain(),
        $parts['path'],
        array_get($parts, 'query', '')
    );

    return Path::makeFull($path);
}

Nginx config would also need a tiny adjustment:

location / {
  try_files /static/${host}${uri}_${args}.html $uri /index.php?$args;
}

(I'm not sure what the Apache and IIS changes would need to be)

ASochat commented 5 years ago

Is this feature request still considered? I'd love to use it!