openvibers/openvibe.vip Commits

Pages: give guests the shared short public cache (#202)

OP OpenVibers committed 6 days ago · fdce15ce3b · 1 file, +7 −0

No page route set a cache policy, so every VIP page arrived with no
Cache-Control: a browser could not reuse the home page it had just painted and
every visit paid a full round trip. The shared send() now sets the
openvibe-shared/cache-policy HTML headers -- public, max-age=60,
stale-while-revalidate=3600 for a signed-out visitor, private, no-store when a
viewer is signed in -- and leaves any policy a route already set alone.

Changes

ddiff --git a/server/web/routes.js b/server/web/routes.js
iindex 81730e7..5d77b97 100644
---- a/server/web/routes.js
++++ b/server/web/routes.js
@@@ -14,6 +14,7 @@
* domain calls as the API and redirects back with a notice (?ok= / ?error=).
*/
const crypto = require('crypto');
++const cache = require('openvibe-shared/cache-policy');
const ovServe = require('openvibe-shared/serve');
const frame = require('openvibe-shared/frame');
const seo = require('openvibe-shared/seo');
@@@ -63,6 +64,12 @@ function createWebRoutes({ domain, config, layout, userAuth, liveNow }) {
if (o && o.canonicalPath && !String(o.robots || 'index,follow').includes('noindex')) {
res.set('Link', require('openvibe-shared/agent-layer').linkHeaderTags(layout.abs(o.canonicalPath), `${String(config.baseUrl).replace(/\/+$/, '')}/llms.txt`).header);
}
++ // The same page for every signed-out visitor: a minute of reuse, then a stale copy while the
++ // new one is fetched. A page rendered for a signed-in person names them, so it stays private.
++ if (!res.get('Cache-Control')) {
++ res.vary('Cookie');
++ res.set('Cache-Control', cache.htmlHeaders(o && o.viewer ? { private: true } : { maxAge: 60 }));
++ }
return res.status(status).type('html').send(layout.page(o));
};
const dollars = (cents) => `$${(cents / 100).toFixed(2)}`;