openvibers/openvibe.markets Commits

Our own files by their content (#2)

OP OpenVibers committed 13 days ago · 8aaeee09bd · 2 files, +18 −1

/x.css?v=<first 10 hex of its sha256>, read once at boot. The stylesheet was served for an hour from the
browser and from Cloudflare after a deploy (cache-control max-age=3600, cf-cache-status HIT), so a page's new
look arrived up to an hour late. Now a changed file is a new URL.

Changes

ddiff --git a/server/views.js b/server/views.js
iindex 7bc9643..48a3885 100644
---- a/server/views.js
++++ b/server/views.js
@@@ -7,6 +7,22 @@ const shell = require('openvibe-shared/shell');
const serve = require('openvibe-shared/serve');
const { PLACES } = require('./elsewhere');
++// Our own files by their content (a release's change is a new address, so neither a browser nor Cloudflare keeps an old
++// one for the hour they may cache it): /x.css?v=<first 10 hex of its sha256>, read once at boot.
++const asset = (() => {
++ const fs = require('node:fs');
++ const crypto = require('node:crypto');
++ const kept = new Map();
++ return (name) => {
++ if (!kept.has(name)) {
++ let v = 'dev';
++ try { v = crypto.createHash('sha256').update(fs.readFileSync(require('node:path').join(__dirname, '..', 'public', name))).digest('hex').slice(0, 10); } catch { /* missing in a test: unversioned */ }
++ kept.set(name, `/${name}?v=${v}`);
++ }
++ return kept.get(name);
++ };
++})();
++
const SITE = 'https://openvibe.markets';
const esc = (v) => String(v == null ? '' : v).replace(/[&<>"']/g, (c) => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;' }[c]));
const money = (n, currency = 'USD') => {
@@@ -28,7 +44,7 @@ function page({ title, body, path = '/', description = 'Every marketplace, one s
name: 'OpenVibe.Markets', siteName: 'OpenVibe.Markets', title, description, canonical: `${SITE}${path}`, robots,
navbar: { service: 'markets', apiBase: 'https://openvibe.network', links: [{ label: 'Search', href: '/' }, { label: 'Deals', href: 'https://openvibe.deals/' }] },
footer: { service: 'markets', variant: 'full', mount: '#ov-footer' },
-- head: `<link rel="stylesheet" href="${serve.url('kit.css')}"><link rel="stylesheet" href="/markets.css">`,
++ head: `<link rel="stylesheet" href="${serve.url('kit.css')}"><link rel="stylesheet" href="${asset('markets.css')}">`,
body: `<div id="navbar-mount"></div><main id="main" class="ovk-page mk">${body}</main>`,
});
}
ddiff --git a/test/app.test.js b/test/app.test.js
iindex 9db91af..257ac71 100644
---- a/test/app.test.js
++++ b/test/app.test.js
@@@ -53,6 +53,7 @@ const OFFERS = [
assert.match(r.text, /Facebook Marketplace/);
assert.match(r.text, /data-ov-trending="own"/, 'a product search keeps its own examples, never the page\'s headings');
assert.match(r.headers.get('cache-control'), /public/);
++ assert.match(r.text, /href="\/markets\.css\?v=[0-9a-f]{10}"/, 'our stylesheet by its content: a release is a new address');
r = await get('/search?q=headphones');
assert.strictEqual(r.status, 200);