New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Hreflang not correctly returned from GraphQL #1376
Comments
I'll have to set up a similar environment here to test, but as a sanity check, are all of your Craft CMS Sites set up with fully qualified domain name, and not a relative path or such? |
Sites don't have a dedicated domain, but they do have their domain set as follows: project.yaml config/general.php
.env
I would assume if the issue was the site config however, that the craft |
I just did a quick check -- it IS using a translated slug if used but NOT the suffix from the Base URL? Again, this is applied correctly to/for the canonical, but not the alternates? Here's an example (the NL links should be prefixed with /nl/) {
entries(site: ["nl", "en"], id: 53) {
url
uri
seomatic {
metaLinkContainer
}
}
} {
"data": {
"entries": [
{
"url": "http://localhost:3000/test-en",
"uri": "test-en",
"seomatic": {
"metaLinkContainer": "<link href=\"http://localhost:3000/test-en\" rel=\"canonical\">\n<link href=\"http://localhost:3000\" rel=\"home\">\n<link type=\"text/plain\" href=\"http://localhost:3000/humans.txt\" rel=\"author\">\n<link href=\"http://localhost:3000/test-nl\" rel=\"alternate\" hreflang=\"nl-nl\">\n<link href=\"http://localhost:3000/test-en\" rel=\"alternate\" hreflang=\"x-default\">\n<link href=\"http://localhost:3000/test-en\" rel=\"alternate\" hreflang=\"en-gb\">"
}
},
{
"url": "http://localhost:3000/nl/test-nl",
"uri": "test-nl",
"seomatic": {
"metaLinkContainer": "<link href=\"http://localhost:3000/nl/test-nl\" rel=\"canonical\">\n<link href=\"http://localhost:3000\" rel=\"home\">\n<link type=\"text/plain\" href=\"http://localhost:3000/humans.txt\" rel=\"author\">\n<link href=\"http://localhost:3000/test-nl\" rel=\"alternate\" hreflang=\"nl-nl\">\n<link href=\"http://localhost:3000/test-en\" rel=\"alternate\" hreflang=\"x-default\">\n<link href=\"http://localhost:3000/test-en\" rel=\"alternate\" hreflang=\"en-gb\">"
}
}
]
}
} |
So sorry for not getting back to you sooner on this... in my test environment, here's the query I'm doing: query MyQuery {
entries(site: ["default", "spanish"]) {
url
language
seomatic(asArray: false) {
metaLinkContainer
}
}
} ...and here's the raw result I'm seeing: {
"url": "http://localhost:8004/blog/my-first-blog",
"language": "en-US",
"seomatic": {
"metaLinkContainer": "<link href=\"http://localhost:8004/blog/my-first-blog\" rel=\"canonical\">\n<link href=\"http://localhost:8004\" rel=\"home\">\n<link type=\"text/plain\" href=\"http://localhost:8004/humans.txt\" rel=\"author\">\n<link href=\"http://localhost:8004/es/blog/my-first-blog\" rel=\"alternate\" hreflang=\"es\">\n<link href=\"http://localhost:8004/blog/my-first-blog\" rel=\"alternate\" hreflang=\"x-default\">\n<link href=\"http://localhost:8004/blog/my-first-blog\" rel=\"alternate\" hreflang=\"en-us\">"
}
},
{
"url": "http://localhost:8004/es/blog/my-first-blog",
"language": "es",
"seomatic": {
"metaLinkContainer": "<link href=\"http://localhost:8004/es/blog/my-first-blog\" rel=\"canonical\">\n<link href=\"http://localhost:8004/es\" rel=\"home\">\n<link type=\"text/plain\" href=\"http://localhost:8004/es/humans.txt\" rel=\"author\">\n<link href=\"http://localhost:8004/es/blog/my-first-blog\" rel=\"alternate\" hreflang=\"es\">\n<link href=\"http://localhost:8004/blog/my-first-blog\" rel=\"alternate\" hreflang=\"x-default\">\n<link href=\"http://localhost:8004/blog/my-first-blog\" rel=\"alternate\" hreflang=\"en-us\">"
} Teasing this out, I am seeing the correct prefixes for the URLs: english siteUrl: <link href="http://localhost:8004/blog/my-first-blog" rel="canonical">
<link href="http://localhost:8004/es/blog/my-first-blog" rel="alternate" hreflang="es">
<link href="http://localhost:8004/blog/my-first-blog" rel="alternate" hreflang="x-default">
<link href="http://localhost:8004/blog/my-first-blog" rel="alternate" hreflang="en-us">" spanish siteUrl: <link href="http://localhost:8004/es/blog/my-first-blog" rel="canonical">
<link href="http://localhost:8004/es/blog/my-first-blog" rel="alternate" hreflang="es">
<link href="http://localhost:8004/blog/my-first-blog" rel="alternate" hreflang="x-default">
<link href="http://localhost:8004/blog/my-first-blog" rel="alternate" hreflang="en-us">" In the case of the Spanish site, the URLs do have the correct language prefixes, so I'm thinking this must be something setup-related, because it's working as expected in my test environment here. |
Is it possible that your entries are not localized for the sites in question or such? I'm a little confused at how this could be happening at this point. |
Hi, not sure if this issue is related to mine, if not I will create a new issue. But I'm have a same kind of problem with a headless website end the hreflang tag. So for the record. I have a craft 4 multi lang setup with a headless frontend. But with this setting enabled all my hreflang tags for all my languages use this url from the Url Override setting. FYI: Correct frontend urls: Current situation with Site URL Override enabled and set to my frontend url:
What you expect:
With the Site Url Override setting disabled it works like it should, but with the backend url.. So that's no good. |
Oh, if you're using the Site URL Override setting, that only allows you to provide a single site URL to override, so I'm not surprised that it wouldn't work properly with multiple sites. The only solution I can think of for this specific type of setup would be either:
#1 would be a much preferred solution here though @jornwildenbeest |
@khalwat i understand that option 1 is the easiest solution. I do think that it is a better and permanent solution to improve the nystudio107\seomatic\helpers\UrlHelper::siteUrl function in a way that you can use it with the Site Url Override option, but also include the language handle etc. I think that is what you mean with option 2. |
Yeah I mean we're getting super-niche here, the issue you're running into only happens if:
I'll have to see how this could potentially be implemented without causing any existing sites to break when changing how the Site URL Override setting is implemented. |
@jornwildenbeest @FreekVR Added via: 3d30331 & 90ed2cd Please test this out, and ensure that it works as needed for your situation. Craft CMS 3: You can try it now by setting your server in your "nystudio107/craft-seomatic": "dev-develop as 3.4.68”, Then do a ….. Craft CMS 4: You can try it now by setting your semver in your "nystudio107/craft-seomatic": "dev-develop-v4 as 4.0.37”, Then do a |
Hi @khalwat, Thanks for the quick response! Looks promising, but with Craft 4.4.17 and php 8.1 I am getting the following error:
|
Hi! Thanks for the heads-up(s)
This was configured for our site too, but apparently not needed, as indeed the Craft vanilla site URLs are already correct for us. Removing this fixed the issue!
Just tried this one too but running into the same error unfortunately. |
@FreekVR Simple fix, sorry about that Addressed in: 5fbb544 & 1cdd96b Just update again, and you'll get the fix: Craft CMS 3: You can try it now by setting your semver in your "nystudio107/craft-seomatic": "dev-develop as 3.4.68”, Then do a ….. Craft CMS 4: You can try it now by setting your semver in your "nystudio107/craft-seomatic": "dev-develop-v4 as 4.0.37”, Then do a |
Excellent, thanks for the quick fix! I can confirm this is working! |
One more thing tho @khalwat I noticed that after updating the canonical tag for another site is looking like this:
Instead of what is was before
Downgrading back solves it. |
@jornwildenbeest Go to SEOmatic -> Content SEO for the section in question, and tell me what is in the Canonical URL setting there? |
Also, did you put fully qualified absolute (not relative) URLs in your |
Hmm, the SEOmatic -> Content SEO -> Canonical URL for the section is set to For some reason it indeed used relative urls in the Canonical URL for the primary site is working fine, but for other sites it now looks likes this:
Still a double '/fr' |
So maybe for your particular setup, you can remove the language prefix from the SEOmatic |
...or you can remove the |
Sorry, with the SEOmatic The problem occurs after setting the siteUrlOverride to the multi site array. Reverting it back to a string with the base url solves canonical problem, but then the hreflang is not working properly. Could it be that there is still a small bug in the new function causing this bug for the canonical url? |
in my case, removing the |
That's strange.. Section settings: With my SEOmatic settings from the project.yaml file in case it is helpful:
Edit: I've removed some of the siteUrlOverride sites here in the project.yaml settings copy, don't confuse that with the screenshot. the config/seomatic.php file only holds the siteUrlOverride setting. |
Did some debugging and found out that in my case, at one point it calls the src/helpers/UrlHelper.php -> siteUrl function with the following siteUrl and path parameters: Logged the
So with line $url = rtrim($siteUrl, '/') . '/' . ltrim($path, '/');` it combines these 2.. Not sure in which part of the plugin it is calling the siteUrl function, but maybe it is helpful. |
The |
Okay so I had an epiphany here, I think I know what is going on. I think much like you set the So let's say an entry's URI is Then SEOmatic notices that this is not a fully qualified, absolute URLs (which is required for the What you need to do is set your site URLs to be fully qualified absolute URLs, not relative URLs. |
Okay, think I figured it out. On line https://github.com/nystudio107/craft-seomatic/blob/develop-v4/src/services/MetaContainers.php#L328 it calls the siteUrl function with a Few lines before that it gets the parsed canonical url: https://github.com/nystudio107/craft-seomatic/blob/develop-v4/src/services/MetaContainers.php#L320 if the Content SEO -> {section} -> Canonical Url setting is set to Later in the UrlHelper class it separates the url in pieces: https://github.com/nystudio107/craft-seomatic/blob/develop-v4/src/helpers/UrlHelper.php#L48 And combines the uri (which also contains the BUT with the new function for siteUrlOverrides, the siteIUrl also contains an absolute url with So the solution in this case is: changing the Canonical Url setting from |
I'm mostly confused about what I did wrong in my test setup that I didn't catch this earlier. @jornwildenbeest I pushed changes that should account for overlapping path and URL segments. Can you switch it back to Craft CMS 3: You can try it now by setting your server in your "nystudio107/craft-seomatic": "dev-develop as 3.4.68”, Then do a ….. Craft CMS 4: You can try it now by setting your semver in your "nystudio107/craft-seomatic": "dev-develop-v4 as 4.0.37”, Then do a |
Happens to the best of us ;) Ah great, seems like everything is working as it should now. Thanks for the fixes! |
btw @jornwildenbeest directly inspired by this particular issue is the Craft Coding Challenge, in case you want to try your hand at writing your own method that combines URLs with overlapping segments: https://www.craftcodingchallenge.com/challenge-14-testing-santas-patience/ No fair cheating & looking at the SEOmatic code tho 😉 |
Haha cool! Thanks for letting me know, I might give it a shot! |
Describe the bug
We're seeing all the hreflangs being returned from GraphQL being equal to the english / unprefixed version of the pages, even though the canonical ánd url values are correct, even the hreflang pointing to the current language isn't correct.
To reproduce
Steps to reproduce the behaviour:
returns
Expected behaviour
The hreflang alternates match the URL or canonical of the given items' tranlations
Versions
The text was updated successfully, but these errors were encountered: