wm-fontis-tibi-2023/api/hooks/ssr/get_read.js

159 lines
4.6 KiB
JavaScript
Raw Permalink Normal View History

2023-12-07 21:24:09 +01:00
// TODO: add query string functionality to cache
const { ssrValidatePath } = require("../config")
const { log } = require("../lib/utils")
const { ssrRequest } = require("../lib/ssr-server.js")
2023-07-13 13:12:19 +02:00
;(function () {
/** @type {HookResponse} */
2023-12-07 21:24:09 +01:00
let response = null
2023-07-13 13:12:19 +02:00
2023-12-07 21:24:09 +01:00
const request = context.request()
let url = request.query("url")
const noCache = request.query("noCache")
2023-07-13 13:12:19 +02:00
// add sentry trace id to head
2023-12-07 21:24:09 +01:00
const trace_id = context.debug.sentryTraceId()
2023-07-13 13:12:19 +02:00
function addSentryTrace(content) {
return content.replace("</head>", '<meta name="sentry-trace" content="' + trace_id + '" /></head>')
}
context.response.header("sentry-trace", trace_id)
if (url) {
// comment will be printed to html later
2023-12-07 21:24:09 +01:00
let comment = ""
/** @type {Date} */ // @ts-ignore
context.ssrCacheValidUntil = null
2023-07-13 13:12:19 +02:00
url = url.split("?")[0]
comment += "url: " + url
if (url && url.length > 1) {
url = url.replace(/\/$/, "")
}
if (url == "/noindex" || !url) {
url = "/" // see .htaccess
}
// check if url is in cache
2023-12-07 21:24:09 +01:00
/** @type {Ssr[]} */ // @ts-ignore
const cache =
2023-07-13 13:12:19 +02:00
!noCache &&
context.db.find("ssr", {
filter: {
path: url,
},
})
if (cache && cache.length) {
// use cache
2023-12-07 21:24:09 +01:00
context.response.header("X-SSR-Cache", "true")
2023-07-13 13:12:19 +02:00
throw {
status: 200,
log: false,
html: addSentryTrace(cache[0].content),
}
}
// validate url
2023-12-07 21:24:09 +01:00
let status = 200
2023-07-13 13:12:19 +02:00
2023-12-07 21:24:09 +01:00
let pNorender = false
let pNotfound = false
2023-07-13 13:12:19 +02:00
2023-12-07 21:24:09 +01:00
const pR = ssrValidatePath(url)
2023-07-13 13:12:19 +02:00
if (pR < 0) {
pNotfound = true
} else if (!pR) {
pNorender = true
}
2023-12-07 21:24:09 +01:00
let head = ""
let html = ""
let error = ""
2023-07-13 13:12:19 +02:00
comment += ", path: " + url
2023-12-07 21:24:09 +01:00
let cacheIt = false
2023-07-13 13:12:19 +02:00
if (pNorender) {
html = "<!-- NO SSR RENDERING -->"
} else if (pNotfound) {
status = 404
html = "404 NOT FOUND"
} else {
2023-12-07 21:24:09 +01:00
// @ts-ignore
context.ssrCache = {}
// @ts-ignore
context.ssrRequest = ssrRequest
2023-07-13 13:12:19 +02:00
// try rendering, if error output plain html
try {
// include App.svelte and render it
// @ts-ignore
2023-12-07 21:24:09 +01:00
// console.log("####### RENDERING ", url)
const app = require("../lib/app.server")
const rendered = app.default.render({
2023-07-13 13:12:19 +02:00
url: url,
})
head = rendered.head
html = rendered.html
2023-12-07 21:24:09 +01:00
// add ssrCache to head, cache is built in ssr.js/apiRequest
2023-07-13 13:12:19 +02:00
head +=
"\n\n" +
"<script>window.__SSR_CACHE__ = " +
// @ts-ignore
JSON.stringify(context.ssrCache) +
"</script>"
// status from webapp
// @ts-ignore
if (context.is404) {
2023-12-07 21:24:09 +01:00
// console.log("########## 404")
2023-07-13 13:12:19 +02:00
status = 404
} else {
cacheIt = true
}
} catch (e) {
// save error for later insert into html
log(e.message)
log(e.stack)
error = "error: " + e.message + "\n\n" + e.stack
}
}
// read html template and replace placeholders
2023-12-07 21:24:09 +01:00
let tpl = context.fs.readFile("templates/spa.html")
2023-07-13 13:12:19 +02: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 + "-->" : "")
// save cache if adviced
if (cacheIt && !noCache) {
context.db.create("ssr", {
2023-12-07 21:24:09 +01:00
// context.debug.dump("ssr", {
2023-07-13 13:12:19 +02:00
path: url,
content: tpl,
})
}
// return html
throw {
status: status,
log: false,
html: addSentryTrace(tpl),
}
} else {
// only admins are allowed to get without url parameter
2023-12-07 21:24:09 +01:00
const auth = context.user.auth()
2023-07-13 13:12:19 +02:00
if (!auth || auth.role !== 0) {
throw {
status: 403,
message: "invalid auth",
auth: auth,
}
}
}
})()