Skip to content

Commit efedf45

Browse files
authored
Merge pull request #361 from kpumuk/pr-224-robots-rebased
Support custom robots directives
2 parents 26fd17d + d7c56ef commit efedf45

File tree

6 files changed

+90
-1
lines changed

6 files changed

+90
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Unreleased
44

55
- Added `truncate_array_items_at_boundaries` configuration option to preserve whole items in multi-item `title` and `keywords` arrays when truncating ([354](https://github.com/kpumuk/meta-tags/pull/354)).
6+
- Added support for custom robots directives via `robots`, `googlebot`, and `bingbot` hashes ([224](https://github.com/kpumuk/meta-tags/pull/224), rebased in [361](https://github.com/kpumuk/meta-tags/pull/361)).
67

78
## 2.23.0 (March 16, 2026) [](https://github.com/kpumuk/meta-tags/compare/v2.22.3...v2.23.0)
89

README.md

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ If you want to set the title and display another text, use this:
154154

155155
### Allowed options for `display_meta_tags` and `set_meta_tags` methods
156156

157-
Use these options to customize the title format:
157+
Use these options to customize the generated tags:
158158

159159
| Option | Description |
160160
| -------------- | ------------------------------------------------------------------------------------------------------------------- |
@@ -173,6 +173,9 @@ Use these options to customize the title format:
173173
| `:nofollow` | Add nofollow meta tag; when true, "robots" will be used; accepts a string with a robot name or an array of strings |
174174
| `:follow` | Add follow meta tag; when true, "robots" will be used; accepts a string with a robot name or an array of strings |
175175
| `:noarchive` | Add noarchive meta tag; when true, "robots" will be used; accepts a string with a robot name or an array of strings |
176+
| `:robots` | Add custom directives to the `robots` meta tag (Hash) |
177+
| `:googlebot` | Add custom directives to the `googlebot` meta tag (Hash) |
178+
| `:bingbot` | Add custom directives to the `bingbot` meta tag (Hash) |
176179
| `:canonical` | Add canonical link tag |
177180
| `:prev` | Add legacy prev pagination link tag |
178181
| `:next` | Add legacy next pagination link tag |
@@ -410,6 +413,37 @@ set_meta_tags noindex: true, follow: true
410413

411414
This tag will prevent search engines from indexing this specific page, but it will still allow them to crawl and index the remaining pages on your website.
412415

416+
### Robots
417+
418+
Use the `robots`, `googlebot`, and `bingbot` hashes when you need directives beyond `index`, `noindex`, `follow`, `nofollow`, and `noarchive`.
419+
420+
This is useful for directives such as `max-snippet`, `max-video-preview`, and `unavailable_after`.
421+
422+
```ruby
423+
set_meta_tags robots: {
424+
"max-snippet" => -1,
425+
"max-video-preview" => -1
426+
}
427+
# <meta name="robots" content="max-snippet:-1, max-video-preview:-1">
428+
429+
set_meta_tags googlebot: {
430+
unavailable_after: "2026-12-31"
431+
}
432+
# <meta name="googlebot" content="unavailable_after:2026-12-31">
433+
434+
set_meta_tags bingbot: {
435+
"max-image-preview" => "large"
436+
}
437+
# <meta name="bingbot" content="max-image-preview:large">
438+
```
439+
440+
These hashes are merged with the existing robots helpers, so `noindex`, `nofollow`, and similar directives still render first for the same meta tag.
441+
442+
Further reading:
443+
444+
- [Google Search Central: robots meta tag and X-Robots-Tag](https://developers.google.com/search/docs/crawling-indexing/robots-meta-tag)
445+
- [Bing Webmaster Guidelines: robots meta tag support](https://www.bing.com/webmasters/help/which-robots-metatags-does-bing-support-5198d240)
446+
413447
### Canonical URL
414448

415449
Canonical link elements help search engines consolidate duplicate or near-duplicate URLs under one preferred URL. They are a signal, not a guarantee.

lib/meta_tags/meta_tags_collection.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,13 @@ def extract_robots
154154
calculate_robots_attributes(result, attributes)
155155
end
156156

157+
[:robots, :googlebot, :bingbot].each do |bot|
158+
values = extract(bot).presence
159+
if values
160+
result[bot.to_s].concat(values.map { |k, v| "#{k}:#{v}" })
161+
end
162+
end
163+
157164
result.transform_values { |v| v.join(", ") }
158165
end
159166

lib/meta_tags/view_helper.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,9 @@ def refresh(refresh)
151151
# @option default [String, Integer] :refresh (nil) meta refresh tag;
152152
# @option default [Hash] :open_graph ({}) add Open Graph meta tags.
153153
# @option default [Hash] :open_search ({}) add Open Search link tag.
154+
# @option default [Hash] :robots ({}) add robots meta tags.
155+
# @option default [Hash] :googlebot ({}) add googlebot meta tags.
156+
# @option default [Hash] :bingbot ({}) add bingbot meta tags.
154157
# @return [String] HTML meta tags to render in HEAD section of the
155158
# HTML document.
156159
# @example Render meta tags in a layout

sig/lib/meta_tags/view_helper.rbs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module MetaTags
44

55
type stringish = String | (_Stringish & Object)
66
type robots_value = bool | String | Array[String]
7+
type meta_tags = Hash[Renderer::meta_key, Renderer::meta_value]
78

89
def meta_tags: () -> MetaTagsCollection
910

spec/view_helper/robots_spec.rb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# frozen_string_literal: true
2+
3+
require "spec_helper"
4+
5+
RSpec.describe MetaTags::ViewHelper, "displaying robots meta tags" do
6+
it "displays meta tags specified with :robots" do
7+
subject.display_meta_tags(robots: {"max-snippet": -1}).tap do |meta|
8+
expect(meta).to have_tag("meta", with: {content: "max-snippet:-1", name: "robots"})
9+
end
10+
end
11+
12+
it "displays meta tags specified with :googlebot" do
13+
subject.display_meta_tags(googlebot: {unavailable_after: "2020-09-21"}).tap do |meta|
14+
expect(meta).to have_tag("meta", with: {content: "unavailable_after:2020-09-21", name: "googlebot"})
15+
end
16+
end
17+
18+
it "displays meta tags specified with :bingbot" do
19+
subject.display_meta_tags(bingbot: {"max-image-preview": "large"}).tap do |meta|
20+
expect(meta).to have_tag("meta", with: {content: "max-image-preview:large", name: "bingbot"})
21+
end
22+
end
23+
24+
it "displays multiple custom robots tags in a hash" do
25+
subject.display_meta_tags(robots: {"max-snippet": -1, "max-video-preview": -1}).tap do |meta|
26+
expect(meta).to have_tag("meta", with: {content: "max-snippet:-1, max-video-preview:-1", name: "robots"})
27+
end
28+
end
29+
30+
it "displays custom robots tags along with noindex" do
31+
subject.noindex(true)
32+
subject.display_meta_tags(robots: {"max-snippet": -1, "max-video-preview": -1}).tap do |meta|
33+
expect(meta).to have_tag("meta", with: {content: "noindex, max-snippet:-1, max-video-preview:-1", name: "robots"})
34+
end
35+
end
36+
37+
it "merges bot-specific directives with helper-based robots directives" do
38+
subject.set_meta_tags(noindex: "googlebot", googlebot: {unavailable_after: "2026-12-31"})
39+
subject.display_meta_tags.tap do |meta|
40+
expect(meta).to have_tag("meta", with: {content: "noindex, unavailable_after:2026-12-31", name: "googlebot"})
41+
end
42+
end
43+
end

0 commit comments

Comments
 (0)