[object Object]

← back to Selfadhesivewallpaper

facets drill-down filtered counts + legacy .html->clean 301 redirects

ea6ec834aa81b781583ac8aa0f20754d86123be0 · 2026-06-01 07:48:24 -0700 · Steve

Files touched

Diff

commit ea6ec834aa81b781583ac8aa0f20754d86123be0
Author: Steve <steve@designerwallcoverings.com>
Date:   Mon Jun 1 07:48:24 2026 -0700

    facets drill-down filtered counts + legacy .html->clean 301 redirects
---
 server.js | 40 ++++++++++++++++++++++++++++++++--------
 1 file changed, 32 insertions(+), 8 deletions(-)

diff --git a/server.js b/server.js
index 8a5ebcf..eced8f8 100644
--- a/server.js
+++ b/server.js
@@ -114,6 +114,14 @@ app.use((req, res, next) => {
   }
   next();
 });
+// Canonicalize legacy .html URLs → clean form with a 301 (static still serves
+// both forms otherwise = duplicate content). Mounted BEFORE express.static.
+const HTML_TO_CLEAN = { '/index.html': '/', '/history.html': '/history', '/care.html': '/care', '/trade.html': '/trade', '/sourcing.html': '/sourcing', '/vocabulary.html': '/vocabulary' };
+app.get(/\.html$/i, (req, res, next) => {
+  const dest = HTML_TO_CLEAN[req.path];
+  if (dest) return res.redirect(301, dest);
+  next();
+});
 app.use(express.static(path.join(__dirname, 'public')));
 
 // Clean-URL routes for extension-less nav links.
@@ -125,15 +133,24 @@ for (const [route, file] of Object.entries(CLEAN_PAGES)) {
   });
 }
 
-app.get('/api/products', (req, res) => {
-  const { q, aesthetic, vendor, sort = 'newest', page = 1, limit = 24 } = req.query;
+// Shared filter — applied identically by /api/products and /api/facets so
+// facet counts reflect the actual FILTERED result set, not the whole catalog.
+// `skip` lets a facet dimension exclude its OWN filter for drill-down counts.
+function filterProducts(opts, skip) {
+  const { q, aesthetic, vendor } = opts;
   let list = PRODUCTS_NICHE;
-  if (q) {
-    const needle = q.toLowerCase();
+  if (q && skip !== 'q') {
+    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 (aesthetic && aesthetic !== 'all' && skip !== 'aesthetic') list = list.filter(p => p.aesthetic === aesthetic);
+  if (vendor && vendor !== 'all' && skip !== 'vendor') list = list.filter(p => p.vendor === vendor);
+  return list;
+}
+
+app.get('/api/products', (req, res) => {
+  const { q, aesthetic, vendor, sort = 'newest', page = 1, limit = 24 } = req.query;
+  let list = filterProducts({ q, aesthetic, vendor });
   list = sortProducts(list, sort);
   const total = list.length;
   const pageNum = Math.max(1, parseInt(page));
@@ -152,12 +169,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 dimension's counts are computed over the set narrowed by the OTHER
+  // active facets (drill-down), so a chip shows how many results it WOULD yield.
+  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;
   }
-  res.json({ aesthetics, vendors, total: PRODUCTS_NICHE.length });
+  // total = the fully-filtered result set (all active facets applied).
+  const total = filterProducts({ q, aesthetic, vendor }).length;
+  res.json({ aesthetics, vendors, total });
 });
 
 app.get('/api/health', (req, res) => res.json({ status: 'ok', count: PRODUCTS_NICHE.length, dropped: DROPPED }));

← 75b5617 noopener,noreferrer on sister-site constellation window.open  ·  back to Selfadhesivewallpaper  ·  Remove Big Red widget — never UX-worked, collided with Help aecf776 →