openvibers/openvibe.coupons Commits

The item pages the sitemap lists have the Markdown twins they name (#218)

OP OpenVibers committed 10 days ago · c2e8f35204 · 2 files, +35 −3

Every page names a twin in its head and Link header, but only a few fixed paths were twinned, so the item pages'
twins answered 404 (11-03, tools/twin-named-check.js). Item pages are twinned now, when they render 200 with a robots
tag that says index; a missing, hidden or noindex item has none.

Changes

ddiff --git a/server/agent-pages.js b/server/agent-pages.js
iindex 4909ce7..fc04985 100644
---- a/server/agent-pages.js
++++ b/server/agent-pages.js
@@@ -5,6 +5,9 @@
// page renders exactly as it does for a visitor who is not signed in - the same router, the same builders
// and the same data. A page behind a person's own token (a watch, a review, an editor) has no twin.
const PATHS = new Set(['/', '/about', '/companion', '/updates']);
++// The item pages the sitemap lists (shops at /m/<slug>, codes at /c/<id>) have twins too: each named one in its head and Link header, and it
++// answered 404 (11-03, tools/twin-named-check.js). Only a 200 page whose robots tag says index is twinned.
++const ITEM_RE = /^\/(?:m\/[a-z0-9-]+|c\/cpn_[0-9A-HJKMNP-TV-Z]{26})$/;
/** A response the page code can write to; the first body written is the twin's HTML. */
function mockRes() {
@@@ -53,7 +56,7 @@ function mockReq(path, query) {
function renderWithRouter(router, path, query) {
return new Promise((resolve) => {
const res = mockRes();
-- const keep = (body) => { resolve(typeof body === 'string' ? body : null); return res; };
++ const keep = (body) => { resolve(typeof body === 'string' && res.statusCode === 200 ? body : null); return res; };
res.send = keep; res.end = keep; res.json = keep;
try {
router.handle(mockReq(path, query), res, () => resolve(null));
@@@ -66,9 +69,13 @@ function renderWithRouter(router, path, query) {
function createAgentPages({ router }) {
async function renderPublicPage(path) {
const p = String(path || '/').replace(/\/+$/, '') || '/';
-- if (!PATHS.has(p)) return null;
++ const item = ITEM_RE.test(p);
++ if (!PATHS.has(p) && !item) return null;
const html = await renderWithRouter(router, p, '');
-- return html ? { html, title: null, updated: null } : null;
++ if (!html) return null;
++ // An item that is not public (taken down, unlisted, noindex) has no twin, whatever its address.
++ if (item && !/<meta\s+name=["']robots["']\s+content=["']index/i.test(html)) return null;
++ return { html, title: null, updated: null };
}
return { renderPublicPage };
}
ddiff --git a/test/agent-pages-items.test.js b/test/agent-pages-items.test.js
nnew file mode 100644
iindex 0000000..6581fb6
---- /dev/null
++++ b/test/agent-pages-items.test.js
@@@ -0,0 +1,25 @@
++'use strict';
++// The item pages the sitemap lists have Markdown twins (11-03: each named one that answered 404). Only a 200 page whose
++// robots tag says index is twinned; an unknown shape, a missing item and a noindex item have none.
++const assert = require('assert');
++const { createAgentPages } = require('../server/agent-pages');
++
++const page = (robots) => `<!doctype html><html><head><title>T</title><meta name="robots" content="${robots}"></head><body><main><h1>An item</h1></main></body></html>`;
++const router = {
++ handle(req, res, next) {
++ if (req.path === '/m/amazon') return res.send(page('index, follow'));
++ if (req.path === '/c/cpn_01M6F12PJ5GH6ZM330823NYSAY') return res.send(page('noindex, follow'));
++ if (req.path === '/m/gone') return res.status(404).send(page('noindex'));
++ return next();
++ },
++};
++
++(async () => {
++ const { renderPublicPage } = createAgentPages({ router });
++ const got = await renderPublicPage('/m/amazon');
++ assert.ok(got && /An item/.test(got.html), 'a public item page is twinned');
++ assert.strictEqual(await renderPublicPage('/c/cpn_01M6F12PJ5GH6ZM330823NYSAY'), null, 'a noindex item has no twin');
++ assert.strictEqual(await renderPublicPage('/m/gone'), null, 'a missing item (404) has no twin');
++ assert.strictEqual(await renderPublicPage('/x/amazon'), null, 'a shape the sitemap does not list has no twin');
++ console.log('agent pages, items: ok');
++})().catch((e) => { console.error(e); process.exit(1); });