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, 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.xml` → `public_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.xml` → `storage_prefix: “/sitemaps/tenant”`



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/site_maps/middleware.rb', line 31

def initialize(
  app,
  adapter: nil,
  public_prefix: nil,
  storage_prefix: 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
  @x_robots_tag = x_robots_tag
  @cache_control = cache_control
end

Instance Method Details

#call(env) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/site_maps/middleware.rb', line 47

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

  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