[object Object]

← back to Contractwallpaper

/api/facets drill-down counts via shared filterProducts() + legacy .html->clean 301 redirect

c90b50da0088bd59fe8b362acac881afc3ffbf8c · 2026-06-01 07:24:54 -0700 · Steve Abrams

facets now counts each dimension over the set filtered by the OTHER active
facets (was counting the whole niche catalog regardless of q/aesthetic/vendor);
/api/products reuses the same helper. Adds 301 from /care|history|sourcing|trade|vocabulary.html
to the extension-less clean URL (runs before express.static).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

Files touched

Diff

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

    /api/facets drill-down counts via shared filterProducts() + legacy .html->clean 301 redirect
    
    facets now counts each dimension over the set filtered by the OTHER active
    facets (was counting the whole niche catalog regardless of q/aesthetic/vendor);
    /api/products reuses the same helper. Adds 301 from /care|history|sourcing|trade|vocabulary.html
    to the extension-less clean URL (runs before express.static).
    
    Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
---
 server.js | 35 ++++++++++++++++++++++++++++-------
 1 file changed, 28 insertions(+), 7 deletions(-)

diff --git a/server.js b/server.js
index 72d924a..2ec27c4 100644
--- a/server.js
+++ b/server.js
@@ -147,17 +147,33 @@ app.use(require('../_shared/api-vendor-redact'));
 require('./_universal-contact')(app, { siteName: "Contract Wallpaper", zdColor: "#7ea8d4", zdPosition: 'right' });
 require('./_universal-auth')(app, { siteName: "contractwallpaper" });
 
+// Clean-URL canonicalization: 301 the legacy `.html` form to the extension-less
+// path (must run BEFORE express.static, else the .html file is served directly).
+app.get(/^\/(care|history|sourcing|trade|vocabulary)\.html$/i, (req, res) => {
+  res.redirect(301, '/' + req.params[0].toLowerCase());
+});
+
 app.use(express.static(path.join(__dirname, 'public')));
 
-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 always reflect the active filtered result set (drill-down),
+// never the whole niche catalog. `skip` lets a facet exclude its own dimension
+// so each facet counts against the OTHER active filters.
+function filterProducts(query, skip) {
+  const { q, aesthetic, vendor } = query || {};
   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 { 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);
@@ -177,11 +193,16 @@ app.get('/api/sliders', (req, res) => {
 
 app.get('/api/facets', (req, res) => {
   const aesthetics = {}; const vendors = {};
-  for (const p of PRODUCTS_NICHE) {
+  // Each facet counts over the set filtered by the OTHER active facets (so the
+  // counts drill down with the user's selections instead of staying fixed at
+  // the whole-catalog totals).
+  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 }));

← 34cc042 fix: add *.bak-* pattern to .gitignore and remove leaked pub  ·  back to Contractwallpaper  ·  noopener,noreferrer on sister-site window.open popup 15566a6 →