Class: SiteMaps::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/site_maps/middleware.rb

Constant Summary collapse

DEFAULT_X_ROBOTS_TAG =
"noindex, follow"
DEFAULT_CACHE_CONTROL =
"public, max-age=3600"
URLSET_XSL_PATH =
"/_sitemap-stylesheet.xsl"
INDEX_XSL_PATH =
"/_sitemap-index-stylesheet.xsl"

Instance Method Summary collapse

Constructor Details

#initialize(app, adapter: nil, public_prefix: nil, storage_prefix: nil, aliases: nil, x_robots_tag: DEFAULT_X_ROBOTS_TAG, cache_control: DEFAULT_CACHE_CONTROL) ⇒ Middleware

Both options accept a callable (0-arg or 1-arg receiving env), which is useful in multi-tenant setups where the prefix depends on the current request/site.

Parameters:

  • adapter (Object, #call, nil) (defaults to: nil)

    Adapter instance, a callable (0-arg or 1-arg receiving the Rack env) that returns an adapter, or nil to fall back to SiteMaps.current_adapter.

  • public_prefix (String, #call, nil) (defaults to: nil)

    A prefix present in the public URL that is absent from the storage path. Stripped from the incoming request path to derive the internal lookup path.

    Example: sitemaps stored at /sitemap.xml, served publicly at /sitemaps/tenant/sitemap.xmlpublic_prefix: "/sitemaps/tenant"

  • storage_prefix (String, #call, nil) (defaults to: nil)

    A prefix present in the storage path that is absent from the public URL. Prepended to the incoming request path to derive the internal lookup path.

    Example: sitemaps stored at /sitemaps/tenant/sitemap.xml, served publicly at /sitemap.xmlstorage_prefix: "/sitemaps/tenant"

  • aliases (Hash{String=>String}, #call, nil) (defaults to: nil)

    Maps an incoming public request path to another public request path, applied before prefix handling. Serves the target's content at the alias path (no redirect).

    Example: serve the sitemap index at both locations → aliases: { "/sitemap.xml" => "/sitemap_index.xml" }

    Accepts a callable (0-arg or 1-arg receiving env) returning such a hash.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/site_maps/middleware.rb', line 40

def initialize(
  app,
  adapter: nil,
  public_prefix: nil,
  storage_prefix: nil,
  aliases: nil,
  x_robots_tag: DEFAULT_X_ROBOTS_TAG,
  cache_control: DEFAULT_CACHE_CONTROL
)
  @app = app
  @adapter = adapter
  @public_prefix = public_prefix
  @storage_prefix = storage_prefix
  @aliases = aliases
  @x_robots_tag = x_robots_tag
  @cache_control = cache_control
end

Instance Method Details

#call(env) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/site_maps/middleware.rb', line 58

def call(env)
  path = resolve_alias(env["PATH_INFO"], env)

  if xsl_request?(path)
    serve_xsl(path)
  elsif path.end_with?(".xml", ".xml.gz")
    pub_prefix = resolve_value(@public_prefix, env)
    sto_prefix = resolve_value(@storage_prefix, env)

    # Strip public prefix (nil = no match when prefix is configured but doesn't match)
    stripped = strip_prefix(path, pub_prefix)

    # Prepend storage prefix to get the internal path used for adapter lookups
    internal_path = stripped && prepend_prefix(stripped, sto_prefix)

    # Only resolve the adapter (potentially expensive: DB lookup, callable) when
    # the path already looks like a sitemap file and passed prefix checks.
    current_adapter = resolve_adapter(env) if internal_path
    if current_adapter && sitemap_request?(internal_path, current_adapter)
      serve_sitemap(internal_path, current_adapter, pub_prefix: pub_prefix, sto_prefix: sto_prefix)
    else
      @app.call(env)
    end
  else
    @app.call(env)
  end
end