[object Object]

← back to 1920swallpaper

fix /api/facets to count the filtered result set, not the whole catalog

ca80344c96516226907283563c5949b58de738de · 2026-06-01 07:10:07 -0700 · SteveStudio2

Files touched

Diff

commit ca80344c96516226907283563c5949b58de738de
Author: SteveStudio2 <steve@designerwallcoverings.com>
Date:   Mon Jun 1 07:10:07 2026 -0700

    fix /api/facets to count the filtered result set, not the whole catalog
---
 server.js | 38 ++++++++++++++++++++++++++------------
 1 file changed, 26 insertions(+), 12 deletions(-)

diff --git a/server.js b/server.js
index e11042e..cec26ce 100644
--- a/server.js
+++ b/server.js
@@ -156,20 +156,29 @@ app.use((req, res, next) => {
 
 app.use(express.static(path.join(__dirname, 'public'), { extensions: ['html'] }));
 
-app.get('/api/products', (req, res) => {
-  const { q, aesthetic, vendor, page = 1, limit = 24, sort = 'newest'} = req.query;
+// Shared query-filter — applied identically by /api/products AND /api/facets so
+// facet counts reflect the actual filtered result set, not the whole catalog.
+// `skip` lets the facets endpoint exclude one dimension (drill-down counts).
+function filterProducts(query, skip) {
   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 { q, aesthetic, vendor, tag } = query;
+  if (skip !== 'q' && q) {
+    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);
+  if (skip !== 'aesthetic' && 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();
-    list = list.filter(p => (p.tags||[]).some(t => String(t).toLowerCase() === wanted));
+  if (skip !== 'tag' && 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);
+  if (skip !== 'vendor' && 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);
@@ -189,12 +198,17 @@ app.get('/api/sliders', (req, res) => {
 });
 
 app.get('/api/facets', (req, res) => {
+  // Each facet dimension is counted over the set filtered by the OTHER active
+  // filters (drill-down counts), so the numbers track the live result set
+  // instead of the unfiltered catalog.
   const aesthetics = {}; const vendors = {};
-  for (const p of PRODUCTS_NICHE) {
+  for (const p of filterProducts(req.query, 'aesthetic')) {
     aesthetics[p.aesthetic] = (aesthetics[p.aesthetic] || 0) + 1;
+  }
+  for (const p of filterProducts(req.query, 'vendor')) {
     vendors[p.vendor] = (vendors[p.vendor] || 0) + 1;
   }
-  res.json({ aesthetics, vendors, total: PRODUCTS_NICHE.length });
+  res.json({ aesthetics, vendors, total: filterProducts(req.query).length });
 });
 
 app.get('/api/health', (req, res) => res.json({ status: 'ok', count: PRODUCTS_NICHE.length, dropped: DROPPED }));

← 7524515 fix: block .bak-* backup file leaks from express.static  ·  back to 1920swallpaper  ·  add 301 redirect from legacy .html URLs to clean-URL form bcb7468 →