NickD
NickD

Reputation: 191

Facebook share preview caches og tags even when the query parameters are changed

I'm using php to to have dynamic og:image based on url parameter, however, even when the query string are different, facebook still cache the default image that I set

<?php 
        $selection = $_GET['selection'];
        $mixName = $_GET['mixName'];
        
        $title = "App title";
        $description = "Default description";
        $image = "default image url";

        if ($selection) {
            $dynamicContent = file_get_contents('calling server to get custom image based on query parameters $selection);
            $data = json_decode($dynamicContent, TRUE);
            if ($data['image']) {
                $image = $data['image'];
            } 

            if ($mixName) {
                $title = $mixName;
            }
        }
    ?>

    <meta name="description" content="description">
    <meta property="og:title" content="<?php echo $title ?>">
    <meta property="og:url" content="website url">
    <meta property="og:description" content="testabc">
    <meta property="og:type" content="website" />
    <meta property="og:image" content="<?php echo $image ?>">

    <meta name="twitter:card" content="summary_large_image">
    <meta name="twitter:title" content="<?php echo $title ?>">
    <meta name="twitter:image" content="<?php echo $image ?>">
    <meta name="twitter:description" content="<?php echo $description ?>">

I also made the test to check http vs https websites and I noticed that my PHP script works fine when on a HTTP (unsecured) environnement, I always get the proper image inside the share preview.

I also the facebook share debugger to manually delete the cache from my website but is still not rendering the custom image.

I know the custom image is fine because when I actually click on the link and go on the page -> inspect and the og:image tags is actually link to my dynamic image URL.

I've read on many post that changing query parameters shouldn't use cache, but it doesn't work for me.

The request method done by php to get the dynamic image takes an average between 2-3 seconds to resolve. Does Facebook share as a max limit where it would use an older cache if it has not been resolved yet ?

Thanks for any information

Upvotes: 0

Views: 459

Answers (1)

C3roe
C3roe

Reputation: 96413

You need to specify the query string parameters in the og:url as well.

Facebook takes that to be the “real” URL of the piece of content you are sharing, and will request the rest of the OG meta data from there.

Be aware that specifying different URLs will also split your like and share counts between those URLs, even if you think of them as basically the same piece of content. To Facebook, different OG URLs, mean different Open Graph Objects.

Upvotes: 1

Related Questions