[object Object]

← back to 1940swallpaper

/api/facets filtered-set counts + legacy .html->clean 301 redirect

783392cfac5081b45e07c8483e4c25cce17b64ab · 2026-06-01 07:13:55 -0700 · steve

facets: counts honor q + cross-facet selections via shared filterProducts()
clean-URL: 301 /care.html etc -> /care (clean route already served)

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

Files touched

Diff

commit 783392cfac5081b45e07c8483e4c25cce17b64ab
Author: steve <steve@designerwallcoverings.com>
Date:   Mon Jun 1 07:13:55 2026 -0700

    /api/facets filtered-set counts + legacy .html->clean 301 redirect
    
    facets: counts honor q + cross-facet selections via shared filterProducts()
    clean-URL: 301 /care.html etc -> /care (clean route already served)
    
    Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
---
 server.js | 28 +++++++++++++++++++++++-----
 1 file changed, 23 insertions(+), 5 deletions(-)

diff --git a/server.js b/server.js
index db449e8..0976aa4 100644
--- a/server.js
+++ b/server.js
@@ -154,17 +154,29 @@ app.use((req, res, next) => {
   next();
 });
 
+// Clean-URL canonicalization — 301 the legacy /foo.html form to /foo so the
+// extension-less route is the single source of truth (no duplicate-content).
+app.get(/^\/(care|history|sourcing|trade|vocabulary)\.html$/, (req, res) => {
+  res.redirect(301, '/' + req.params[0]);
+});
+
 app.use(express.static(path.join(__dirname, 'public')));
 
-app.get('/api/products', (req, res) => {
-  const { q, aesthetic, vendor, page = 1, limit = 24, sort = 'newest'} = req.query;
+// Shared filter so /api/products and /api/facets agree on the result set.
+function filterProducts({ q, aesthetic, vendor } = {}) {
   let list = PRODUCTS_NICHE;
   if (q) {
-    const needle = q.toLowerCase();
+    const needle = String(q).toLowerCase();
     list = list.filter(p => (p.title || '').toLowerCase().includes(needle) || (p.tags || []).some(t => t.toLowerCase().includes(needle)));
   }
   if (aesthetic && aesthetic !== 'all') list = list.filter(p => p.aesthetic === aesthetic);
   if (vendor && vendor !== 'all') list = list.filter(p => p.vendor === vendor);
+  return list;
+}
+
+app.get('/api/products', (req, res) => {
+  const { q, aesthetic, vendor, page = 1, limit = 24, sort = 'newest'} = req.query;
+  let list = filterProducts({ q, aesthetic, vendor });
   list = sortProducts(list, sort);
   const total = list.length;
   const pageNum = Math.max(1, parseInt(page) || 1);
@@ -184,12 +196,18 @@ app.get('/api/sliders', (req, res) => {
 });
 
 app.get('/api/facets', (req, res) => {
+  // Counts reflect the actual FILTERED result set (honoring q + cross-facet
+  // selections), not the unfiltered niche catalog. For each facet dimension we
+  // hold OUT that dimension's own selection so its option counts stay drill-able.
+  const { q, aesthetic, vendor } = req.query;
   const aesthetics = {}; const vendors = {};
-  for (const p of PRODUCTS_NICHE) {
+  for (const p of filterProducts({ q, vendor })) {
     aesthetics[p.aesthetic] = (aesthetics[p.aesthetic] || 0) + 1;
+  }
+  for (const p of filterProducts({ q, aesthetic })) {
     vendors[p.vendor] = (vendors[p.vendor] || 0) + 1;
   }
-  res.json({ aesthetics, vendors, total: PRODUCTS_NICHE.length });
+  res.json({ aesthetics, vendors, total: filterProducts({ q, aesthetic, vendor }).length });
 });
 
 app.get('/api/health', (req, res) => res.json({ status: 'ok', count: PRODUCTS_NICHE.length, dropped: DROPPED }));

← 61f2bbd noopener,noreferrer on sister-site window.open popup  ·  back to 1940swallpaper  ·  Remove Big Red widget — never UX-worked, collided with Help 5ffef2e →