mirror of
https://gh.wpcy.net/https://github.com/discourse/discourse.git
synced 2026-05-04 09:16:52 +08:00
Follow up to comments in https://github.com/discourse/discourse/pull/34466. We apply a header to minimize duplication, and also promote the check earlier.
46 lines
1.3 KiB
Ruby
46 lines
1.3 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
module Middleware
|
|
class CrawlerHooks
|
|
def initialize(app)
|
|
@app = app
|
|
end
|
|
|
|
def call(env)
|
|
request = Rack::Request.new(env)
|
|
status, headers, response = @app.call(env)
|
|
|
|
if status == 200 && headers["X-Discourse-Crawler-View"] &&
|
|
SiteSetting.content_localization_enabled &&
|
|
SiteSetting.content_localization_crawler_param
|
|
response = transform_response(request:, response:)
|
|
end
|
|
|
|
[status, headers, response]
|
|
end
|
|
|
|
private
|
|
|
|
def transform_response(request:, response:)
|
|
locale = request.params[Discourse::LOCALE_PARAM]
|
|
|
|
if SiteSetting.content_localization_enabled &&
|
|
SiteSetting.content_localization_crawler_param && locale.present?
|
|
html_fragment = Nokogiri::HTML5.parse(response.body)
|
|
|
|
html_fragment
|
|
.css("a[href^='/'], a[href^='#{Discourse.base_url}']")
|
|
.each do |link|
|
|
uri = Addressable::URI.parse(link["href"])
|
|
uri.query_values = (uri.query_values || {}).merge(Discourse::LOCALE_PARAM => locale)
|
|
link["href"] = uri.to_s
|
|
end
|
|
|
|
transformed_html = html_fragment.to_html
|
|
return [transformed_html || response]
|
|
end
|
|
|
|
response
|
|
end
|
|
end
|
|
end
|