Class: IndexBoost::Middleware

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(app, token:, **options) ⇒ Middleware

Returns a new instance of Middleware.

Parameters:

  • app (#call)

    The next Rack app

  • token (String)

    IndexBoost render token (required)

  • options (Hash)

    a customizable set of options

Options Hash (**options):

  • :service_url (String)

    Override render URL

  • :timeout (Integer)

    HTTP timeout in seconds (default 30)

  • :ignored_paths (Array)

    Array of Regexp to skip

Raises:

  • (ArgumentError)


29
30
31
32
33
34
35
36
37
# File 'lib/indexboost/middleware.rb', line 29

def initialize(app, token:, **options)
  raise ArgumentError, "[IndexBoost] token is required" if token.nil? || token.empty?

  @app            = app
  @token          = token
  @service_url    = options.fetch(:service_url, DEFAULT_SERVICE_URL).chomp("/")
  @timeout        = options.fetch(:timeout, 30)
  @ignored_paths  = options.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