Add meta tags to Collection HTML (#39357)

This commit is contained in:
David Roetzel 2026-06-10 15:28:52 +02:00 committed by GitHub
parent bcb8553e01
commit 70bb70c2e6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 55 additions and 38 deletions

View File

@ -18,7 +18,6 @@ class CollectionsController < ApplicationController
respond_to do |format|
format.html do
expires_in expiration_duration, public: true unless user_signed_in?
render template: 'home/index'
end
format.json do

View File

@ -0,0 +1,20 @@
- content_for :page_title, @collection.name
- content_for :header_tags do
%meta{ name: 'robots', content: 'noindex, noarchive' }/
%link{ rel: 'alternate', type: 'application/activity+json', href: ap_account_collection_url(@collection.account_id, @collection) }/
%link{ rel: 'alternate', type: 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"', href: ap_account_collection_url(@collection.account_id, @collection) }/
- unless @collection.sensitive?
%meta{ name: 'description', content: @collection.description }/
= opengraph 'og:description', @collection.description
= opengraph 'og:site_name', site_title
= opengraph 'og:type', 'website'
= opengraph 'og:title', @collection.name
= opengraph 'og:url', collection_url(@collection)
- if @collection.language.present?
= opengraph 'og:locale', @collection.language
= opengraph 'twitter:card', 'summary'
= render 'shared/web_app'

View File

@ -92,6 +92,7 @@ RSpec.configure do |config|
config.include CommandLineHelpers, type: :cli
config.include SystemHelpers, type: :system
config.include Shoulda::Matchers::ActiveModel, type: :validator
config.include HtmlHeadInspection, type: :request
# TODO: Remove when Devise fixes https://github.com/heartcombo/devise/issues/5705
config.before do

View File

@ -16,18 +16,4 @@ RSpec.describe 'The account show page' do
expect(head_meta_content('og:image')).to match '.+'
expect(head_meta_content('og:url')).to eq short_account_url(username: alice.username)
end
def head_link_icons
response
.parsed_body
.search('html head link[rel=icon]')
end
def head_meta_content(property)
response
.parsed_body
.search("html head meta[property='#{property}']")
.attr('content')
.text
end
end

View File

@ -6,12 +6,20 @@ RSpec.describe 'Collections' do
describe 'GET /collections/:id' do
subject { get collection_path(collection) }
let(:collection) { Fabricate(:collection) }
let(:collection) do
Fabricate(:collection, name: 'Frequent posters', description: 'Amazing people that can quickly fill your timeline', language: 'en')
end
it 'returns success' do
it 'returns success and includes opengraph meta tags' do
subject
expect(response).to have_http_status(200)
expect(head_meta_content('og:title')).to eq 'Frequent posters'
expect(head_meta_content('og:description')).to eq 'Amazing people that can quickly fill your timeline'
expect(head_meta_content('og:type')).to eq 'website'
expect(head_meta_content('og:url')).to eq collection_url(collection)
expect(head_meta_content('og:locale')).to eq 'en'
end
end

View File

@ -41,26 +41,5 @@ RSpec.describe 'Statuses' do
expect(head_meta_content('og:locale')).to eq 'ca'
expect(head_meta_content('og:description')).to eq status_text
end
def head_link_icons
response
.parsed_body
.search('html head link[rel=icon]')
end
def head_meta_content(property)
response
.parsed_body
.search("html head meta[property='#{property}']")
.attr('content')
.text
end
def head_meta_exists(property)
!response
.parsed_body
.search("html head meta[property='#{property}']")
.empty?
end
end
end

View File

@ -0,0 +1,24 @@
# frozen_string_literal: true
module HtmlHeadInspection
def head_link_icons
response
.parsed_body
.search('html head link[rel=icon]')
end
def head_meta_content(property)
response
.parsed_body
.search("html head meta[property='#{property}']")
.attr('content')
.text
end
def head_meta_exists(property)
!response
.parsed_body
.search("html head meta[property='#{property}']")
.empty?
end
end