openvibers/openvibe.reviews Commits
The item pages the sitemap lists have the Markdown twins they name (#210)
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', '/everywhere']);++// The item pages the sitemap lists (places at /e/<slug>) 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 = /^\/e\/[a-z0-9-]+$/;/** 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 === '/e/alice-126-west-13-street-manhattan-ny') return res.send(page('index, follow'));++ if (req.path === '/e/hidden-place') return res.send(page('noindex, follow'));++ if (req.path === '/e/gone') return res.status(404).send(page('noindex'));++ return next();++ },++};++++(async () => {++ const { renderPublicPage } = createAgentPages({ router });++ const got = await renderPublicPage('/e/alice-126-west-13-street-manhattan-ny');++ assert.ok(got && /An item/.test(got.html), 'a public item page is twinned');++ assert.strictEqual(await renderPublicPage('/e/hidden-place'), null, 'a noindex item has no twin');++ assert.strictEqual(await renderPublicPage('/e/gone'), null, 'a missing item (404) has no twin');++ assert.strictEqual(await renderPublicPage('/r/abc'), 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', '/everywhere']);
const PATHS = new Set(['/', '/about', '/everywhere']);
+// The item pages the sitemap lists (places at /e/<slug>) 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 = /^\/e\/[a-z0-9-]+$/;
/** 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 === '/e/alice-126-west-13-street-manhattan-ny') return res.send(page('index, follow'));
+ if (req.path === '/e/hidden-place') return res.send(page('noindex, follow'));
+ if (req.path === '/e/gone') return res.status(404).send(page('noindex'));
+ return next();
+ },
+};
+
+(async () => {
+ const { renderPublicPage } = createAgentPages({ router });
+ const got = await renderPublicPage('/e/alice-126-west-13-street-manhattan-ny');
+ assert.ok(got && /An item/.test(got.html), 'a public item page is twinned');
+ assert.strictEqual(await renderPublicPage('/e/hidden-place'), null, 'a noindex item has no twin');
+ assert.strictEqual(await renderPublicPage('/e/gone'), null, 'a missing item (404) has no twin');
+ assert.strictEqual(await renderPublicPage('/r/abc'), 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); });