Skip to content
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

Solve SEF canonical issues #9565

Merged
merged 9 commits into from Mar 26, 2016
Merged

Conversation

andrepereiradasilva
Copy link
Contributor

This Pull Request is related to issues #9333 and #9556.

Also to PR #9559

Summary of Changes

Add a better check for adding the canonical html tag.

Testing Instructions

  1. Apply this patch. With SEF system plugin active
  2. Do the following test scenarios:
Domain in the SEF plugin field Canonical added by a component Result
(empty) No The canonical html tag will NOT be added
(empty) Yes No change (the component added canonical html tag stays the same)
Yes No The canonical html tag will be added with the new domain typed in the SEF plugin domain field (ex: https://www.example.com/menu-item/).
Yes Yes The canonical html tag will be the same as added by the component but now with the new domain typed in the SEF plugin domain field.

@andrepereiradasilva
Copy link
Contributor Author

@scence @SharkyKZ @infograf768 @ggppdk @chivitli

Can you test this and check if all issues are resolved?

@andrepereiradasilva
Copy link
Contributor Author

@wilsonge if tested ok, IMHO should go to 3.5.1

{
$doc = $this->app->getDocument();
$doc = JFactory::getDocument();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any reason this can't be $this->app->getDocument() still?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You get a fatal error if you try to access it like that in onAfterRoute event

Sorry, clarification, you get a fatal error after, because it returns null

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes ... try it and you'll see ... php errors.

it seems if you use the $this->app->getDocument(); in onAfterRoute event you get problems. Don't know why. Maybe @mbabker knows better why this happens.

BTW that has the whole reason i change it to onAfterDispatch earlier. After component rendering you can use it.

@SharkyKZ
Copy link
Contributor

If canonical is added only when domain is set, there is no need to revert other changes since in this case SEF plugin should override components anyway. Otherwise components would not use the canonical domain.

In such a case, canonical should simply be the specified domain + current path.

@SharkyKZ
Copy link
Contributor

By my logic, it would be best to check for existing canonicals before doing anything.

  1. If canonical is found and canonical domain is set, replace current domain with canonical domain.
  2. If canonical is found and canonical domain is not set, leave it as is.
  3. If canonical is not found and canonical domain is set, add a canonical.
  4. If canonical is not found and canonical domain is not set, leave it as is or add a possibly incorrect canonical.

@andrepereiradasilva
Copy link
Contributor Author

ok so let me try to code something for that.
don't test yet.

@andrepereiradasilva
Copy link
Contributor Author

ok i changed the code.

How it works now:

  • Back to using onAfterDispatch.
  • If a canonical EXISTS in the document when the onAfterDispatch is run, the plugin doesn't add a canonical.
    This should solve issues with components and duplicate canonicals.
  • If a canonical DOESN'T EXIST in the document when the onAfterDispatch is run and a domain is ADDED in the SEF plugin domain field, it adds the canonical with that domain.
  • If a canonical DOESN'T EXIST in the document when the onAfterDispatch is run and a domain is NOT ADDED in the SEF plugin domain field, it only adds the canonical if the canonical URI is different than the current uri.

Please check code and test.

Open to any suggestions/improvements regarding this.

@ggppdk
Copy link
Contributor

ggppdk commented Mar 24, 2016

Nice and simple to allow components to add rel canonical without reverse engineering anything

  • will test this

@SharkyKZ
Copy link
Contributor

It doesn't override component canonicals when canonical domain is set.
Can add this: foreach ($doc->_links as $canonical => $link) to get existing canonical link as $canonical. Then use it with JUri::getInstance() to get only the path. Then prepend the canonical $domain to it.
EDIT: in this case the original canonical also needs to be unset unset($doc->_links[$canonical]) since we're adding a modified.

@chivitli
Copy link
Contributor

For me this is good, I would never override canonical from a custom component. The rest works as described. So, test ok from me.

It still remains an issue though that canonicals generated this way are not real canonicals, so I am not sure if this is useful at all (or that it doesn't have a negative impact).

@chivitli
Copy link
Contributor

I just realized you would like the plugin to replace the domain part of the custom component - that sounds like a good idea. At present, this is indeed not the case.

@andrepereiradasilva
Copy link
Contributor Author

It doesn't override component canonicals when canonical domain is set.
Can add this: foreach ($doc->_links as $canonical => $link) to get existing canonical link as $canonical. Then use it with JUri::getInstance() to get only the path. Then prepend the canonical $domain to it.
EDIT: in this case the original canonical also needs to be unset unset($doc->_links[$canonical]) since we're adding a modified.

I just realized you would like the plugin to replace the domain part of the custom component - that sounds like a good idea. At present, this is indeed not the case.

I will add this them.

@andrepereiradasilva
Copy link
Contributor Author

please check now.

@chivitli
Copy link
Contributor

Test successful, works as described.

@andrepereiradasilva
Copy link
Contributor Author

Test instructions updated.

@chivitli if it's ok for you. please mark as "Tested successfully" in "Joomla! Issue Tracker" https://issues.joomla.org/tracker/joomla-cms/9565 (after login the "Test this" button appears).
For more information see https://docs.joomla.org/Testing_Joomla!_patches#Recording_test_results

@chivitli
Copy link
Contributor

I have tested this item ✅ successfully on d00b99c


This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/9565.

@SharkyKZ
Copy link
Contributor

I have tested this item ✅ successfully on d00b99c

Works as expected.


This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/9565.

@infograf768
Copy link
Member

Sorry folks, but I am totally lost...
I am using basic staging.
After this patch, eventhough I have left the Domain field empty in the SEF plugin, I now get a canonical.

 <base href="http://localhost:8888/trunkgitnew/contact-component.html" />
  <meta name="description" content="UNE DESCRIPTION" />
  <meta name="generator" content="Joomla! - Open Source Content Management" />
  <title>Contact Component - My very long test site name</title>
  <link href="http://localhost:8888/trunkgitnew/contact-component.html" rel="canonical" />
  <link href="/trunkgitnew/templates/protostar/favicon.ico" rel="shortcut icon" type="image/vnd.microsoft.icon" />
[...]

As far as I know, a canonical link is only used to show Search Engine the preferred url when multiple URLs reach the same page
A canonical link element is an HTML element that helps webmasters prevent duplicate content issues by specifying the "canonical" or "preferred" version of a web page[1][2] as part of search engine optimization. It is described in RFC 6596, which went live in April 2012.

Why then create a canonical when only one URL exists?

@infograf768
Copy link
Member

I do now understand though that we get also a canonical before this patch when loading another page than the home as I have indeed overseen that in my former tests...
I just don't understand the use of it in that case.

@chivitli
Copy link
Contributor

There are always multiple urls in Joomla. Not only multiple, but an infinite number of them... As mentioned, Google says: Mark up the canonical page and any other variants with a rel="canonical" link element.

There is no need for this optimization to not show canonical on the canonical page.

@infograf768
Copy link
Member

Are'nt we going to get a canonical for ANY page loaded this way. I mean when we have another url to get to the same page, we would have a different canonical.

@ggppdk
Copy link
Contributor

ggppdk commented Mar 25, 2016

Are'nt we going to get a canonical for ANY page loaded this way. I mean when we have another url to get to the same page, we would have a different canonical.

Ok the plugin, previously had code:

  • not to add rel canonical if canonical URL is same as current URL

Now it WILL happen that it will add 2 (different) rel canonical for 2 pages that are same

But

  • the effect will be the same as before, goggle will consider the 2 pages different
    because if you do not add rel canonical for 2 urls then the rel canonical is supposed to be the URL itself !

@ggppdk
Copy link
Contributor

ggppdk commented Mar 25, 2016

I don't know google having special code to try to detect if 2 pages are the same if rel canonical is missing, furthermore which 2 pages would it compare and why it try to compare them ?

  • anyway the code to check if current URL is same as calculated canonical is not directly relevant to this PR, so it can be re-added

@joomla-cms-bot
Copy link

This PR has received new commits.

CC: @chivitli, @SharkyKZ


This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/9565.

@andrepereiradasilva
Copy link
Contributor Author

ok. updated the code, now it ONLY adds (or changes in the case a component already added) the canonical if the SEF plugin domain field has some value.
Also added a placeholder (hint) to the domain field.

Test instructions updated in the first post, please test now.

@joomla-cms-bot
Copy link

This PR has received new commits.

CC: @chivitli, @SharkyKZ


This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/9565.

@SharkyKZ
Copy link
Contributor

Since canonical URL is now used only to canonize domains, it can be left as current path with canonical domain prepended. Routed URL is incorrect anyways, so no point in leaving it there.

@andrepereiradasilva
Copy link
Contributor Author

right, you're right will change it

@joomla-cms-bot
Copy link

This PR has received new commits.

CC: @chivitli, @SharkyKZ


This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/9565.

@andrepereiradasilva
Copy link
Contributor Author

done please retest and mark the test result again.

@SharkyKZ
Copy link
Contributor

I have tested this item ✅ successfully on 86f63a6


This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/9565.

1 similar comment
@chivitli
Copy link
Contributor

I have tested this item ✅ successfully on 86f63a6


This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/9565.

@andrepereiradasilva
Copy link
Contributor Author

@infograf768 ok for you now too?

@@ -22,6 +22,7 @@
<field name="domain" type="url"
description="PLG_SEF_DOMAIN_DESCRIPTION"
label="PLG_SEF_DOMAIN_LABEL"
hint="https://www.domain.tld"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is an example link can you make sure it uses example.com please :) thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@joomla-cms-bot
Copy link

This PR has received new commits.

CC: @chivitli, @SharkyKZ


This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/9565.

@andrepereiradasilva
Copy link
Contributor Author

Just a little change at @wilsonge request, no need to retest.

@infograf768
Copy link
Member

I have tested this item ✅ successfully on 99b82a7


This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/9565.

@infograf768
Copy link
Member

It is now behaving as should. Thanks!

RTC

@wilsonge

You can merge into 3.5.1


This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/9565.

@joomla-cms-bot joomla-cms-bot added the RTC This Pull Request is Ready To Commit label Mar 26, 2016
@wilsonge wilsonge merged commit d8a127f into joomla:staging Mar 26, 2016
@joomla-cms-bot joomla-cms-bot removed the RTC This Pull Request is Ready To Commit label Mar 26, 2016
@wilsonge
Copy link
Contributor

Merged - thanks :)

@andrepereiradasilva andrepereiradasilva deleted the patch-6 branch March 26, 2016 15:12
@wilsonge wilsonge added this to the Joomla 3.5.1 milestone Mar 26, 2016
@andrepereiradasilva
Copy link
Contributor Author

@SharkyKZ @chivitli @infograf768 @ggppdk @wilsonge thanks for tests and/or contributions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants