yarn package upgrades, ssr update

This commit is contained in:
2023-11-15 07:00:12 +00:00
parent f1d30945c1
commit 942f92c477
18 changed files with 1800 additions and 1392 deletions

View File

@@ -1,16 +1,23 @@
const { ssrValidatePath, ssrAllowedAPIEndpoints } = require("../config")
// TODO: add query string functionality to cache
const { ssrValidatePath } = require("../config")
const { log } = require("../lib/utils")
const { ssrRequest } = require("../lib/ssr-server")
const { obj2str, log } = require("../lib/utils")
;(function () {
/** @type {HookResponse} */
var response = null
// @ts-ignore
let response = null
var request = context.request()
var url = request.query("url")
var noCache = request.query("noCache")
const request = context.request()
let url = request.query("url")
const noCache = request.query("noCache")
// add sentry trace id to head
var trace_id = context.debug.sentryTraceId()
const trace_id = context.debug.sentryTraceId()
/**
* @param {string} content
*/
function addSentryTrace(content) {
return content.replace("</head>", '<meta name="sentry-trace" content="' + trace_id + '" /></head>')
}
@@ -18,7 +25,9 @@ const { obj2str, log } = require("../lib/utils")
if (url) {
// comment will be printed to html later
var comment = ""
let comment = ""
/** @type {Date} */ // @ts-ignore
context.ssrCacheValidUntil = null
url = url.split("?")[0]
comment += "url: " + url
@@ -31,7 +40,8 @@ const { obj2str, log } = require("../lib/utils")
}
// check if url is in cache
var cache =
/** @type {Ssr[]} */ // @ts-ignore
const cache =
!noCache &&
context.db.find("ssr", {
filter: {
@@ -39,93 +49,70 @@ const { obj2str, log } = require("../lib/utils")
},
})
if (cache && cache.length) {
// use cache
throw {
status: 200,
log: false,
html: addSentryTrace(cache[0].content),
const validUntil = cache[0].validUntil ? new Date(cache[0].validUntil.unixMilli()) : null
// context.debug.dump("cache validUntil", validUntil)
if (!validUntil || validUntil > new Date()) {
// context.debug.dump("using cache")
// 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")
// @ts-ignore
context.db.delete("ssr", cache[0].id)
}
}
// validate url
var status = 200
let status = 200
var pNorender = false
var pNotfound = false
let pNorender = false
let pNotfound = false
var pR = ssrValidatePath(url)
const pR = ssrValidatePath(url)
if (pR < 0) {
pNotfound = true
} else if (!pR) {
pNorender = true
}
var head = ""
var html = ""
var error = ""
let head = ""
let html = ""
let error = ""
comment += ", path: " + url
var cacheIt = false
let cacheIt = false
if (pNorender) {
html = "<!-- NO SSR RENDERING -->"
} else if (pNotfound) {
status = 404
html = "404 NOT FOUND"
} else {
// @ts-ignore
context.ssrCache = {}
// @ts-ignore
context.ssrRequest = ssrRequest
// try rendering, if error output plain html
try {
// @ts-ignore
context.ssrCache = {}
// @ts-ignore
context.ssrFetch = function (endpoint, options) {
var data
if (ssrAllowedAPIEndpoints.indexOf(endpoint) > -1) {
var _options = Object.assign({}, options)
if (_options.sort) _options.sort = [_options.sort]
try {
/*console.log(
"SSR",
endpoint,
JSON.stringify(_options)
)*/
var goSlice = context.db.find(endpoint, _options || {})
// need to deep copy, so shift and delete on pure js is possible
data = JSON.parse(JSON.stringify(goSlice))
} catch (e) {
console.log("ERROR", JSON.stringify(e))
data = []
}
} else {
console.log("SSR forbidden", endpoint)
data = []
}
var count = (data && data.length) || 0
if (options && count == options.limit) {
// read count from db
count = context.db.count(endpoint, _options || {})
}
var r = { data: data, count: count }
// @ts-ignore
context.ssrCache[obj2str({ endpoint: endpoint, options: options })] = r
return r
}
// include App.svelte and render it
// @ts-ignore
var app = require("../lib/app.server")
var rendered = app.default.render({
// console.log("####### RENDERING ", url)
const app = require("../lib/app.server")
const rendered = app.default.render({
url: url,
})
head = rendered.head
html = rendered.html
// add ssrCache to head
// add ssrCache to head, cache is built in ssr.js/apiRequest
head +=
"\n\n" +
"<script>window.__SSR_CACHE__ = " +
@@ -136,11 +123,12 @@ const { obj2str, log } = require("../lib/utils")
// status from webapp
// @ts-ignore
if (context.is404) {
// console.log("########## 404")
status = 404
} else {
cacheIt = true
}
} catch (e) {
} catch (/** @type {any} */ e) {
// save error for later insert into html
log(e.message)
log(e.stack)
@@ -149,7 +137,7 @@ const { obj2str, log } = require("../lib/utils")
}
// read html template and replace placeholders
var tpl = context.fs.readFile("templates/spa.html")
let tpl = context.fs.readFile("templates/spa.html")
tpl = tpl.replace("<!--HEAD-->", head)
tpl = tpl.replace("<!--HTML-->", html)
tpl = tpl.replace("<!--SSR.ERROR-->", error ? "<!--" + error + "-->" : "")
@@ -158,8 +146,11 @@ const { obj2str, log } = require("../lib/utils")
// save cache if adviced
if (cacheIt && !noCache) {
context.db.create("ssr", {
// context.debug.dump("ssr", {
path: url,
content: tpl,
// @ts-ignore
validUntil: context.ssrCacheValidUntil,
})
}
@@ -171,7 +162,7 @@ const { obj2str, log } = require("../lib/utils")
}
} else {
// only admins are allowed to get without url parameter
var auth = context.user.auth()
const auth = context.user.auth()
if (!auth || auth.role !== 0) {
throw {
status: 403,