diff --git a/docs/seo.md b/docs/seo.md index 2b719fd..40b83e6 100644 --- a/docs/seo.md +++ b/docs/seo.md @@ -49,10 +49,24 @@ with it off it blocks everything. so the site still loads (with default previews) during a restart: ```nginx + # Shared by every block that sends a page to the API. + # (Or repeat these lines in each block, as nginx-dev does.) + # proxy_http_version 1.1; + # proxy_set_header Host $host; + # proxy_set_header X-Real-IP $remote_addr; + # proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + # proxy_set_header X-Forwarded-Proto $scheme; + location = /sitemap.xml { proxy_pass http://127.0.0.1:3001; - proxy_set_header Host $host; - proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + } + + # The home page needs its own block: try_files would otherwise find + # the site's folder and serve index.html without asking the API. + location = / { + proxy_pass http://127.0.0.1:3001; + proxy_intercept_errors on; + error_page 500 502 503 504 = @static; } location / { @@ -61,19 +75,30 @@ with it off it blocks everything. location @page { proxy_pass http://127.0.0.1:3001; - proxy_set_header Host $host; - proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_intercept_errors on; - error_page 502 503 504 = @static; + error_page 500 502 503 504 = @static; } location @static { try_files /index.html =404; } + + # Uploads are bigger than the /api/ block's 64k limit allows. + location = /api/admin/media { + proxy_pass http://127.0.0.1:3001; + client_max_body_size 26m; + proxy_read_timeout 60s; + } ``` Keep the existing `/api/` and `/uploads/` blocks; they're more - specific and still win. Use the port the API listens on. + specific and still win. Use the port the API listens on. A 404 + from the API (a record that doesn't exist) goes through as it is; + 500 to 504 fall back to the plain page. + + A dev or test server should also send `X-Robots-Tag: noindex, + nofollow` and its own blocking `robots.txt`, since it shares the + production build's settings: see `nginx-dev` in the repo root. 3. Check it: `curl -s https://nextgenerationofunity.org/events/ | grep og:title` should show the event's name, and Facebook's Sharing Debugger or diff --git a/server/src/routes/pages.js b/server/src/routes/pages.js index 6684c7c..7c8905c 100644 --- a/server/src/routes/pages.js +++ b/server/src/routes/pages.js @@ -18,6 +18,10 @@ the app's own not-found page with a 404 status, so search engines drop it. + If the metadata can't be built (no site.json at SITE_CONFIG, say), + the page is still served, as built, and the cause is logged once: + a preview is never worth a page that won't load. + Mounted last, after every /api and /uploads route; an unknown /api path is still the API's JSON 404. ═══════════════════════════════════════════════════════════════ */ @@ -49,12 +53,28 @@ function indexHtml() { const pages = new Hono(); -pages.get("/sitemap.xml", (c) => - c.body(sitemap(c.get("db")), 200, { +/* Warned once per cause rather than on every request, so a missing + site.json doesn't bury the log. */ +const warned = new Set(); +function warnOnce(key, message) { + if (warned.has(key)) return; + warned.add(key); + console.error(message); +} + +pages.get("/sitemap.xml", (c) => { + let xml; + try { + xml = sitemap(c.get("db")); + } catch (err) { + warnOnce(`sitemap:${err.message}`, `pages: can't build the sitemap: ${err.message}`); + return c.text("The sitemap isn't available.", 503); + } + return c.body(xml, 200, { "Content-Type": "application/xml; charset=utf-8", "Cache-Control": CACHE, - }), -); + }); +}); pages.get("*", (c, next) => { const pathname = c.req.path; @@ -70,8 +90,24 @@ pages.get("*", (c, next) => { return c.text("The site hasn't been built.", 503); } - const meta = metaFor(c.get("db"), pathname); - return c.html(renderPage(html, meta), meta.status, { + // The metadata is an extra. If it can't be built (site.json + // missing, a query failing), the page goes out as it was built, + // with the site's default tags, rather than not at all. + let meta; + let page; + try { + meta = metaFor(c.get("db"), pathname); + page = renderPage(html, meta); + } catch (err) { + warnOnce( + `meta:${err.message}`, + `pages: serving ${pathname} without its metadata: ${err.message}` + + (err.code === "ENOENT" ? " (set SITE_CONFIG to the deployed site.json)" : ""), + ); + return c.html(html, 200, { "Cache-Control": "no-store" }); + } + + return c.html(page, meta.status, { "Cache-Control": meta.status === 200 ? CACHE : "no-store", }); });