[object Object]

← back to 1950swallpaper

/api/facets counts reflect the active filtered result set (drill-down via shared filterProducts)

4f3f7ef12bf3164ccb1c4c6af98270b23954de27 · 2026-06-01 07:14:29 -0700 · SteveStudio2

Files touched

Diff

commit 4f3f7ef12bf3164ccb1c4c6af98270b23954de27
Author: SteveStudio2 <steve@designerwallcoverings.com>
Date:   Mon Jun 1 07:14:29 2026 -0700

    /api/facets counts reflect the active filtered result set (drill-down via shared filterProducts)
---
 server.js | 42 +++++++++++++++++++++++++++++++-----------
 1 file changed, 31 insertions(+), 11 deletions(-)

diff --git a/server.js b/server.js
index 756808b..f2a0452 100644
--- a/server.js
+++ b/server.js
@@ -166,6 +166,13 @@ try {
 }
 require("./_universal-contact")(app, cfg.contact);
 require("./_universal-auth")(app, cfg.auth);
+
+// Clean URLs: 301 the legacy /foo.html form to the extension-less /foo.
+// Runs BEFORE express.static so the static handler can't serve the .html copy.
+for (const page of ['care', 'history', 'sourcing', 'trade']) {
+  app.get('/' + page + '.html', (req, res) => res.redirect(301, '/' + page));
+}
+
 app.use(express.static(path.join(__dirname, 'public')));
 
 // Clean-URL routes for extension-less nav links to static content pages.
@@ -175,20 +182,27 @@ for (const page of ['care', 'history', 'sourcing', 'trade']) {
   });
 }
 
-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 active result set.
+function filterProducts(query) {
+  const { q, aesthetic, vendor, tag } = query;
   let list = PRODUCTS_NICHE;
   if (q) {
-    const needle = q.toLowerCase();
-    list = list.filter(p => (p.title || '').toLowerCase().includes(needle) || (p.tags || []).some(t => t.toLowerCase().includes(needle)));
+    const needle = String(q).toLowerCase();
+    list = list.filter(p => (p.title || '').toLowerCase().includes(needle) || (p.tags || []).some(t => String(t).toLowerCase().includes(needle)));
   }
   if (aesthetic && aesthetic !== 'all') list = list.filter(p => p.aesthetic === aesthetic);
   // SWEEP-A1: tag filter — sub-bucket rails on the homepage call /api/products?tag=<name>
-  if (req.query.tag) {
-    const wanted = String(req.query.tag).toLowerCase();
+  if (tag) {
+    const wanted = String(tag).toLowerCase();
     list = list.filter(p => (p.tags||[]).some(t => String(t).toLowerCase() === wanted));
   }
   if (vendor && vendor !== 'all') list = list.filter(p => p.vendor === vendor);
+  return list;
+}
+
+app.get('/api/products', (req, res) => {
+  const { page = 1, limit = 24, sort = 'newest'} = req.query;
+  let list = filterProducts(req.query);
   list = sortProducts(list, sort);
   const total = list.length;
   const pageNum = Math.max(1, parseInt(page) || 1);
@@ -208,12 +222,18 @@ app.get('/api/sliders', (req, res) => {
 });
 
 app.get('/api/facets', (req, res) => {
+  // Counts reflect the active FILTERED result set (drill-down), not the whole
+  // niche catalog. Each facet dimension is computed over the set filtered by
+  // the OTHER active dimensions so its own options stay selectable.
+  const base = filterProducts(req.query);
   const aesthetics = {}; const vendors = {};
-  for (const p of PRODUCTS_NICHE) {
-    aesthetics[p.aesthetic] = (aesthetics[p.aesthetic] || 0) + 1;
-    vendors[p.vendor] = (vendors[p.vendor] || 0) + 1;
-  }
-  res.json({ aesthetics, vendors, total: PRODUCTS_NICHE.length });
+  // aesthetic counts: filter by everything EXCEPT aesthetic
+  const aesView = filterProducts({ ...req.query, aesthetic: undefined });
+  for (const p of aesView) aesthetics[p.aesthetic] = (aesthetics[p.aesthetic] || 0) + 1;
+  // vendor counts: filter by everything EXCEPT vendor
+  const venView = filterProducts({ ...req.query, vendor: undefined });
+  for (const p of venView) vendors[p.vendor] = (vendors[p.vendor] || 0) + 1;
+  res.json({ aesthetics, vendors, total: base.length });
 });
 
 app.get('/api/health', (req, res) => res.json({ status: 'ok', count: PRODUCTS_NICHE.length, dropped: DROPPED }));

← abff768 fix: remove leaked public/index.html.bak-* file and tighten  ·  back to 1950swallpaper  ·  Remove Big Red widget — never UX-worked, collided with Help e68486a →