evaluate method

FacetedUrlDecision evaluate(
  1. Map<String, String> queryParameters
)

Evaluates an incoming faceted query map and returns canonical and indexing decisions.

Implementation

FacetedUrlDecision evaluate(Map<String, String> queryParameters) {
  final cleanBase = baseCategoryUrl.replaceAll(RegExp(r'/+$'), '');
  final pageNum = int.tryParse(queryParameters['page'] ?? '') ?? 1;

  // 1. Check deep pagination crawl budget protection
  if (pageNum > noindexPaginationAfterPage) {
    return FacetedUrlDecision(
      shouldIndex: false,
      canonicalUrl: '$cleanBase/',
      robots: RobotsDirective.noindexFollow,
      disallowedReason:
          'Deep pagination beyond page $noindexPaginationAfterPage',
    );
  }

  // 2. Filter out pagination from parameter count check
  final activeFilters = queryParameters.keys
      .where((k) => k != 'page' && k != 'sort')
      .toSet();

  // 3. Unlimited combinatorics trap prevention: If filters exceed threshold, noindex
  if (activeFilters.length > maxFilterParameters) {
    return FacetedUrlDecision(
      shouldIndex: false,
      canonicalUrl: '$cleanBase/',
      robots: RobotsDirective.noindexFollow,
      disallowedReason:
          'Active filter count (${activeFilters.length}) exceeds threshold ($maxFilterParameters)',
    );
  }

  // 4. Sorting query parameters (price_asc, newest) should never be indexed as distinct pages
  if (queryParameters.containsKey('sort')) {
    return FacetedUrlDecision(
      shouldIndex: false,
      canonicalUrl: '$cleanBase/',
      robots: RobotsDirective.noindexFollow,
      disallowedReason:
          'Sorting parameter should not produce duplicate indexable pages',
    );
  }

  // 5. Curated indexable facets (e.g. /category?brand=nike)
  final isAllowedIndexable =
      activeFilters.isNotEmpty &&
      activeFilters.every((f) => allowedIndexableParams.contains(f));

  if (isAllowedIndexable && !canonicalizeToCategory) {
    final sortedQuery = activeFilters
        .map((k) => '$k=${queryParameters[k]}')
        .join('&');
    return FacetedUrlDecision(
      shouldIndex: true,
      canonicalUrl: '$cleanBase/?$sortedQuery',
      robots: RobotsDirective.indexFollow,
    );
  }

  // Default: canonicalize to base category URL
  return FacetedUrlDecision(
    shouldIndex: activeFilters.isEmpty && pageNum == 1,
    canonicalUrl: '$cleanBase/',
    robots: RobotsDirective.indexFollow,
  );
}