Template
deploy to production / deploy (push) Successful in 43s
Der SSR-Hook koordiniert Renders jetzt über den Server-Lock statt sich auf
den Unique-Index zu verlassen: acquire("ssr:render:"+url, {waitMs:15000}),
danach Cache-Doppelprüfung, Schreiben nur durch den Lock-Inhaber und
Freigabe erst nach dem Commit des Cache-Eintrags.
Wer den Lock nicht bekommt, rendert als Rückfallebene ohne Cache-Schreiben;
ein älterer Server ohne context.lock fällt ohne Fehler auf das bisherige
Verhalten zurück.
tibi-types: LockPackage (4e13d4a).
314 lines
11 KiB
JavaScript
314 lines
11 KiB
JavaScript
const utils = require("../lib/utils")
|
||
|
||
const { release } = require("../config-client")
|
||
|
||
const { ssrValidatePath } = require("../config")
|
||
const { ssrRequest } = require("../lib/ssr-server")
|
||
const APP_SERVER_MODULE_PATH = "../lib/app.server"
|
||
|
||
// Upper bound for waiting on another renderer of the same URL. Long enough to
|
||
// cover a normal render, short enough that a visitor does not sit in the
|
||
// request for the whole hook timeout.
|
||
const SSR_LOCK_WAIT_MS = 15000
|
||
;(function () {
|
||
var request = context.request()
|
||
|
||
var trackingCall = request.header("x-ssr-skip")
|
||
if (trackingCall) {
|
||
// skip tracking
|
||
// no cache header
|
||
context.response.header("Cache-Control", "no-cache, no-store, must-revalidate")
|
||
throw {
|
||
status: parseInt(trackingCall),
|
||
html: "",
|
||
log: false,
|
||
}
|
||
}
|
||
|
||
let url = request.query("url") || request.header("x-ssr-url") || "/"
|
||
const noCache = request.query("noCache")
|
||
|
||
const trace_id = context.debug.sentryTraceId()
|
||
/**
|
||
* @param {string} content
|
||
*/
|
||
function addSentryTrace(content) {
|
||
return content.replace("</head>", '<meta name="sentry-trace" content="' + trace_id + '" /></head>')
|
||
}
|
||
|
||
context.response.header("sentry-trace", trace_id)
|
||
const auth = context.user.auth()
|
||
if (auth && auth.role == 0) {
|
||
} else if (url) {
|
||
/** @type {Date} */ // @ts-ignore
|
||
context.ssrCacheValidUntil = null
|
||
|
||
var comment = ""
|
||
|
||
url = url.split("?")[0]
|
||
comment += "url: " + url
|
||
|
||
if (url && url.length > 1) {
|
||
url = url.replace(/\/$/, "")
|
||
}
|
||
if (url == "/index" || !url) {
|
||
url = "/" // see .htaccess
|
||
}
|
||
|
||
function useCache(/** @type {string} */ _url) {
|
||
var cache =
|
||
!noCache &&
|
||
context.db.find("ssr", {
|
||
filter: {
|
||
path: _url,
|
||
},
|
||
})
|
||
|
||
if (cache && cache.length) {
|
||
// check that entry is still allowed to be published
|
||
const validUntil = cache[0].validUntil ? new Date(cache[0].validUntil.unixMilli()) : null
|
||
|
||
if (!validUntil || validUntil > new Date()) {
|
||
// use cache
|
||
context.response.header("X-SSR-Cache", "true")
|
||
throw {
|
||
status: 200,
|
||
log: false,
|
||
html: addSentryTrace(cache[0].content),
|
||
}
|
||
} else {
|
||
// cache is invalid, delete it
|
||
context.response.header("X-SSR-Cache", "invalid")
|
||
context.db.delete("ssr", cache[0].id)
|
||
}
|
||
}
|
||
}
|
||
|
||
// validate url
|
||
var status = 200
|
||
|
||
var pNorender = false
|
||
var pNotfound = false
|
||
|
||
const pR = ssrValidatePath(url)
|
||
if (pR === -1) {
|
||
pNotfound = true
|
||
comment += ", notFound"
|
||
} else if (!pR) {
|
||
pNorender = true
|
||
comment += ", noRender"
|
||
} else if (typeof pR === "string") {
|
||
url = pR
|
||
comment += ", cache url: " + url
|
||
}
|
||
|
||
if (noCache) {
|
||
comment += ", noCache"
|
||
}
|
||
|
||
if (!pNorender && !pNotfound) {
|
||
// check if we have a cache
|
||
useCache(url)
|
||
}
|
||
|
||
let head = ""
|
||
let html = ""
|
||
let error = ""
|
||
|
||
comment += ", path: " + url
|
||
|
||
var cacheIt = false
|
||
// Cross-request coordination for this URL: one renderer at a time.
|
||
// lockKey stays empty when no lock is taken (noCache/old server).
|
||
var lockKey = ""
|
||
var lockHeld = false
|
||
var skipCacheWrite = false
|
||
if (pNorender) {
|
||
html = "<!-- NO SSR RENDERING -->"
|
||
} else if (pNotfound) {
|
||
status = 404
|
||
html = "404 NOT FOUND"
|
||
} else {
|
||
if (!noCache) {
|
||
// Coordinate renders across requests. Without this, a cache
|
||
// invalidation makes every concurrent request render and write
|
||
// the same page; the DB unique index then turns the losers into
|
||
// duplicate-key errors and wasted CPU.
|
||
lockKey = "ssr:render:" + url
|
||
try {
|
||
const lock = context.lock.acquire(lockKey, { waitMs: SSR_LOCK_WAIT_MS })
|
||
if (lock.acquired) {
|
||
lockHeld = true
|
||
// The previous owner may have populated the cache while
|
||
// we waited — never render a page that already exists.
|
||
useCache(url)
|
||
comment += ", lock"
|
||
} else {
|
||
// Timed out: somebody is still working on this URL.
|
||
skipCacheWrite = true
|
||
useCache(url)
|
||
comment += ", lockTimeout"
|
||
}
|
||
} catch (/** @type {any} */ e) {
|
||
// useCache throws its hook response: never swallow that.
|
||
if (lockHeld || skipCacheWrite) throw e
|
||
// context.lock is not available (older server) or acquire
|
||
// failed. Render as before, without coordination.
|
||
utils.log("ssr lock: " + (e && e.message ? e.message : e))
|
||
lockKey = ""
|
||
comment += ", lockUnavailable"
|
||
}
|
||
}
|
||
|
||
// @ts-ignore
|
||
context.ssrCache = {}
|
||
// @ts-ignore – tracks dependencies as { "col:id": true, "col:*": true }
|
||
context.ssrDeps = {}
|
||
// @ts-ignore
|
||
context.ssrRequest = ssrRequest
|
||
|
||
try {
|
||
// if error, output plain html without prerendering
|
||
// @ts-ignore
|
||
const app = require(APP_SERVER_MODULE_PATH)
|
||
|
||
const rendered = app.default.render({
|
||
url: url,
|
||
})
|
||
head = rendered.head
|
||
html = rendered.html
|
||
|
||
head +=
|
||
"\n\n" +
|
||
"<script>window.__SSR_CACHE__ = " +
|
||
// @ts-ignore
|
||
JSON.stringify(context.ssrCache) +
|
||
"</script>"
|
||
|
||
// status from webapp
|
||
// @ts-ignore
|
||
if (context.is404) {
|
||
status = 404
|
||
} else {
|
||
cacheIt = true
|
||
}
|
||
} catch (/** @type {any} */ e) {
|
||
utils.log(e.message)
|
||
utils.log(e.stack)
|
||
error = "error: " + e.message + "\n\n" + e.stack
|
||
|
||
// utils.log(e)
|
||
// for (var property in e) {
|
||
// utils.log(property + ": " + e[property])
|
||
// }
|
||
// error = JSON.stringify(e)
|
||
}
|
||
}
|
||
|
||
var tpl = context.fs.readFile("templates/spa.html")
|
||
|
||
// Extract language from URL for <html lang="..."> (before other replacements)
|
||
var langMatch = url.match(/^\/(de|en)(\/|$)/)
|
||
var pageLang = langMatch ? langMatch[1] : "de"
|
||
tpl = tpl.replace(/<html lang="[^"]*">/, '<html lang="' + pageLang + '">')
|
||
|
||
tpl = tpl.replace("<!--HEAD-->", head)
|
||
tpl = tpl.replace("<!--HTML-->", html)
|
||
tpl = tpl.replace("<!--SSR.ERROR-->", error ? "<!--" + error + "-->" : "")
|
||
|
||
// Deduplicate deps: if "col:*" exists, drop all "col:<id>" for that collection
|
||
// @ts-ignore – ssrDeps is set dynamically above
|
||
var depsKeys = context.ssrDeps ? Object.keys(context.ssrDeps) : []
|
||
/** @type {{[key: string]: boolean}} */
|
||
var wildcardCols = {}
|
||
depsKeys.forEach(function (k) {
|
||
if (k.endsWith(":*")) wildcardCols[k.split(":")[0]] = true
|
||
})
|
||
depsKeys = depsKeys.filter(function (k) {
|
||
if (k.endsWith(":*")) return true
|
||
return !wildcardCols[k.split(":")[0]]
|
||
})
|
||
|
||
// @ts-ignore – append deps for debugging
|
||
comment += ", deps: [" + depsKeys.join(", ") + "]"
|
||
tpl = tpl.replace("<!--SSR.COMMENT-->", comment ? "<!--" + comment + "-->" : "")
|
||
|
||
if (cacheIt && !noCache) {
|
||
// save cache
|
||
//
|
||
// `context.db.create` ist ein Insert, kein Upsert. Nach einer
|
||
// Invalidierung rendern mehrere Anfragen dieselbe URL gleichzeitig,
|
||
// und alle schreiben denselben Eintrag. Der Unique-Index auf `path`
|
||
// lässt genau einen gewinnen; die Verlierer bekommen "duplicate
|
||
// key".
|
||
//
|
||
// Das wird hier bewusst geschluckt. Der Eintrag existiert dann
|
||
// schon — der Gewinner hat ihn geschrieben, es fehlt nichts. Und
|
||
// die Seite ist ohnehin fertig gerendert, `tpl` steht bereit.
|
||
//
|
||
// Wichtig ist das aus zwei Gründen: Ein durchschlagender Fehler
|
||
// liefert eine gute Seite als HTTP 500 aus, und ein
|
||
// 500-Dokument hat kein #appContainer — die Seite bleibt beim
|
||
// Besucher dann für immer leer, weil hydrate() nichts zum
|
||
// Einhängen findet.
|
||
//
|
||
// Seit context.lock rendert pro URL nur noch ein Arbeiter. Wer den
|
||
// Lock nicht bekommt (Timeout), rendert zwar als Rückfallebene,
|
||
// schreibt aber nicht: ein zweiter Schreiber würde nur wieder am
|
||
// Unique-Index scheitern.
|
||
if (skipCacheWrite) {
|
||
comment += ", cacheWriteSkipped"
|
||
} else {
|
||
try {
|
||
context.db.create("ssr", {
|
||
path: url,
|
||
content: tpl,
|
||
// @ts-ignore
|
||
validUntil: context.ssrCacheValidUntil,
|
||
// dependency strings: "col:id" for detail, "col:*" for list (deduplicated)
|
||
dependencies: depsKeys,
|
||
})
|
||
} catch (e) {
|
||
utils.log("ssr cache: Eintrag für " + url + " wurde parallel geschrieben, Anfrage ohne eigenen Cache-Eintrag")
|
||
} finally {
|
||
// Hand over only after the write is committed, so a waiter
|
||
// that acquires next actually sees the entry.
|
||
if (lockHeld) {
|
||
try {
|
||
context.lock.release(lockKey)
|
||
} catch (/** @type {any} */ e) {
|
||
utils.log("ssr lock release: " + (e && e.message ? e.message : e))
|
||
}
|
||
lockHeld = false
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
// The page was not cached (404, render error, noCache or lock timeout):
|
||
// free the claim before the response is thrown.
|
||
if (lockHeld) {
|
||
try {
|
||
context.lock.release(lockKey)
|
||
} catch (/** @type {any} */ e) {
|
||
utils.log("ssr lock release: " + (e && e.message ? e.message : e))
|
||
}
|
||
lockHeld = false
|
||
}
|
||
|
||
throw {
|
||
status: status,
|
||
log: false,
|
||
html: addSentryTrace(tpl),
|
||
}
|
||
} else {
|
||
// only admins are allowed
|
||
throw {
|
||
status: 403,
|
||
message: "invalid auth",
|
||
auth: auth,
|
||
release: release,
|
||
}
|
||
}
|
||
})()
|