Class: Seo::BrokenLinkRedirectMap

Inherits:
Object
  • Object
show all
Defined in:
app/services/seo/broken_link_redirect_map.rb

Overview

Resolves legacy product/support URL patterns to current canonical paths
via DB lookups. Used by Seo::InternalLinkValidator#suggest_correction to
provide actionable suggestions when Sunny writes a deprecated URL format.

All static redirect rules (category renames, path moves) are handled by
Cloudflare bulk redirects and the site_maps table — NOT hardcoded here.

Constant Summary collapse

LEGACY_DYNAMIC_PATTERNS =
[
  { regex: %r{\A/products/code/([^/]+?)(/reviews|/faqs)?\z},  type: :sku },
  { regex: %r{\A/products/line/([^/]+?)(/reviews|/faqs)?\z},  type: :product_line },
  { regex: %r{\A/support/sku/([^/]+)\z},                      type: :support_sku },
  { regex: %r{\A/support/([^/]+)/([^/]+)\z},                  type: :support_sub_pl },
  { regex: %r{\A/support/([^/]+)\z},                          type: :support_main_pl },
].freeze
SUPPORT_STATIC_SLUGS =
%w[search installation request articles].freeze

Class Method Summary collapse

Class Method Details

.lookup(path) ⇒ String?

Look up a path in the legacy dynamic patterns.

Parameters:

  • path (String)

    e.g., "/products/code/TW-SM05KS-HP"

Returns:

  • (String, nil)

    canonical path or nil if not a legacy pattern



23
24
25
26
27
28
29
30
# File 'app/services/seo/broken_link_redirect_map.rb', line 23

def self.lookup(path)
  return nil if path.blank?

  normalized = path.chomp('/')
  normalized = path if normalized.blank?

  resolve_legacy_dynamic(normalized)
end

.resolve_legacy_dynamic(path) ⇒ String?

Resolve legacy /products/code/, /products/line/, /support/ paths by
looking up the canonical path from ProductLine/Item records.

Returns:

  • (String, nil)

    canonical path or nil if not a legacy pattern



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/services/seo/broken_link_redirect_map.rb', line 35

def self.resolve_legacy_dynamic(path)
  LEGACY_DYNAMIC_PATTERNS.each do |pattern|
    match = path.match(pattern[:regex])
    next unless match

    return case pattern[:type]
           when :sku
             item = Item.find_by(sku: match[1])
             next unless item&.canonical_path
             section = match[2]&.delete_prefix('/')
             section ? "/#{item.canonical_path}/#{section}" : "/#{item.canonical_path}"
           when :product_line
             pl = ProductLine.find_by(slug_ltree: LtreePaths.slug_ltree_from_legacy_hyphen_url(match[1]))
             next unless pl&.canonical_path
             section = match[2]&.delete_prefix('/')
             section ? "/#{pl.canonical_path}/#{section}" : "/#{pl.canonical_path}"
           when :support_sku
             item = Item.find_by(sku: match[1])
             next unless item&.canonical_path
             "/#{item.canonical_path}/support"
           when :support_sub_pl
             pl = ProductLine.find_by(slug_ltree: LtreePaths.slug_ltree_from_legacy_hyphen_url(match[2]))
             next unless pl&.canonical_path
             "/#{pl.canonical_path}/support"
           when :support_main_pl
             return nil if match[1].in?(SUPPORT_STATIC_SLUGS)
             pl = ProductLine.find_by(slug_ltree: LtreePaths.slug_ltree_from_legacy_hyphen_url(match[1]))
             next unless pl&.canonical_path
             "/#{pl.canonical_path}/support"
           end
  end

  nil
end