Class: IndexBoost::Middleware
- Inherits:
-
Object
- Object
- IndexBoost::Middleware
- Defined in:
- lib/indexboost/middleware.rb
Overview
Rack middleware that intercepts crawler requests and serves rendered HTML from the IndexBoost render service.
Usage in config/application.rb:
config.middleware.insert_before 0, IndexBoost::Middleware,
token: ENV.fetch("INDEXBOOST_TOKEN")
Constant Summary collapse
- CRAWLERS =
/googlebot|bingbot|gptbot|claudebot|perplexitybot|duckduckbot| slurp|naverbot|yandexbot|baiduspider|facebookexternalhit| twitterbot|linkedinbot|whatsapp|telegrambot|applebot|rogerbot| semrushbot|ahrefsbot|bytespider|dotbot|mj12bot|pinterestbot/ix- STATIC_EXTENSIONS =
/\.(js|css|png|jpg|jpeg|gif|webp|svg|ico|woff|woff2| ttf|eot|otf|pdf|zip|xml|map|txt|json|csv|gz|br)$/ix- DEFAULT_SERVICE_URL =
"https://render.getindexboost.com"
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, token:, **options) ⇒ Middleware
constructor
A new instance of Middleware.
Constructor Details
#initialize(app, token:, **options) ⇒ Middleware
Returns a new instance of Middleware.
29 30 31 32 33 34 35 36 37 |
# File 'lib/indexboost/middleware.rb', line 29 def initialize(app, token:, **) raise ArgumentError, "[IndexBoost] token is required" if token.nil? || token.empty? @app = app @token = token @service_url = .fetch(:service_url, DEFAULT_SERVICE_URL).chomp("/") @timeout = .fetch(:timeout, 30) @ignored_paths = .fetch(:ignored_paths, [%r{^/api/}, %r{^/rails/}]) end |
Instance Method Details
#call(env) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/indexboost/middleware.rb', line 39 def call(env) return @app.call(env) unless should_render?(env) html = fetch_rendered(env) return @app.call(env) if html.nil? [ 200, { "Content-Type" => "text/html; charset=utf-8", "X-IndexBoost-Rendered" => "true", "Cache-Control" => "public, max-age=3600", }, [html], ] end |