jumph4x / canonical-rails Goto Github PK
View Code? Open in Web Editor NEWQuick and configurable canonical tag to use in Rails views.
License: MIT License
Quick and configurable canonical tag to use in Rails views.
License: MIT License
My website supports multiple languages, for example:
https://www.example.com/mypage
https://www.example.com/en-US/mypage
https://www.example.com/fr-FR/mypage
https://www.example.com/mypage
and https://www.example.com/en-US/mypage
are actually the same page because en-US
is my default language.
What would be the best option to remove /en-US
from canonical URLs ? I can write a PR if necessary.
Gem version: 0.2.5
Ruby version: 2.6.3
Rails version: 6.0.0rc1
While attempting to upgrade an application from Rails 5.2 to 6.0.0rc1, I ran into this issue related to new autoload logic and an initializer in the canonical-rails engine.rb file:
/Users/mhwang/.rvm/gems/ruby-2.6.3/gems/railties-6.0.0.rc1/lib/rails/initializable.rb:32:in `instance_exec': DEPRECATION WARNING: Initialization autoloaded the constant CanonicalRails::TagHelper. (ActiveSupport::DeprecationException)
Being able to do this is deprecated. Autoloading during initialization is going
to be an error condition in future versions of Rails.
Reloading does not reboot the application, and therefore code executed during
initialization does not run again. So, if you reload CanonicalRails::TagHelper, for example,
the expected changes won't be reflected in that stale Module object.
This autoloaded constant has been unloaded.
Please, check the "Autoloading and Reloading Constants" guide for solutions.
(called from <top (required)> at /Users/mhwang/fuzzy/code/marketing/config/environment.rb:5)
The line of code that is the issue is in engine.rb:
module CanonicalRails
class Engine < ::Rails::Engine
initializer 'canonical_rails.add_helpers' do |app|
ActionView::Base.send :include, CanonicalRails::TagHelper
end
end
end
If I remove the initializer, the application loads fine. Without the initializer, the canonical_tag helper still works on each webpage.
This change is also backwards compatible to Rails 5.2. Is it worth considering removing this initializer for future compatibility if it still works without? Any suggestions would be appreciated!
Is there a reason the README suggests installing from GitHub, and not from rubygems?
In #70 the explicit dependency on "sprockets", '~> 3.0'
was relaxed, so the gem can be used with applications that are using sprockets 4.
However, the dummy application in the test suite is not compatible with sprockets 4. The following error message is generated:
An error occurred while loading ./spec/helpers/canonical_rails/tag_helper_spec.rb.
Failure/Error: Dummy::Application.initialize!
Sprockets::Railtie::ManifestNeededError:
Expected to find a manifest file in `app/assets/config/manifest.js`
But did not, please create this file and use it to link any assets that need
to be rendered by your app:
Example:
//= link_tree ../images
//= link_directory ../javascripts .js
//= link_directory ../stylesheets .css
and restart your server
For more information see: https://github.com/rails/sprockets/blob/070fc01947c111d35bb4c836e9bb71962a8e0595/UPGRADING.md#manifestjs
There's an obvious fix, which is to create the missing file. Creating an empty manifest.js
appears to work fine, and the test suite then runs and (mostly, see #86) passes.
However, I can see that #80 proposes removing the dummy application entirely, so I'm not sure which approach you would prefer.
Аfter migrating to rails 6 from 5.2, a port was added to the url. How do I hide it?
I have been using canonical for quite some time, but it just stopped working after migrating to devise 4.2.0. This is preparation for rails 5
It only happens in the the rspec.
<link href="http://mysiteno route matches {:action=>"application", :controller=>"layouts"}" rel="canonical" />
The code i have debugged is:
11: def path_without_html_extension
=> 12: request.path.sub(/\.html$/, '')
13: end
and here the request.path returns
"No route matches {:action=>\"application\", :controller=>\"layouts\"}"
Not sure where to go from here.
Is there any helper to detect if current url is canonical or not?
I'm adding amp(https://www.ampproject.org/docs/get_started/create/basic_markup) to the project. And it is better to have canonical tag with html extension.
Is it possible to add html extension to canonical tag?
Hello! By the issue title you're probably thinking that you should close this issue. Please don't!
I get this error when I run Spree's frontend build on Ruby 2.0.0:
1) Visiting Products should be able to display products priced under 10 dollars
Failure/Error: within(:css, '#sidebar_products_search') { click_button "Search" }
ActionView::Template::Error:
undefined method `encoding' for {"price_range_any"=>["Under $10.00"]}:ActiveSupport::HashWithIndifferentAccess
# /Users/ryanbigg/.rbenv/versions/2.0.0-p451/lib/ruby/2.0.0/cgi/util.rb:7:in `escape'
# /Users/ryanbigg/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/canonical-rails-0.0.6/app/helpers/canonical_rails/tag_helper.rb:36:in `block in whitelisted_query_string'
# /Users/ryanbigg/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/canonical-rails-0.0.6/app/helpers/canonical_rails/tag_helper.rb:36:in `each'
# /Users/ryanbigg/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/canonical-rails-0.0.6/app/helpers/canonical_rails/tag_helper.rb:36:in `map'
# /Users/ryanbigg/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/canonical-rails-0.0.6/app/helpers/canonical_rails/tag_helper.rb:36:in `whitelisted_query_string'
# /Users/ryanbigg/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/canonical-rails-0.0.6/app/helpers/canonical_rails/tag_helper.rb:21:in `canonical_href'
# /Users/ryanbigg/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/canonical-rails-0.0.6/app/helpers/canonical_rails/tag_helper.rb:25:in `canonical_tag'
# ./app/views/spree/shared/_head.html.erb:7:in `_1a267b5e5f91d9723f931f318739dddf'
I think it also happens on Ruby 1.9.
I am not familiar with your library, but I think it is to blame since it is at the root of this issue.
I have a few subdomain site set up using the gem Apartment, they all have similar content so I intend to add "alternate" tag.
<link rel="canonical" href='https://hk.lvh.me/en/programs/enroll?course_code=AT212121321' />
<link rel="alternate" hreflang="en-sg" href='https://sg.lvh.me/en/programs/enroll?course_code=AT212121321' />
<link rel="alternate" hreflang="en-tw" href='https://tw.lvh.me/en/programs/enroll?course_code=AT212121321' />
<link rel="alternate" hreflang="zh-tw" href='https://tw.lvh.me/zh/programs/enroll?course_code=AT212121321' />
Is there a way to support adding some whitelisted subdomains? Thanks :)
I don't know what the history is here, but it seems that travis-ci is no longer working for this gem. There's no CI results shown beside any commits, and PRs don't appear to have any CI checks run against them either.
It would be great to get CI working again, and I was wondering if you wanted to move to Github Actions? Or would you prefer to resurrect Travis, or something else?
Hey,
i'm not a SEO expert but i think this gem is not multi-language ready, right?
I mean, in a site with pages and url in different languages www.example.com/page
should not be the canonical of www.example.com/it/page
.
For Google this would mean to index only the base language version, and that's not what you want in this context.
While running the test suite with sprockets 4 (and adding the missing manifest file, see #85), I found that there are two failures with rack 3.
Sprockets < 4.2.0 (September 5 2023) will run without error, since they have a constraint to rack 2, but newer versions of sprockets support rack 3.
The errors are:
1) CanonicalRails::TagHelper w/ custom config with parameters should escape allowed params properly
Failure/Error: expect(helper.allowed_query_string).to eq '?page=5&keywords=%22here+be+dragons%22&search[super]=special'
expected: "?page=5&keywords=%22here+be+dragons%22&search[super]=special"
got: "?page=5&keywords=%22here+be+dragons%22&search%5Bsuper%5D=special"
(compared using ==)
# ./spec/helpers/canonical_rails/tag_helper_spec.rb:207:in `block (4 levels) in <top (required)>'
2) CanonicalRails::TagHelper w/ custom config with parameters should output allowed params using proper syntax (?key=value&key=value)
Failure/Error: expect(helper.canonical_tag).to eq '<link href="http://www.mywebstore.com/our_resources/?page=5&keywords=%22here+be+dragons%22&search[super]=special" rel="canonical" />'
expected: "<link href=\"http://www.mywebstore.com/our_resources/?page=5&keywords=%22here+be+dragons%22&search[super]=special\" rel=\"canonical\" />"
got: "<link href=\"http://www.mywebstore.com/our_resources/?page=5&keywords=%22here+be+dragons%22&search%5Bsuper%5D=special\" rel=\"canonical\" />"
(compared using ==)
# ./spec/helpers/canonical_rails/tag_helper_spec.rb:211:in `block (4 levels) in <top (required)>'
I haven't investigated what the root cause of these failures are, but they can be avoided by adding s.add_dependency "rack", '< 3'
to the gemspec.
Nice gem man! Thanks for doing that!
Can you improve the read-me file to something like this article?
http://blog.planetargon.com/entries/2014/4/4/easily-add-canonical-urls-to-your-rails-app
The current read-me is not so clear.
Thanks!
What are your thoughts on including a config option to force the protocol for the canonical URL href? For example, a site could have the same content hosted on http & https and wants to always have https be the canonical URL. With the current setup, the protocol is based on the incoming request protocol.
If you agree that is useful, I can work on a pull request.
Canonical url does not include port number on the host.
You cannot configure if you want to add a trailing slash or have it removed.
Currently I have my rails setup to not use trailing slash
application.rb
config.action_controller.default_url_options = { trailing_slash: false }
However the canonical URL has a trailing slash and this URL is redirect to no trailing slash.
Hi !
Not a big deal, but I think current documentation is a bit misleading and could cause problems for some developers. Canonical tag should be always put in the HTTP header or in the head tag of the HTML document. Current README suggest putting canonical_tag "into the views" which I think should be changed to "head section of your layout file".
Putting canonical tag into the body is listed as mistake number 5 at http://googlewebmastercentral.blogspot.ie/2013/04/5-common-mistakes-with-relcanonical.html
In app/views/layouts/application.html.erb
I have:
<link rel="canonical" href="https://www.my-domain.com" />
Would it be any different if I had, instead:
<%= canonical_tag -%>
?
My canonical_rails.rb
file:
config.collection_actions = [:index]
config.whitelisted_parameters = [:page, :room_id, :style]
For this url:
http://www.example.com/model?room_id=1&style=all&utf8=✓
canonical tag generates the following :
http://www.example.com/model/?room_id&style=all&page
while I expect something like this
http://www.example.com/model?room_id=1&style=all&page=1
Does this gem (or gem in combination with some other technique) facilitate replacing Rails' hardwired "underscore" paths and urls with the more SEO-friendly "hyphenated" paths and urls.
Specifically is there any way given a controller method def blue_socks ....
to have the auto-magic rails routes and paths and urls be in the form blue-socks
(which Google will understand as "blue socks" instead of blue_socks
(which to Google is just a non-word)?
Having installed this gem to play with it, it's nice on one hand to have an easy way to add a canonical to the layout, but the real SEO issue for Rails apps, it seems to me, is undoing Rails' (tragically IMO) baked-in "underscore" assumption for all routes, paths, urls.
Since Google explicitly states hyphenated words is better, surely there is some way to accomplish that with Rails?
Rails 7.0.1 has just been released, but because canonical-rails uses rails (>= 4.1, <= 7.0)
, bundler won't allow it.
Please change the dependency to rails (>= 4.1, < 7.1)
thx
My URL is https://www.neueve.com/content/6/S2/S7 however Canonical Rails is outputting the canonical version as https://www.neueve.com/content/6/s2/s7.
This is incorrect, and URLs are case sensitive.
The bug is the "downcase" on line 32 of tag_helper.rb:
raw "#{canonical_protocol}#{host}#{port}#{path_without_html_extension}#{trailing_slash_if_needed}#{whitelisted_query_string}".downcase
Please delete the .downcase thanks!
Rails 6.1 has now been released. Do you know when this gem will support it? Thanks!
I have an events resource, and created an alias route for 'e'. How can I make the canonical route the full resource name, events?
Rails.application.routes.draw do
resources :events
resources :e, controller: 'events'
end
Also, I'm not sure I understand the config.collection_actions = []
. What options are available to go into that array?
Is there a way to only allow a "page" parameter to be whitelisted for the :search controller?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.