I am using Drupal 9.0 -
And selected Advanced option: Run the page body through the AMP library

But it is showing AMP page validation error.

Actually, when we are log-in, it is showing link rel=canonical for AMP pages. But when we are not login, it is not showing link rel=canonical and because of that it is showing AMP validation error.

What is the solution?

Comments

rajesh190888 created an issue. See original summary.

rajesh190888’s picture

Title: link rel=canonical is missing while not login » in AMP, link rel=canonical is missing while not login
Priority: Major » Critical
nravens’s picture

I have the same issue, also Drupal 9

Have you found a solution?

pick_d’s picture

Not sure if it's AMP's or Metatag's issue.

Probably you have Metatag 8.x-1.15 installed. For me downgrading to Metatag 8.x-1.14 fixes the issue.
https://www.drupal.org/project/metatag/issues/3189863

nravens’s picture

Same issue and the same thing, rolling back to version 8.x-1.14 worked for me. Thank you.

rajesh190888’s picture

I am using version 8.x-1.14 only

pkmedi’s picture

Hmm - also facing this issue on 9.1.2 - downgrading was not sloving the issue.
The strange thing is i can see the canonical in incognito and logged in only on validating for Google amp, reports the error.

pkmedi’s picture

Not very common - but quick fix that did the trick... for anyone serves that issue...

Make a change in html.html.twig amptheme or sub and set it in directly in the theme:

after:

<head> -->
<link rel="canonical" href="{{ url('<current>') }}">  

hope its helps somebody.

pick_d’s picture

pkmedi

Proposed change works for me, thanks a lot.

nravens’s picture

Do your AMP pages still validate with #8?

For some strange reason, validation error says rel="canonical" is duplicated but when I check page source it's only there once when using the suggestion in #8

pick_d’s picture

I reverted the change because at some point I got duplicated rel="canonical" issue.

So the fix mentioned in #8 worked well when module failed to do so. But when module works just fine, no need to apply #8. At least that how things work in my case.

makkus183’s picture

I get the duplicated error @nravens mentioned in #10 in local dev environment. In Production Mode it is working fine when adding the link to the template as mentioned in #8

This behaviour remains the same regardless of being logged in or not

Edit: I was wrong, it is duplicated when being logged in as mentioned by others before

jesmaster’s picture

StatusFileSize
new494 bytes

I found the offending code and fixed the issue. If you've modified the template to force the line in then you can take it out.

Attached is the patch.

jesmaster’s picture

To explain the error:

In PHP 7 (0 == 'string') will always return true

so it was mistaking the 0 key (which is typically the canonical url) as the "big_pipe_detect_nojs" key and removing it.

In PHP 8 this was fixed so (0 == 'string') will return false as expected which would explain why this issue seemed to randomly disappear for some people as they most likely upgraded to PHP 8 which fixed the logic.

So the simple fix here was to change it to a === check to ensure the type matches as well as the value which will fix the check for PHP7 and have no effect for PHP8.

Reference: https://stackoverflow.com/a/6843075

jsobiecki’s picture

I can confirm that patch from https://www.drupal.org/project/amp/issues/3189756#comment-14138468 solved issue for me.

damontgomery’s picture

Thanks. I also had success with #13.

When we used `debug` in the URL, we got the canonical link, but when we didn't we did not get the link and the validation failed.

Thanks for the explanation for the issue. Maybe some people will be convinced to stop using `==`. :)

php > print 0 == 'hello' ? 'equals' : 'does not equal';
equals

php > print '0' == 'hello' ? 'equals' : 'does not equal';
does not equal

php > print 0 == '2 hello' ? 'equals' : 'does not equal';
does not equal

php > print 'hello' == 0 ? 'equals' : 'does not equal';
equals

php > print 0 == 'false' ? 'equals' : 'does not equal';
equals

php > print 0 == false ? 'equals' : 'does not equal';
equals

php > print 0 == 'true' ? 'equals' : 'does not equal';
equals

php > print 0 == true ? 'equals' : 'does not equal';
does not equal

What happens is that if a string starts with a number, it casts to a number, but if it doesn't it casts to 0. The boolean FALSE casts to 0 and TRUE to 1. Then the integers are compared.

I think for some reason this was to support something like:

php > print '2 cats' + '10 cats';
12
joshfeinstein’s picture

StatusFileSize
new498 bytes

The patch from June 2021 (#13) above fixes the AMP issue, but doesn't correctly address the big_pipe conditional. The correct value isn't $key, but $value[1].