← back to Suedewallpaper
Facet counts reflect filtered result set + 301 legacy .html to clean URLs
f548395fc928888c6284d03e4f6e8c45017daf0b · 2026-06-01 07:51:52 -0700 · Steve
filterProducts() shared by /api/products and /api/facets so each facet chip
counts over the set narrowed by the OTHER active facets (drill-down), and
total reflects the fully-filtered set instead of the whole catalog. Add a
301 redirect from legacy /foo.html to the clean /foo path before static.
Files touched
Diff
commit f548395fc928888c6284d03e4f6e8c45017daf0b
Author: Steve <steve@designerwallcoverings.com>
Date: Mon Jun 1 07:51:52 2026 -0700
Facet counts reflect filtered result set + 301 legacy .html to clean URLs
filterProducts() shared by /api/products and /api/facets so each facet chip
counts over the set narrowed by the OTHER active facets (drill-down), and
total reflects the fully-filtered set instead of the whole catalog. Add a
301 redirect from legacy /foo.html to the clean /foo path before static.
---
server.js | 42 +++++++++++++++++++++++++++++++++---------
1 file changed, 33 insertions(+), 9 deletions(-)
diff --git a/server.js b/server.js
index 8d2ee44..90adb0d 100644
--- a/server.js
+++ b/server.js
@@ -166,25 +166,42 @@ app.use((req, res, next) => {
if (/\.bak|\.pre-/i.test(req.path)) return res.status(404).send('Not found');
next();
});
+
+// Clean-URL canonicalization — 301 the legacy .html form to the clean path
+// BEFORE express.static can serve the .html (keeps one canonical URL for SEO).
+const INFO_PAGES = ['care', 'history', 'sourcing', 'trade', 'vocabulary'];
+app.get(/^\/(index|care|history|sourcing|trade|vocabulary)\.html$/i, (req, res) => {
+ const name = req.params[0].toLowerCase();
+ res.redirect(301, name === 'index' ? '/' : '/' + name);
+});
+
app.use(express.static(path.join(__dirname, 'public')));
// Clean-URL routes for the static info pages (nav links use extension-less paths).
-const INFO_PAGES = ['care', 'history', 'sourcing', 'trade', 'vocabulary'];
for (const name of INFO_PAGES) {
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
+// 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, 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);
@@ -204,12 +221,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 }));
← df227d1 Pass noopener,noreferrer to sister-site constellation window
·
back to Suedewallpaper
·
Remove Big Red widget — never UX-worked, collided with Help 37ba366 →