← back to Restaurantwallpaper
/api/facets: drill-down counts over active filtered set (shared filterProducts)
19a66dd5a65920be79ce00767344e336e05b258c · 2026-06-01 07:45:15 -0700 · Steve Abrams
Files touched
Diff
commit 19a66dd5a65920be79ce00767344e336e05b258c
Author: Steve Abrams <steve@designerwallcoverings.com>
Date: Mon Jun 1 07:45:15 2026 -0700
/api/facets: drill-down counts over active filtered set (shared filterProducts)
---
server.js | 45 ++++++++++++++++++++++++++++++++++++---------
1 file changed, 36 insertions(+), 9 deletions(-)
diff --git a/server.js b/server.js
index 001b7ff..9f75101 100644
--- a/server.js
+++ b/server.js
@@ -167,17 +167,38 @@ for (const [route, file] of Object.entries(CLEAN_URLS)) {
app.get(route, (req, res) => res.sendFile(path.join(__dirname, 'public', file)));
}
+// Canonicalize legacy /*.html → clean URLs (301) BEFORE express.static, so the
+// static dir doesn't serve both forms (duplicate content). Nav links are clean.
+const HTML_TO_CLEAN = {
+ '/index.html': '/',
+ '/history.html': '/history',
+ '/care.html': '/care',
+ '/trade.html': '/trade',
+ '/sourcing.html': '/sourcing',
+};
+for (const [legacy, clean] of Object.entries(HTML_TO_CLEAN)) {
+ app.get(legacy, (req, res) => res.redirect(301, clean));
+}
+
app.use(express.static(path.join(__dirname, 'public')));
+// Shared filter — apply any subset of the active facets (q/aesthetic/vendor).
+// `skip` lets a facet-count pass exclude its OWN dimension so each facet's
+// counts reflect the set narrowed by the OTHER active facets (drill-down).
+function filterProducts(list, { q, aesthetic, vendor } = {}, skip = '') {
+ let out = list;
+ if (q && skip !== 'q') {
+ const needle = String(q).toLowerCase();
+ out = out.filter(p => (p.title || '').toLowerCase().includes(needle) || (p.tags || []).some(t => String(t).toLowerCase().includes(needle)));
+ }
+ if (aesthetic && aesthetic !== 'all' && skip !== 'aesthetic') out = out.filter(p => p.aesthetic === aesthetic);
+ if (vendor && vendor !== 'all' && skip !== 'vendor') out = out.filter(p => p.vendor === vendor);
+ return out;
+}
+
app.get('/api/products', (req, res) => {
const { q, aesthetic, vendor, page = 1, limit = 24, sort = 'newest'} = req.query;
- 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)));
- }
- if (aesthetic && aesthetic !== 'all') list = list.filter(p => p.aesthetic === aesthetic);
- if (vendor && vendor !== 'all') list = list.filter(p => p.vendor === vendor);
+ let list = filterProducts(PRODUCTS_NICHE, { q, aesthetic, vendor });
list = sortProducts(list, sort);
const total = list.length;
const pageNum = Math.max(1, parseInt(page) || 1);
@@ -197,12 +218,18 @@ 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 counted over the set narrowed by the OTHER active facets.
+ for (const p of filterProducts(PRODUCTS_NICHE, { q, aesthetic, vendor }, 'aesthetic')) {
aesthetics[p.aesthetic] = (aesthetics[p.aesthetic] || 0) + 1;
+ }
+ for (const p of filterProducts(PRODUCTS_NICHE, { q, aesthetic, vendor }, 'vendor')) {
vendors[p.vendor] = (vendors[p.vendor] || 0) + 1;
}
- res.json({ aesthetics, vendors, total: PRODUCTS_NICHE.length });
+ // total = fully-filtered result-set count
+ const total = filterProducts(PRODUCTS_NICHE, { 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 }));
← 27313b3 fix: remove leaked .bak file from public/, fix bak-guard reg
·
back to Restaurantwallpaper
·
noopener,noreferrer on sister-site constellation window.open f6b3e22 →