craft-seomatic icon indicating copy to clipboard operation
craft-seomatic copied to clipboard

[FR] Headless Social Media Preview via CP host

Open d--j opened this issue 2 years ago • 3 comments

Is your feature request related to a problem? Please describe.

If CraftCMS&SEOMatic is used in a headless context, the Social Media Preview URLs that SEOMatic generates are not working. They point to the site but the site does not have CraftCMS installed (it's headless after all) and thus they generate 404s.

Describe the solution you would like

Add a configuration option to generate the Social Media Preview URLs with the CP host info. Or – alternatively – always use the CP host URL.

Describe alternatives you have considered

We currently use this hotfix to patch the generated URLs:

// patch SEOMatic preview target to always use the CP url
Event::on(Plugins::class, Plugins::EVENT_AFTER_LOAD_PLUGINS, static function () {
    Event::on(
        Entry::class,
        Element::EVENT_REGISTER_PREVIEW_TARGETS,
        static function (RegisterPreviewTargetsEvent $e) {
            foreach ($e->previewTargets as $i => $target) {
                if (
                    is_array($target) &&
                    array_key_exists('url', $target) &&
                    str_contains($target['url'], 'seomatic/preview-social-media')
                ) {
                    $siteHostInfo                 = UrlHelper::hostInfo($target['url']);
                    $e->previewTargets[$i]['url'] = UrlHelper::cpHost().substr($target['url'], strlen($siteHostInfo));
                }
            }
        }
    );
});

d--j avatar Jun 26 '22 15:06 d--j

Perhaps if headlessMode is enabled, we can use the CP URL?

khalwat avatar Jun 26 '22 15:06 khalwat

Yes, that's a good idea.

d--j avatar Jun 26 '22 16:06 d--j

This is related to #950

d--j avatar Jul 11 '22 21:07 d--j

You can now use the Site URL Override feature to get this to work.

Addressed in: https://github.com/nystudio107/craft-seomatic/commit/61f941efb3ffc81afc0c940e663b1ec63fc625f7 & https://github.com/nystudio107/craft-seomatic/commit/3ec9a31184b4d0abf65ca6d5d57717318e60a9e7

Craft CMS 3:

You can try it now by setting your semver in your composer.json to look like this:

    "nystudio107/craft-seomatic": "dev-develop as 3.4.67”,

Then do a composer clear-cache && composer update

…..

Craft CMS 4:

You can try it now by setting your semver in your composer.json to look like this:

    "nystudio107/craft-seomatic": "dev-develop-v4 as 4.0.36”,

Then do a composer clear-cache && composer update

khalwat avatar Nov 28 '23 14:11 khalwat