[object Object]

← back to 1900swallpaper

facets counts now reflect q+tag filters, add legacy .html -> clean-URL 301 redirect

879943987d64b465c8d5a0209667543da47c1858 · 2026-06-01 07:11:12 -0700 · steve

Files touched

Diff

commit 879943987d64b465c8d5a0209667543da47c1858
Author: steve <steve@designerwallcoverings.com>
Date:   Mon Jun 1 07:11:12 2026 -0700

    facets counts now reflect q+tag filters, add legacy .html -> clean-URL 301 redirect
---
 server.js | 31 ++++++++++++++++++++++++++++---
 1 file changed, 28 insertions(+), 3 deletions(-)

diff --git a/server.js b/server.js
index fbf0922..92c1706 100644
--- a/server.js
+++ b/server.js
@@ -155,6 +155,15 @@ app.use((req, res, next) => {
   next();
 });
 
+// Clean-URL canonicalisation: 301 the legacy `/foo.html` form to `/foo`. The
+// static handler below (extensions:['html']) then serves the file. Idempotent —
+// requests already in clean form fall straight through.
+app.get(/^\/(.+)\.html$/i, (req, res, next) => {
+  const base = req.params[0];
+  if (base === 'index') return res.redirect(301, '/');
+  return res.redirect(301, '/' + base + (req.url.includes('?') ? req.url.slice(req.url.indexOf('?')) : ''));
+});
+
 app.use(express.static(path.join(__dirname, 'public'), { extensions: ['html'] }));
 
 app.get('/api/products', (req, res) => {
@@ -185,12 +194,28 @@ app.get('/api/sliders', (req, res) => {
 });
 
 app.get('/api/facets', (req, res) => {
-  const aesthetics = railFacets(PRODUCTS_NICHE, SITE_RAILS);
+  // Counts MUST reflect the same filters as /api/products so chip labels match
+  // post-filter card counts. Apply the q + tag filters here; the `aesthetic`
+  // filter is intentionally NOT applied to the aesthetics facet so the rail
+  // chips stay switchable, but it IS applied to the reported `total`.
+  const { q, aesthetic } = req.query;
+  let base = PRODUCTS_NICHE;
+  if (q) {
+    const needle = String(q).toLowerCase();
+    base = base.filter(p => (p.title || '').toLowerCase().includes(needle) || (p.tags || []).some(t => String(t).toLowerCase().includes(needle)));
+  }
+  if (req.query.tag) {
+    const wanted = String(req.query.tag).toLowerCase();
+    base = base.filter(p => (p.tags || []).some(t => String(t).toLowerCase() === wanted));
+  }
+  const aesthetics = railFacets(base, SITE_RAILS);
   const vendors = {};
-  for (const p of PRODUCTS_NICHE) {
+  for (const p of base) {
     vendors[p.vendor] = (vendors[p.vendor] || 0) + 1;
   }
-  res.json({ aesthetics, vendors, total: PRODUCTS_NICHE.length });
+  let totalList = base;
+  if (aesthetic && aesthetic !== 'all') totalList = base.filter(p => productInRail(p, aesthetic));
+  res.json({ aesthetics, vendors, total: totalList.length });
 });
 
 app.get('/api/health', (req, res) => res.json({ status: 'ok', count: PRODUCTS_NICHE.length, dropped: DROPPED }));

← 3b01401 fix: remove leaked public bak file and fix 404 guard regex t  ·  back to 1900swallpaper  ·  add explicit noopener,noreferrer to window.open external nav a00a92b →