XF 2.2 Serving Avatars via CDN

motowebmaster

Well-known member
I've been using a CDN again, but on a very selective (limited) basis. Currently just some of the static-image elements of my theme. I won't ever enable CDN for anything in a forum post.

However, I'd like to try serving my user-uploaded avatars via the CDN. Is there a particular template or style-property I should look for?
 
I guess, you already set up CDN file storing, right?

No, I don't intend to.

so today an avatar URL after my domain would be:

/data/avatars/s/11/11298.jpg?1616029888

I can manually change the domain URL in my browser to my CDN subdomain and that same avatar is pulled/displayed. I'm willing to change a few templates to accomplish this - versus changing a setting in config.php. I realize this isn't the easy to accomplish this, but after a few years of trial-and-error I believe it is better approach.

Are there templates that can be updated?
 
You don't find this in templates. src/XF/Template/Templater.php, see function fnAvatar.
Also you can modify src/XF/Entity/User.php, function getAvatarUrl.
 
It appears to be this particular code:

Code:
return $app->applyExternalDataUrl(
                "avatars/{$sizeCode}/{$group}/{$this->user_id}.jpg?{$this->avatar_date}",
                $canonical
            );

Not sure what to change on the return statement. Tried a few options without success.
 
Still working on it. I managed to find a custom-code option that worked, until I tested changing my avatar, my CDN wouldn't update. Reverted to the original.

It would be useful if a future XF version supported specifying an external data URL for avatars-only.
 
until I tested changing my avatar, my CDN wouldn't update. Reverted to the original.
What does that mean? XF uses a cache-buster query string on avatar URLs, so if you change your avatar the URL changes as well - a properly configured CDN should have no issue to catch this.

It would be useful if a future XF version supported specifying an external data URL for avatars-only.
PHP:
$config['externalDataUrl'] = function ($externalPath, $pathType) {
    if (preg_match('#^avatars/#', $externalPath))
    {
        return 'https://cdn.base.url/data/' . $externalPath;
    }

    return 'data/' . $externalPath;
};

in config.php
 
Last edited:
What does that mean? XF uses a cache-buster query string on avatar URLs, so if you change your avatar the URL changes as well - a properly configured CDN should have no issue to catch this.


PHP:
$config['externalDataUrl'] = function ($externalPath, $pathType) {
    if (preg_match('#^avatars/#', $externalPath))
    {
        return 'https://cdn.base.url/data/' . $externalPath;
    }

    return 'data/' . $externalPath;
};

in config.php

Thanks Kirby, This worked great!

Still had the same caching issue and removed this code for now, need to spend some time on my CDN config. Don't think I'm handing query strings properly.
 
Can you detail the different network path this change takes?

I'm failing to see the gain. Both output the same cdn.domain.com/bucket/file.jpg for me.

It is likely easier for most sites to use the easier approach and serve all external attachments via their preferred CDN.

I'm just weird, and didn't want user uploaded forum post attachments to be served from my CDN - only avatars.
 
Last edited:
Back
Top Bottom