2
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-03-04 01:15:08 +08:00
discourse/spec/requests/crawler_hreflang_spec.rb
Natalie Tay ced7833cd0
FEATURE: Append locale to links in crawler view if present (#34466)
This applies the following when
`SiteSetting.content_localization_enabled &&
SiteSetting.content_localization_crawler_param` and in crawler view.
- note: `content_localization_crawler_param` is an experimental site
setting
- attaches the following in crawler view, depending on the values in
`SiteSetting.content_localization_supported_locales`, assuming en, ja,
ko.
([old](https://developers.google.com/search/blog/2013/04/x-default-hreflang-for-international-pages)
but likely relevant)
```
      <link rel="alternate" href="https://nat-1.demo-by-discourse.com/" hreflang="x-default">
      <link rel="alternate" href="https://nat-1.demo-by-discourse.com/?tl=en" hreflang="en">
      <link rel="alternate" href="https://nat-1.demo-by-discourse.com/?tl=ja" hreflang="ja">
      <link rel="alternate" href="https://nat-1.demo-by-discourse.com/?tl=ko" hreflang="ko">
```
- appends the `tl` param based on the presence of it in the incoming
request
- this method puts every response for crawler requests through Nokogiri
parse.. so we'll have to see how this goes in practice
  
 ### Video
 
In the following video you can see each page has the code chunk above in
`head`, and each navigated page has the appropriate appended URL param.


/t/160415
2025-08-22 08:46:33 +08:00

44 lines
1.5 KiB
Ruby

# frozen_string_literal: true
describe "Crawler hreflang tags" do
fab!(:user)
fab!(:post) { Fabricate(:post, user:) }
describe "when viewing a topic as crawler" do
before do
SiteSetting.content_localization_enabled = true
SiteSetting.content_localization_crawler_param = true
SiteSetting.content_localization_supported_locales = "en|ja|es"
end
it "includes hreflang tags when viewed as crawler" do
get "/t/#{post.topic.slug}/#{post.topic.id}",
headers: {
"User-Agent" => "Googlebot/2.1 (+http://www.google.com/bot.html)",
}
expect(response.body).to include('<link rel="alternate" href=')
expect(response.body).to include('hreflang="x-default"')
expect(response.body).to include('hreflang="en"')
expect(response.body).to include('hreflang="ja"')
expect(response.body).to include('hreflang="es"')
expect(response.body).to include("?#{Discourse::LOCALE_PARAM}=ja")
end
it "doesn't include hreflang tags for normal users" do
get "/t/#{post.topic.slug}/#{post.topic.id}"
expect(response.body).not_to include('hreflang="x-default"')
end
it "doesn't include hreflang tags when settings are disabled" do
SiteSetting.content_localization_enabled = false
get "/t/#{post.topic.slug}/#{post.topic.id}",
headers: {
"User-Agent" => "Googlebot/2.1 (+http://www.google.com/bot.html)",
}
expect(response.body).not_to include('hreflang="x-default"')
end
end
end