[object Object]

← back to Hospitalitywallpaper

facets: count over the active filtered set via shared filterProducts() drill-down + 301 redirect legacy .html to clean URLs

c601a13f1ecfd18173a7e210cd73308641e971df · 2026-06-01 07:30:44 -0700 · Steve Abrams

Files touched

Diff

commit c601a13f1ecfd18173a7e210cd73308641e971df
Author: Steve Abrams <steve@designerwallcoverings.com>
Date:   Mon Jun 1 07:30:44 2026 -0700

    facets: count over the active filtered set via shared filterProducts() drill-down + 301 redirect legacy .html to clean URLs
---
 server.js | 39 ++++++++++++++++++++++++++++++---------
 1 file changed, 30 insertions(+), 9 deletions(-)

diff --git a/server.js b/server.js
index 7a23a43..d33a57b 100644
--- a/server.js
+++ b/server.js
@@ -159,6 +159,13 @@ app.use((req, res, next) => {
   next();
 });
 
+// 301 legacy /*.html -> clean URLs (must run BEFORE express.static, else the
+// static handler serves the .html form). /index.html -> /, others drop .html.
+app.get(/^\/(care|history|sourcing|trade|vocabulary|index)\.html$/, (req, res) => {
+  const name = req.params[0];
+  res.redirect(301, name === 'index' ? '/' : '/' + name);
+});
+
 app.use(express.static(path.join(__dirname, 'public')));
 
 // Clean-URL routes for the static pages in public/ — lets nav links drop .html.
@@ -166,15 +173,24 @@ for (const name of ['care', 'history', 'sourcing', 'trade', 'vocabulary']) {
   app.get('/' + name, (req, res) => res.sendFile(path.join(__dirname, 'public', name + '.html')));
 }
 
-app.get('/api/products', (req, res) => {
-  const { q, aesthetic, vendor, page = 1, limit = 24, sort = 'newest'} = req.query;
+// Shared filter — applied identically by /api/products and /api/facets so that
+// facet counts reflect the actual FILTERED result set, not the whole niche.
+// `skip` excludes one dimension so each facet drills down over the OTHER
+// active selections (standard storefront drill-down behaviour).
+function filterProducts({ q, aesthetic, vendor } = {}, skip = null) {
   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);
+  if (skip !== 'aesthetic' && aesthetic && aesthetic !== 'all') list = list.filter(p => p.aesthetic === aesthetic);
+  if (skip !== 'vendor' && 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);
@@ -194,14 +210,19 @@ app.get('/api/sliders', (req, res) => {
 });
 
 app.get('/api/facets', (req, res) => {
+  const { q, aesthetic, vendor } = req.query;
   const aesthetics = {}; const vendors = {};
-  for (const p of PRODUCTS_NICHE) {
+  // Each facet dimension is counted over the set filtered by the OTHER active
+  // facets (drill-down), so counts reflect what selecting that value would
+  // actually yield — not the unfiltered niche catalog.
+  for (const p of filterProducts({ q, aesthetic, vendor }, 'aesthetic')) {
     aesthetics[p.aesthetic] = (aesthetics[p.aesthetic] || 0) + 1;
+  }
+  for (const p of filterProducts({ q, aesthetic, vendor }, 'vendor')) {
     vendors[p.vendor] = (vendors[p.vendor] || 0) + 1;
   }
-  // total reflects the FILTERED (niche) list — what users actually see in the
-  // grid. Previously emitted PRODUCTS.length which overcounted by the niche drop.
-  res.json({ aesthetics, vendors, total: PRODUCTS_NICHE.length });
+  // total reflects the fully-filtered result set — what users actually see.
+  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 }));

← 3abdac0 fix: remove public .bak snapshot + fix guard regex to block  ·  back to Hospitalitywallpaper  ·  add noopener,noreferrer to sister-site window.open popup 8031067 →