yarn upgrade
This commit is contained in:
@@ -1,23 +1,42 @@
|
||||
// @ts-check
|
||||
const utils = require("../lib/utils")
|
||||
|
||||
var utils = require("../lib/utils")
|
||||
const { release } = require("../config-client")
|
||||
|
||||
const { ssrValidatePath } = require("../config")
|
||||
const { ssrRequest } = require("../lib/ssr-server")
|
||||
;(function () {
|
||||
var request = context.request()
|
||||
var url = request.query("url")
|
||||
var noCache = request.query("noCache")
|
||||
|
||||
var trace_id = context.debug.sentryTraceId()
|
||||
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>'
|
||||
)
|
||||
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
|
||||
|
||||
if (url) {
|
||||
var comment = ""
|
||||
|
||||
url = url.split("?")[0]
|
||||
@@ -26,23 +45,36 @@ var utils = require("../lib/utils")
|
||||
if (url && url.length > 1) {
|
||||
url = url.replace(/\/$/, "")
|
||||
}
|
||||
if (url == "/noindex" || !url) {
|
||||
if (url == "/index" || !url) {
|
||||
url = "/" // see .htaccess
|
||||
}
|
||||
|
||||
var cache =
|
||||
!noCache &&
|
||||
context.db.find("ssr", {
|
||||
filter: {
|
||||
path: url,
|
||||
},
|
||||
})
|
||||
function useCache(/** @type {string} */ _url) {
|
||||
var cache =
|
||||
!noCache &&
|
||||
context.db.find("ssr", {
|
||||
filter: {
|
||||
path: _url,
|
||||
},
|
||||
})
|
||||
|
||||
if (cache && cache.length) {
|
||||
// use cache
|
||||
throw {
|
||||
status: 200,
|
||||
html: addSentryTrace(cache[0].content),
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,16 +84,30 @@ var utils = require("../lib/utils")
|
||||
var pNorender = false
|
||||
var pNotfound = false
|
||||
|
||||
var pR = utils.ssrValidatePath(url)
|
||||
if (pR < 0) {
|
||||
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
|
||||
}
|
||||
|
||||
var head = ""
|
||||
var html = ""
|
||||
var error = ""
|
||||
if (noCache) {
|
||||
comment += ", noCache"
|
||||
}
|
||||
|
||||
if (!pNorender && !pNotfound) {
|
||||
// check if we have a cache
|
||||
useCache(url)
|
||||
}
|
||||
|
||||
let head = ""
|
||||
let html = ""
|
||||
let error = ""
|
||||
|
||||
comment += ", path: " + url
|
||||
|
||||
@@ -72,64 +118,17 @@ var utils = require("../lib/utils")
|
||||
status = 404
|
||||
html = "404 NOT FOUND"
|
||||
} else {
|
||||
// @ts-ignore
|
||||
context.ssrCache = {}
|
||||
// @ts-ignore
|
||||
context.ssrRequest = ssrRequest
|
||||
|
||||
try {
|
||||
// if error, output plain html without prerendering
|
||||
|
||||
// @ts-ignore
|
||||
context.ssrCache = {}
|
||||
// @ts-ignore
|
||||
context.ssrFetch = function (endpoint, options) {
|
||||
var data
|
||||
if (
|
||||
endpoint == "product" ||
|
||||
endpoint == "category" ||
|
||||
endpoint == "country" ||
|
||||
endpoint == "content"
|
||||
) {
|
||||
var _options = Object.assign({}, options)
|
||||
const app = require("../lib/app.server")
|
||||
|
||||
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[
|
||||
utils.obj2str({ endpoint: endpoint, options: options })
|
||||
] = r
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
// @ts-ignore
|
||||
var app = require("../lib/app.server")
|
||||
|
||||
var rendered = app.default.render({
|
||||
const rendered = app.default.render({
|
||||
url: url,
|
||||
})
|
||||
head = rendered.head
|
||||
@@ -149,60 +148,47 @@ var utils = require("../lib/utils")
|
||||
} else {
|
||||
cacheIt = true
|
||||
}
|
||||
} catch (e) {
|
||||
} 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")
|
||||
tpl = tpl.replace("<!--HEAD-->", head)
|
||||
tpl = tpl.replace("<!--HTML-->", html)
|
||||
tpl = tpl.replace(
|
||||
"<!--SSR.ERROR-->",
|
||||
error ? "<!--" + error + "-->" : ""
|
||||
)
|
||||
tpl = tpl.replace(
|
||||
"<!--SSR.COMMENT-->",
|
||||
comment ? "<!--" + comment + "-->" : ""
|
||||
)
|
||||
tpl = tpl.replace("<!--SSR.ERROR-->", error ? "<!--" + error + "-->" : "")
|
||||
tpl = tpl.replace("<!--SSR.COMMENT-->", comment ? "<!--" + comment + "-->" : "")
|
||||
|
||||
if (cacheIt && !noCache) {
|
||||
// save cache
|
||||
context.db.create("ssr", {
|
||||
path: url,
|
||||
content: tpl,
|
||||
// @ts-ignore
|
||||
validUntil: context.ssrCacheValidUntil,
|
||||
})
|
||||
}
|
||||
|
||||
tpl.replace(
|
||||
"</head>",
|
||||
'<meta name="sentry-trace" content="' + trace_id + '" /></head>'
|
||||
)
|
||||
|
||||
throw {
|
||||
status: status,
|
||||
log: false,
|
||||
html: addSentryTrace(tpl),
|
||||
}
|
||||
} else {
|
||||
var auth = context.user.auth()
|
||||
if (!auth || auth.role !== 0) {
|
||||
// only admins are allowed
|
||||
throw {
|
||||
status: 403,
|
||||
message: "invalid auth",
|
||||
auth: auth,
|
||||
}
|
||||
// only admins are allowed
|
||||
throw {
|
||||
status: 403,
|
||||
message: "invalid auth",
|
||||
auth: auth,
|
||||
release: release,
|
||||
}
|
||||
}
|
||||
})()
|
||||
|
||||
/*
|
||||
require("../lib/hook.test")
|
||||
console.log("hook test ende")
|
||||
throw {
|
||||
status: 500,
|
||||
msg: "TEST",
|
||||
}
|
||||
*/
|
||||
|
||||
@@ -1,8 +1,25 @@
|
||||
// @ts-check
|
||||
const { release } = require("../config-client")
|
||||
var utils = require("../lib/utils")
|
||||
|
||||
;(function () {
|
||||
if (context.request().query("clear")) {
|
||||
console.log("CLEARING SSR CACHE")
|
||||
var removed = utils.clearSSRCache()
|
||||
var stats = {
|
||||
status: 200,
|
||||
message: "ok",
|
||||
removed: removed,
|
||||
release: release,
|
||||
}
|
||||
|
||||
utils.log(stats)
|
||||
|
||||
throw stats
|
||||
}
|
||||
|
||||
throw {
|
||||
status: 500,
|
||||
message: "ssr is only a dummy collection",
|
||||
release: release,
|
||||
}
|
||||
})()
|
||||
|
||||
Reference in New Issue
Block a user