Has anyone used Laravel Elixir's versioning method and figured out a way how to upload to a CDN like AWS's S3 and not get rev-manifest.json error's?
Quite rightly Elixir by default doesn't know I've thrown my content in a bucket on S3. So how can I tell it and now can I make that process automated? I notice @JeffreyWay hosts his JS and CSS on his server and some images are on S3. That's my current backup plan but I'd love to serve all versions JS and CSS from S3.
S3 is not a CDN. Use Cloudfront. With Cloudfront you point it at your server so when it doesn't have a file their servers will fetch it off of your server and then serve it to clients. Works perfectly with elixir.
@tomschlick how do you actually achieve that within a Laravel app? How do you reference your assets in your views so that Cloudfront knows where to pluck them from?
I added this function to my helpers.php file (create one in /resources/helpers.php if you don't already have one, reference it in the composer.json autoload section as a file.).
/**
* Get the path to a versioned Elixir file.
*
* @param string $file
*
* @return string
*/
function elixirCDN($file)
{
$cdn = '';
if(env('CDN_URL', false))
{
$cdn = env('CDN_URL');
}
return $cdn . elixir($file);
}
It works like a charm but the global.css file I am refereing gets loaded 2x times during page load. 1 time from the cdn as it should with the cloudfront url and a second time from the server itself (same file, same name, same content).
It seems that the CDN is redirecting to the origin with a 301 status code. Does the exlixr code put out a 301 during elixir versioning or so?
That happened to me originally, I believe it was an issue with the http to https redirection or the www to non-www redirection in the nginx config. Check there first.
Thank you @tomschlick - your answer pointed me into the right direction.
For future reference of others, here is what happens in this setup - I think it helps to understand the WHY of it.
In my case I had the CDN_URL set to HTTP://mysubdomain.cloudfront.net and at the same time used a http:// to https:// redirect in the .htaccess on an apache server (I suppose the same effect happens with www. and non-www. redirects)
there cloudfront will get a 301 redirect and CACHE this as the content of the requested resource
On any subsequent page AWS Cloudfront will deliver the 301 redirect as the resource and the resource itself will then be loaded from the server as if there was not CDN setup - quite confusing...
@EventFellows yup that was the problem I originally encountered. I couldn't remember what it was as it happened over a year ago but thats exactly it. You need to make sure that origin url is using https if your app is https.