[object Object]

← back to Agedwallpaper

facets: count over filtered result set (drill-down via shared filterProducts), not whole niche catalog

dc135587902f16365787ce905f4ad15d4af8c9aa · 2026-06-01 07:16:57 -0700 · SteveStudio2

Files touched

Diff

commit dc135587902f16365787ce905f4ad15d4af8c9aa
Author: SteveStudio2 <steve@designerwallcoverings.com>
Date:   Mon Jun 1 07:16:57 2026 -0700

    facets: count over filtered result set (drill-down via shared filterProducts), not whole niche catalog
---
 server.js | 48 +++++++++++++++++++++++++++++++++++-------------
 1 file changed, 35 insertions(+), 13 deletions(-)

diff --git a/server.js b/server.js
index 7f74e32..4d4613f 100644
--- a/server.js
+++ b/server.js
@@ -165,23 +165,43 @@ app.use((req, res, next) => {
   if (/\.(bak)([.\-]|$)|\.pre-|\.orig$/i.test(req.path)) return res.status(404).send('Not found');
   next();
 });
+// Legacy .html URLs 301 → clean form (before static so .html never serves directly).
+const CLEAN_PAGES = { care: 'care.html', history: 'history.html', sourcing: 'sourcing.html', trade: 'trade.html' };
+for (const route of Object.keys(CLEAN_PAGES)) {
+  app.get('/' + route + '.html', (req, res) => res.redirect(301, '/' + route));
+}
+
 app.use(express.static(path.join(__dirname, 'public')));
 
 // Clean-URL routes for extension-less nav links → static pages.
-const CLEAN_PAGES = { care: 'care.html', history: 'history.html', sourcing: 'sourcing.html', trade: 'trade.html', about: 'history.html' };
 for (const [route, file] of Object.entries(CLEAN_PAGES)) {
   app.get('/' + route, (req, res) => res.sendFile(path.join(__dirname, 'public', file)));
 }
+// /about → served by the "short history" page (no dedicated about.html exists)
+app.get('/about', (req, res) => res.sendFile(path.join(__dirname, 'public', 'history.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 (skip !== 'aesthetic' && aesthetic && aesthetic !== 'all') list = list.filter(p => p.aesthetic === aesthetic);
+  if (skip !== 'tag' && tag) {
+    const wanted = String(tag).toLowerCase();
+    list = list.filter(p => (p.tags || []).some(t => String(t).toLowerCase() === wanted));
   }
-  if (aesthetic && aesthetic !== 'all') list = list.filter(p => p.aesthetic === aesthetic);
-  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);
@@ -201,12 +221,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 }));
@@ -217,9 +242,6 @@ app.get('/sample/:handle', (req, res) => {
   res.redirect(302, p.product_url || `${DW_SHOPIFY}/products/${encodeURIComponent(p.handle)}#sample`);
 });
 
-// /about → served by the "short history" page (no dedicated about.html exists)
-app.get('/about', (req, res) => res.sendFile(path.join(__dirname, 'public', 'history.html')));
-
 // sitemap.xml + robots.txt for SEO
 app.get('/robots.txt', (req, res) => {
   res.type('text/plain').send(`User-agent: *

← 4b8dc70 fix: remove leaked public/index.html.bak, harden bak guard r  ·  back to Agedwallpaper  ·  Remove Big Red widget — never UX-worked, collided with Help e28703a →