[object Object]

← back to Apartmentwallpaper

facets count over filtered result set via shared filterProducts(); 301 legacy .html to clean URLs

7f2f2b64f15a45508867b5ad15ede9bfc3c9155b · 2026-06-01 07:18:41 -0700 · Steve

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

Files touched

Diff

commit 7f2f2b64f15a45508867b5ad15ede9bfc3c9155b
Author: Steve <steve@designerwallcoverings.com>
Date:   Mon Jun 1 07:18:41 2026 -0700

    facets count over filtered result set via shared filterProducts(); 301 legacy .html to clean URLs
    
    Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
---
 server.js | 46 +++++++++++++++++++++++++++++++++++-----------
 1 file changed, 35 insertions(+), 11 deletions(-)

diff --git a/server.js b/server.js
index fc16dc4..070722e 100644
--- a/server.js
+++ b/server.js
@@ -85,6 +85,12 @@ app.use((req, res, next) => {
 // Fleet-wide DW vendor-name redactor on /api/* (drops vendors facet, replaces vendor field).
 app.use(require('../_shared/api-vendor-redact'));
 
+// Clean URLs: 301 the legacy /foo.html form to the extension-less /foo.
+// Runs BEFORE express.static so the static handler can't serve the .html copy.
+for (const page of ['care', 'history', 'sourcing', 'trade', 'vocabulary']) {
+  app.get('/' + page + '.html', (req, res) => res.redirect(301, '/' + page));
+}
+
 app.use(express.static(path.join(__dirname, 'public')));
 
 // Clean-URL routes for editorial pages (extension-less nav links).
@@ -142,19 +148,32 @@ function sortProducts(list, sort) {
   }
 }
 
-app.get('/api/products', (req, res) => {
-  const { q, aesthetic, vendor, sort, page = 1, limit = 24 } = 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 { sort, page = 1, limit = 24 } = req.query;
+  let list = filterProducts(req.query);
   list = sortProducts(list, sort);
   const total = list.length;
-  const pageNum = Math.max(1, parseInt(page));
-  const lim = Math.min(60, parseInt(limit));
+  const pageNum = Math.max(1, parseInt(page) || 1);
+  const lim = Math.min(60, parseInt(limit) || 24);
   const start = (pageNum - 1) * lim;
   res.json({ total, page: pageNum, limit: lim, pages: Math.ceil(total / lim), items: list.slice(start, start + lim) });
 });
@@ -169,12 +188,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 }));

← 1918bed feat(fleet): theme1/theme2 query-param-gated toggle on catal  ·  back to Apartmentwallpaper  ·  add noopener,noreferrer to sister-site window.open popup dc74f6b →