openvibers/openvibe.coupons Commits
The item pages the sitemap lists have the Markdown twins they name (#218)
Changes
@@@ -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 };}nnew file mode 100644@@@ -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); });
@@ -5,6 +5,9 @@
@@ -5,6 +5,9 @@
// page renders exactly as it does for a visitor who is not signed in - the same router, the same builders
// 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.
// 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']);
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. */
/** A response the page code can write to; the first body written is the twin's HTML. */
function mockRes() {
function mockRes() {
@@ -53,7 +56,7 @@ function mockReq(path, query) {
@@ -53,7 +56,7 @@ function mockReq(path, query) {
function renderWithRouter(router, path, query) {
function renderWithRouter(router, path, query) {
return new Promise((resolve) => {
return new Promise((resolve) => {
const res = mockRes();
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;
res.send = keep; res.end = keep; res.json = keep;
try {
try {
router.handle(mockReq(path, query), res, () => resolve(null));
router.handle(mockReq(path, query), res, () => resolve(null));
@@ -66,9 +69,13 @@ function renderWithRouter(router, path, query) {
@@ -66,9 +69,13 @@ function renderWithRouter(router, path, query) {
function createAgentPages({ router }) {
function createAgentPages({ router }) {
async function renderPublicPage(path) {
async function renderPublicPage(path) {
const p = String(path || '/').replace(/\/+$/, '') || '/';
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, '');
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 };
return { renderPublicPage };
}
}
new file mode 100644
new file mode 100644
@@ -0,0 +1,25 @@
@@ -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); });