tibi-docs/api/hooks/ssr/get_read.js

179 lines
4.8 KiB
JavaScript
Raw Normal View History

2024-04-10 10:26:40 +02:00
const utils = require("../lib/utils")
2023-11-15 08:00:12 +01:00
2024-04-10 10:26:40 +02:00
const { release } = require("../config-client")
2023-02-21 14:00:56 +01:00
2024-04-10 10:26:40 +02:00
const { ssrValidatePath } = require("../config")
const { ssrRequest } = require("../lib/ssr-server")
2023-02-21 14:00:56 +01:00
;(function () {
2024-04-10 10:26:40 +02:00
var request = context.request()
2023-02-21 14:00:56 +01:00
2024-04-10 10:26:40 +02:00
var trackingCall = request.header("x-ssr-skip")
if (trackingCall) {
// skip tracking
throw {
status: parseInt(trackingCall),
html: "",
log: false,
}
}
let url = request.query("url") || request.header("x-ssr-url") || "/"
2023-11-15 08:00:12 +01:00
const noCache = request.query("noCache")
2023-02-21 14:00:56 +01:00
2023-11-15 08:00:12 +01:00
const trace_id = context.debug.sentryTraceId()
2024-04-10 10:26:40 +02:00
/**
* @param {string} content
*/
2023-02-21 14:00:56 +01:00
function addSentryTrace(content) {
return content.replace("</head>", '<meta name="sentry-trace" content="' + trace_id + '" /></head>')
}
2024-04-10 10:26:40 +02:00
context.response.header("sentry-trace", trace_id)
const auth = context.user.auth()
if (auth && auth.role == 0) {
} else if (url) {
var comment = ""
2023-02-21 14:00:56 +01:00
url = url.split("?")[0]
comment += "url: " + url
if (url && url.length > 1) {
url = url.replace(/\/$/, "")
}
2024-04-10 10:26:40 +02:00
if (url == "/index" || !url) {
2023-02-21 14:00:56 +01:00
url = "/" // see .htaccess
}
2024-04-10 10:26:40 +02:00
function useCache(/** @type {string} */ _url) {
var cache =
!noCache &&
context.db.find("ssr", {
filter: {
path: _url,
},
})
if (cache && cache.length) {
// use cache
context.response.header("X-SSR-Cache", "true")
throw {
status: 200,
log: false,
html: addSentryTrace(cache[0].content),
}
2023-02-21 14:00:56 +01:00
}
}
// validate url
2024-04-10 10:26:40 +02:00
var status = 200
2023-02-21 14:00:56 +01:00
2024-04-10 10:26:40 +02:00
var pNorender = false
var pNotfound = false
2023-02-21 14:00:56 +01:00
2023-11-15 08:00:12 +01:00
const pR = ssrValidatePath(url)
2024-04-10 10:26:40 +02:00
if (pR === -1) {
2023-02-21 14:00:56 +01:00
pNotfound = true
2024-04-10 10:26:40 +02:00
comment += ", notFound"
2023-02-21 14:00:56 +01:00
} else if (!pR) {
pNorender = true
2024-04-10 10:26:40 +02:00
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)
2023-02-21 14:00:56 +01:00
}
2023-11-15 08:00:12 +01:00
let head = ""
let html = ""
let error = ""
2023-02-21 14:00:56 +01:00
comment += ", path: " + url
2024-04-10 10:26:40 +02:00
var cacheIt = false
2023-02-21 14:00:56 +01:00
if (pNorender) {
html = "<!-- NO SSR RENDERING -->"
} else if (pNotfound) {
status = 404
html = "404 NOT FOUND"
} else {
2023-11-15 08:00:12 +01:00
// @ts-ignore
context.ssrCache = {}
// @ts-ignore
context.ssrRequest = ssrRequest
2023-02-21 14:00:56 +01:00
try {
2024-04-10 10:26:40 +02:00
// if error, output plain html without prerendering
2023-02-21 14:00:56 +01:00
// @ts-ignore
2023-11-15 08:00:12 +01:00
const app = require("../lib/app.server")
2024-04-10 10:26:40 +02:00
2023-11-15 08:00:12 +01:00
const rendered = app.default.render({
2023-02-21 14:00:56 +01:00
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
}
2024-04-10 10:26:40 +02:00
} catch (/** @type {any} */ e) {
utils.log(e.message)
utils.log(e.stack)
2023-02-21 14:00:56 +01:00
error = "error: " + e.message + "\n\n" + e.stack
2024-04-10 10:26:40 +02:00
// utils.log(e)
// for (var property in e) {
// utils.log(property + ": " + e[property])
// }
// error = JSON.stringify(e)
2023-02-21 14:00:56 +01:00
}
}
2024-04-10 10:26:40 +02:00
var tpl = context.fs.readFile("templates/spa.html")
2023-02-21 14:00:56 +01:00
tpl = tpl.replace("<!--HEAD-->", head)
tpl = tpl.replace("<!--HTML-->", html)
tpl = tpl.replace("<!--SSR.ERROR-->", error ? "<!--" + error + "-->" : "")
tpl = tpl.replace("<!--SSR.COMMENT-->", comment ? "<!--" + comment + "-->" : "")
if (cacheIt && !noCache) {
2024-04-10 10:26:40 +02:00
// save cache
2023-02-21 14:00:56 +01:00
context.db.create("ssr", {
path: url,
content: tpl,
})
}
throw {
status: status,
log: false,
html: addSentryTrace(tpl),
}
} else {
2024-04-10 10:26:40 +02:00
// only admins are allowed
throw {
status: 403,
message: "invalid auth",
auth: auth,
release: release,
2023-02-21 14:00:56 +01:00
}
}
})()