Skip to content

Commit 669ba9a

Browse files
committed
Add canonical link tag to guides head
The canonical link tells search engines what URL is preferred for the current document. By point to the latest released version, it makes sure search engines prefer the latest versions in search results. Guides that have been removed will have a canonical URL that will result in a 404. These will mostyl be ignored by search engines, but we can also not show the canonical url for guides that will be deleted in the next version.
1 parent 042e392 commit 669ba9a

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

guides/rails_guides/generator.rb

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -186,13 +186,14 @@ def generate_guide(guide, output_file)
186186

187187
view = ActionView::Base.with_empty_template_cache.with_view_paths(
188188
[@source_dir],
189-
edge: @edge,
190-
version: @version,
191-
epub: "epub/#{epub_filename}",
192-
language: @language,
193-
direction: @direction,
194-
uuid: SecureRandom.uuid,
195-
digest_paths: @digest_paths
189+
edge: @edge,
190+
version: @version,
191+
epub: "epub/#{epub_filename}",
192+
language: @language,
193+
direction: @direction,
194+
uuid: SecureRandom.uuid,
195+
digest_paths: @digest_paths,
196+
canonical_url: canonical_url(output_file)
196197
)
197198
view.extend(Helpers)
198199

@@ -223,6 +224,12 @@ def generate_guide(guide, output_file)
223224
end if !dry_run?
224225
end
225226

227+
def canonical_url(path)
228+
url = "https://guides.rubyonrails.org/"
229+
url += path unless path == "index.html"
230+
url
231+
end
232+
226233
def warn_about_broken_links(html)
227234
anchors = extract_anchors(html)
228235
check_fragment_identifiers(html, anchors)

guides/source/layout.html.erb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
<link rel="apple-touch-icon" href="images/icon.png">
1515

16+
<link rel="canonical" href="<%= @canonical %>">
17+
1618
<script src="<%= digest_path('javascripts/@hotwired--turbo.js') %>" data-turbo-track="reload"></script>
1719
<script src="<%= digest_path('javascripts/clipboard.js') %>" data-turbo-track="reload"></script>
1820
<script src="<%= digest_path('javascripts/guides.js') %>" data-turbo-track="reload"></script>

0 commit comments

Comments
 (0)