This commit is contained in:
parent
6f1ab474fc
commit
1ebd1424e5
2
.env
2
.env
@ -5,4 +5,4 @@ UID=100
|
|||||||
GID=101
|
GID=101
|
||||||
RELEASE_ORG_SLUG=webmakers-gmbh
|
RELEASE_ORG_SLUG=webmakers-gmbh
|
||||||
RELEASE_PROJECT_SLUG=tibi_starter
|
RELEASE_PROJECT_SLUG=tibi_starter
|
||||||
#START_SCRIPT=:ssr
|
START_SCRIPT=:ssr
|
7
.vscode/settings.json
vendored
7
.vscode/settings.json
vendored
@ -18,11 +18,14 @@
|
|||||||
{
|
{
|
||||||
"match": "/api/.*(\\.ya?ml|js|env)$",
|
"match": "/api/.*(\\.ya?ml|js|env)$",
|
||||||
"isAsync": false,
|
"isAsync": false,
|
||||||
"cmd": "docker compose -p tibi-docs restart tibiserver",
|
"cmd": "cd ${currentWorkspace} && docker compose -f docker-compose-local.yml restart tibiserver",
|
||||||
"event": "onFileChange"
|
"event": "onFileChange"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"i18n-ally.localesPaths": ["frontend/locales"],
|
"i18n-ally.localesPaths": ["frontend/locales"],
|
||||||
"i18n-ally.sourceLanguage": "de",
|
"i18n-ally.sourceLanguage": "de",
|
||||||
"i18n-ally.keystyle": "nested"
|
"i18n-ally.keystyle": "nested",
|
||||||
|
"[svelte]": {
|
||||||
|
"editor.defaultFormatter": "svelte.svelte-vscode"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,28 +18,22 @@ meta:
|
|||||||
columns:
|
columns:
|
||||||
- id
|
- id
|
||||||
- insertTime
|
- insertTime
|
||||||
- path
|
- source: path
|
||||||
|
filter: true
|
||||||
|
|
||||||
permissions:
|
permissions:
|
||||||
public:
|
public:
|
||||||
methods:
|
methods:
|
||||||
get: false
|
|
||||||
post: false
|
|
||||||
put: false
|
|
||||||
delete: false
|
|
||||||
user:
|
|
||||||
methods:
|
|
||||||
get: false
|
|
||||||
post: false
|
|
||||||
put: false
|
|
||||||
delete: false
|
|
||||||
"token:${SSR_TOKEN}":
|
|
||||||
methods:
|
|
||||||
# only via url=
|
|
||||||
get: true
|
get: true
|
||||||
post: true
|
post: true
|
||||||
put: false
|
put: false
|
||||||
delete: false
|
delete: false
|
||||||
|
user:
|
||||||
|
methods:
|
||||||
|
get: true
|
||||||
|
post: false
|
||||||
|
put: false
|
||||||
|
delete: true
|
||||||
|
|
||||||
hooks:
|
hooks:
|
||||||
get:
|
get:
|
||||||
@ -51,7 +45,6 @@ hooks:
|
|||||||
type: javascript
|
type: javascript
|
||||||
file: hooks/ssr/post_bind.js
|
file: hooks/ssr/post_bind.js
|
||||||
|
|
||||||
# we only need hooks
|
|
||||||
fields:
|
fields:
|
||||||
- name: path
|
- name: path
|
||||||
type: string
|
type: string
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
const apiSsrBaseURL = "http://localhost:8080/api/v1/_/allkids_erfurt"
|
const apiSsrBaseURL =
|
||||||
|
"http://localhost:" + context.config.server().api.port + "/api/v1/_/" + context.api().namespace + "/"
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
apiSsrBaseURL,
|
apiSsrBaseURL,
|
||||||
ssrValidatePath: function (path) {
|
ssrValidatePath: function (/** @type {string} */ path) {
|
||||||
// validate if path ssr rendering is ok, -1 = NOTFOUND, 0 = NO SSR, 1 = SSR
|
// validate if path ssr rendering is ok, -1 = NOTFOUND, 0 = NO SSR, 1 = SSR
|
||||||
// pe. use context.readCollection("product", {filter: {path: path}}) ... to validate dynamic urls
|
// pe. use context.readCollection("product", {filter: {path: path}}) ... to validate dynamic urls
|
||||||
|
|
||||||
@ -11,7 +12,6 @@ module.exports = {
|
|||||||
|
|
||||||
// // all other sites are in db
|
// // all other sites are in db
|
||||||
//path = path?.replace(/^\//, "")
|
//path = path?.replace(/^\//, "")
|
||||||
console.log("PATH:", path)
|
|
||||||
const resp = context.db.find("content", {
|
const resp = context.db.find("content", {
|
||||||
filter: {
|
filter: {
|
||||||
$and: [{ path }],
|
$and: [{ path }],
|
||||||
@ -19,7 +19,6 @@ module.exports = {
|
|||||||
|
|
||||||
selector: { _id: 1 },
|
selector: { _id: 1 },
|
||||||
})
|
})
|
||||||
console.log("RESP:", resp?.length)
|
|
||||||
if (resp && resp.length) {
|
if (resp && resp.length) {
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
const { apiSsrBaseURL, ssrPublishCheckCollections } = require("../config")
|
const { apiSsrBaseURL } = require("../config")
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* api request via server, cache result in context.ssrCache
|
* api request via server, cache result in context.ssrCache
|
||||||
@ -6,21 +6,24 @@ const { apiSsrBaseURL, ssrPublishCheckCollections } = require("../config")
|
|||||||
*
|
*
|
||||||
* @param {string} cacheKey
|
* @param {string} cacheKey
|
||||||
* @param {string} endpoint
|
* @param {string} endpoint
|
||||||
|
* @param {string} query
|
||||||
* @param {ApiOptions} options
|
* @param {ApiOptions} options
|
||||||
* @returns {ApiResult}
|
* @returns {ApiResult<any>}
|
||||||
*/
|
*/
|
||||||
function ssrRequest(cacheKey, endpoint, query, options) {
|
function ssrRequest(cacheKey, endpoint, query, options) {
|
||||||
let url = endpoint + (query ? "?" + query : "")
|
let url = endpoint + (query ? "?" + query : "")
|
||||||
|
|
||||||
// console.log("############ FETCHING ", apiSsrBaseURL + url)
|
// console.log("############ FETCHING ", apiSsrBaseURL + url)
|
||||||
|
|
||||||
const response = context.http.fetch(apiSsrBaseURL + "/" + url, {
|
const response = context.http.fetch(apiSsrBaseURL + url, {
|
||||||
method: options.method,
|
method: options.method,
|
||||||
headers: options.headers,
|
headers: options.headers,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// console.log(JSON.stringify(response.headers, null, 2))
|
||||||
|
|
||||||
const json = response.body.json()
|
const json = response.body.json()
|
||||||
const count = parseInt(response.headers["x-results-count"] || "0")
|
const count = parseInt(response.headers["X-Results-Count"] || "0")
|
||||||
|
|
||||||
// json is go data structure and incompatible with js, so we need to convert it
|
// json is go data structure and incompatible with js, so we need to convert it
|
||||||
const r = { data: JSON.parse(JSON.stringify(json)), count: count }
|
const r = { data: JSON.parse(JSON.stringify(json)), count: count }
|
||||||
|
@ -3,6 +3,7 @@ const { apiClientBaseURL } = require("../config-client")
|
|||||||
/**
|
/**
|
||||||
* convert object to string
|
* convert object to string
|
||||||
* @param {any} obj object
|
* @param {any} obj object
|
||||||
|
* @returns {string} string
|
||||||
*/
|
*/
|
||||||
function obj2str(obj) {
|
function obj2str(obj) {
|
||||||
if (Array.isArray(obj)) {
|
if (Array.isArray(obj)) {
|
||||||
@ -32,70 +33,6 @@ function obj2str(obj) {
|
|||||||
if (obj) return obj
|
if (obj) return obj
|
||||||
}
|
}
|
||||||
|
|
||||||
// fetch polyfill
|
|
||||||
// [MIT License](LICENSE.md) © [Jason Miller](https://jasonformat.com/)
|
|
||||||
const _f = function (
|
|
||||||
/** @type {string | URL} */ url,
|
|
||||||
/** @type {{ method?: any; credentials?: any; headers?: any; body?: any; }} */ options
|
|
||||||
) {
|
|
||||||
if (typeof XMLHttpRequest === "undefined") {
|
|
||||||
return Promise.resolve(null)
|
|
||||||
}
|
|
||||||
|
|
||||||
options = options || {}
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
const request = new XMLHttpRequest()
|
|
||||||
const keys = []
|
|
||||||
const all = []
|
|
||||||
const headers = {}
|
|
||||||
|
|
||||||
const response = () => ({
|
|
||||||
ok: ((request.status / 100) | 0) == 2, // 200-299
|
|
||||||
statusText: request.statusText,
|
|
||||||
status: request.status,
|
|
||||||
url: request.responseURL,
|
|
||||||
text: () => Promise.resolve(request.responseText),
|
|
||||||
json: () => Promise.resolve(request.responseText).then(JSON.parse),
|
|
||||||
blob: () => Promise.resolve(new Blob([request.response])),
|
|
||||||
clone: response,
|
|
||||||
headers: {
|
|
||||||
// @ts-ignore
|
|
||||||
keys: () => keys,
|
|
||||||
// @ts-ignore
|
|
||||||
entries: () => all,
|
|
||||||
get: (n) => headers[n.toLowerCase()],
|
|
||||||
has: (n) => n.toLowerCase() in headers,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
request.open(options.method || "get", url, true)
|
|
||||||
|
|
||||||
request.onload = () => {
|
|
||||||
request
|
|
||||||
.getAllResponseHeaders()
|
|
||||||
// @ts-ignore
|
|
||||||
.replace(/^(.*?):[^\S\n]*([\s\S]*?)$/gm, (m, key, value) => {
|
|
||||||
keys.push((key = key.toLowerCase()))
|
|
||||||
all.push([key, value])
|
|
||||||
headers[key] = headers[key] ? `${headers[key]},${value}` : value
|
|
||||||
})
|
|
||||||
resolve(response())
|
|
||||||
}
|
|
||||||
|
|
||||||
request.onerror = reject
|
|
||||||
|
|
||||||
request.withCredentials = options.credentials == "include"
|
|
||||||
|
|
||||||
for (const i in options.headers) {
|
|
||||||
request.setRequestHeader(i, options.headers[i])
|
|
||||||
}
|
|
||||||
|
|
||||||
request.send(options.body || null)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
const _fetch = typeof fetch === "undefined" ? (typeof window === "undefined" ? _f : window.fetch || _f) : fetch
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* api request via client or server
|
* api request via client or server
|
||||||
* server function ssrRequest is called via context.ssrRequest, binded in ssr hook
|
* server function ssrRequest is called via context.ssrRequest, binded in ssr hook
|
||||||
@ -103,27 +40,29 @@ const _fetch = typeof fetch === "undefined" ? (typeof window === "undefined" ? _
|
|||||||
* @param {string} endpoint
|
* @param {string} endpoint
|
||||||
* @param {ApiOptions} options
|
* @param {ApiOptions} options
|
||||||
* @param {any} body
|
* @param {any} body
|
||||||
|
* @param {import("../../../frontend/src/sentry")} sentry
|
||||||
|
* @param {typeof fetch} _fetch
|
||||||
* @returns {Promise<ApiResult<any>>}
|
* @returns {Promise<ApiResult<any>>}
|
||||||
*/
|
*/
|
||||||
function apiRequest(endpoint, options, body) {
|
function apiRequest(endpoint, options, body, sentry, _fetch) {
|
||||||
// TODO cache only for GET
|
// TODO cache only for GET
|
||||||
|
|
||||||
// first check cache if on client
|
// first check cache if on client
|
||||||
const cacheKey = obj2str({ endpoint: endpoint, options: options })
|
const cacheKey = obj2str({ endpoint: endpoint, options: options })
|
||||||
options.method = options?.method || "GET"
|
console.log("SSR CHECK", cacheKey)
|
||||||
|
|
||||||
|
let method = options?.method || "GET"
|
||||||
|
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
if (typeof window !== "undefined" && window.__SSR_CACHE__ && options?.method === "GET") {
|
if (typeof window !== "undefined" && window.__SSR_CACHE__ && method === "GET") {
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
const cache = window.__SSR_CACHE__[cacheKey]
|
const cache = window.__SSR_CACHE__[cacheKey]
|
||||||
console.log("SSR:", cacheKey, cache)
|
console.log("SSR HIT:", cacheKey, cache)
|
||||||
if (cache) {
|
if (cache) {
|
||||||
return Promise.resolve(cache)
|
return Promise.resolve(cache)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let method = options?.method || "GET"
|
|
||||||
|
|
||||||
let query = "&count=1"
|
let query = "&count=1"
|
||||||
if (options?.filter) query += "&filter=" + encodeURIComponent(JSON.stringify(options.filter))
|
if (options?.filter) query += "&filter=" + encodeURIComponent(JSON.stringify(options.filter))
|
||||||
if (options?.sort) query += "&sort=" + options.sort + "&sort=_id"
|
if (options?.sort) query += "&sort=" + options.sort + "&sort=_id"
|
||||||
@ -138,6 +77,7 @@ function apiRequest(endpoint, options, body) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @type {{[key: string]: string}} */
|
||||||
let headers = {
|
let headers = {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
}
|
}
|
||||||
@ -153,8 +93,19 @@ function apiRequest(endpoint, options, body) {
|
|||||||
return d
|
return d
|
||||||
} else {
|
} else {
|
||||||
// client
|
// client
|
||||||
let url = endpoint + (query ? "?" + query : "")
|
let url = apiClientBaseURL + endpoint + (query ? "?" + query : "")
|
||||||
console.log("URL:", url)
|
|
||||||
|
const span = sentry.currentTransaction()?.startChild({
|
||||||
|
op: "fetch",
|
||||||
|
description: method + " " + url,
|
||||||
|
data: Object.assign({}, options, { url }),
|
||||||
|
})
|
||||||
|
const trace_id = span?.toTraceparent()
|
||||||
|
if (trace_id) {
|
||||||
|
headers["sentry-trace"] = trace_id
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @type {{[key: string]: any}} */
|
||||||
const requestOptions = {
|
const requestOptions = {
|
||||||
method,
|
method,
|
||||||
mode: "cors",
|
mode: "cors",
|
||||||
@ -165,7 +116,7 @@ function apiRequest(endpoint, options, body) {
|
|||||||
requestOptions.body = JSON.stringify(body)
|
requestOptions.body = JSON.stringify(body)
|
||||||
}
|
}
|
||||||
|
|
||||||
return _fetch(apiClientBaseURL + url, requestOptions).then((response) => {
|
const response = _fetch(url, requestOptions).then((response) => {
|
||||||
return response?.json().then((json) => {
|
return response?.json().then((json) => {
|
||||||
if (response?.status < 200 || response?.status >= 400) {
|
if (response?.status < 200 || response?.status >= 400) {
|
||||||
return Promise.reject({ response, data: json })
|
return Promise.reject({ response, data: json })
|
||||||
@ -173,6 +124,11 @@ function apiRequest(endpoint, options, body) {
|
|||||||
return Promise.resolve({ data: json || null, count: response.headers?.get("x-results-count") || 0 })
|
return Promise.resolve({ data: json || null, count: response.headers?.get("x-results-count") || 0 })
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
span?.end()
|
||||||
|
|
||||||
|
// @ts-ignore
|
||||||
|
return response
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -41,10 +41,12 @@ var { LIGHTHOUSE_TOKEN } = require("../config")
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* clear SSR cache
|
* clear SSR cache
|
||||||
|
* @returns {number}
|
||||||
*/
|
*/
|
||||||
function clearSSRCache() {
|
function clearSSRCache() {
|
||||||
var info = context.db.deleteMany("ssr", {})
|
var info = context.db.deleteMany("ssr", {})
|
||||||
context.response.header("X-SSR-Cleared", info.removed)
|
context.response.header("X-SSR-Cleared", info.removed)
|
||||||
|
return info.removed
|
||||||
}
|
}
|
||||||
|
|
||||||
function calculateAverageDynamically(dbObjs) {
|
function calculateAverageDynamically(dbObjs) {
|
||||||
|
@ -1,29 +1,38 @@
|
|||||||
// TODO: add query string functionality to cache
|
const utils = require("../lib/utils")
|
||||||
|
|
||||||
|
const { release } = require("../config-client")
|
||||||
|
|
||||||
const { ssrValidatePath } = require("../config")
|
const { ssrValidatePath } = require("../config")
|
||||||
const { log } = require("../lib/utils")
|
const { ssrRequest } = require("../lib/ssr-server")
|
||||||
const { ssrRequest } = require("../lib/ssr-server.js")
|
|
||||||
|
|
||||||
;(function () {
|
;(function () {
|
||||||
/** @type {HookResponse} */
|
var request = context.request()
|
||||||
let response = null
|
|
||||||
|
|
||||||
const request = context.request()
|
var trackingCall = request.header("x-ssr-skip")
|
||||||
let url = request.query("url")
|
if (trackingCall) {
|
||||||
|
// skip tracking
|
||||||
|
throw {
|
||||||
|
status: parseInt(trackingCall),
|
||||||
|
html: "",
|
||||||
|
log: false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let url = request.query("url") || request.header("x-ssr-url") || "/"
|
||||||
const noCache = request.query("noCache")
|
const noCache = request.query("noCache")
|
||||||
|
|
||||||
// add sentry trace id to head
|
|
||||||
const trace_id = context.debug.sentryTraceId()
|
const trace_id = context.debug.sentryTraceId()
|
||||||
|
/**
|
||||||
|
* @param {string} content
|
||||||
|
*/
|
||||||
function addSentryTrace(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)
|
|
||||||
|
|
||||||
if (url) {
|
context.response.header("sentry-trace", trace_id)
|
||||||
// comment will be printed to html later
|
const auth = context.user.auth()
|
||||||
let comment = ""
|
if (auth && auth.role == 0) {
|
||||||
/** @type {Date} */ // @ts-ignore
|
} else if (url) {
|
||||||
context.ssrCacheValidUntil = null
|
var comment = ""
|
||||||
|
|
||||||
url = url.split("?")[0]
|
url = url.split("?")[0]
|
||||||
comment += "url: " + url
|
comment += "url: " + url
|
||||||
@ -31,40 +40,55 @@ const { ssrRequest } = require("../lib/ssr-server.js")
|
|||||||
if (url && url.length > 1) {
|
if (url && url.length > 1) {
|
||||||
url = url.replace(/\/$/, "")
|
url = url.replace(/\/$/, "")
|
||||||
}
|
}
|
||||||
if (url == "/noindex" || !url) {
|
if (url == "/index" || !url) {
|
||||||
url = "/" // see .htaccess
|
url = "/" // see .htaccess
|
||||||
}
|
}
|
||||||
|
|
||||||
// check if url is in cache
|
function useCache(/** @type {string} */ _url) {
|
||||||
/** @type {Ssr[]} */ // @ts-ignore
|
var cache =
|
||||||
const cache =
|
!noCache &&
|
||||||
!noCache &&
|
context.db.find("ssr", {
|
||||||
context.db.find("ssr", {
|
filter: {
|
||||||
filter: {
|
path: _url,
|
||||||
path: url,
|
},
|
||||||
},
|
})
|
||||||
})
|
|
||||||
if (cache && cache.length) {
|
if (cache && cache.length) {
|
||||||
// use cache
|
// use cache
|
||||||
context.response.header("X-SSR-Cache", "true")
|
context.response.header("X-SSR-Cache", "true")
|
||||||
throw {
|
throw {
|
||||||
status: 200,
|
status: 200,
|
||||||
log: false,
|
log: false,
|
||||||
html: addSentryTrace(cache[0].content),
|
html: addSentryTrace(cache[0].content),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// validate url
|
// validate url
|
||||||
let status = 200
|
var status = 200
|
||||||
|
|
||||||
let pNorender = false
|
var pNorender = false
|
||||||
let pNotfound = false
|
var pNotfound = false
|
||||||
|
|
||||||
const pR = ssrValidatePath(url)
|
const pR = ssrValidatePath(url)
|
||||||
if (pR < 0) {
|
if (pR === -1) {
|
||||||
pNotfound = true
|
pNotfound = true
|
||||||
|
comment += ", notFound"
|
||||||
} else if (!pR) {
|
} else if (!pR) {
|
||||||
pNorender = true
|
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 head = ""
|
||||||
@ -73,7 +97,7 @@ const { ssrRequest } = require("../lib/ssr-server.js")
|
|||||||
|
|
||||||
comment += ", path: " + url
|
comment += ", path: " + url
|
||||||
|
|
||||||
let cacheIt = false
|
var cacheIt = false
|
||||||
if (pNorender) {
|
if (pNorender) {
|
||||||
html = "<!-- NO SSR RENDERING -->"
|
html = "<!-- NO SSR RENDERING -->"
|
||||||
} else if (pNotfound) {
|
} else if (pNotfound) {
|
||||||
@ -85,20 +109,17 @@ const { ssrRequest } = require("../lib/ssr-server.js")
|
|||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
context.ssrRequest = ssrRequest
|
context.ssrRequest = ssrRequest
|
||||||
|
|
||||||
// try rendering, if error output plain html
|
|
||||||
try {
|
try {
|
||||||
// include App.svelte and render it
|
// if error, output plain html without prerendering
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
|
|
||||||
// console.log("####### RENDERING ", url)
|
|
||||||
const app = require("../lib/app.server")
|
const app = require("../lib/app.server")
|
||||||
|
|
||||||
const rendered = app.default.render({
|
const rendered = app.default.render({
|
||||||
url: url,
|
url: url,
|
||||||
})
|
})
|
||||||
head = rendered.head
|
head = rendered.head
|
||||||
html = rendered.html
|
html = rendered.html
|
||||||
|
|
||||||
// add ssrCache to head, cache is built in ssr.js/apiRequest
|
|
||||||
head +=
|
head +=
|
||||||
"\n\n" +
|
"\n\n" +
|
||||||
"<script>window.__SSR_CACHE__ = " +
|
"<script>window.__SSR_CACHE__ = " +
|
||||||
@ -109,50 +130,49 @@ const { ssrRequest } = require("../lib/ssr-server.js")
|
|||||||
// status from webapp
|
// status from webapp
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
if (context.is404) {
|
if (context.is404) {
|
||||||
// console.log("########## 404")
|
|
||||||
status = 404
|
status = 404
|
||||||
} else {
|
} else {
|
||||||
cacheIt = true
|
cacheIt = true
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (/** @type {any} */ e) {
|
||||||
// save error for later insert into html
|
utils.log(e.message)
|
||||||
log(e.message)
|
utils.log(e.stack)
|
||||||
log(e.stack)
|
|
||||||
error = "error: " + e.message + "\n\n" + 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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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("<!--HEAD-->", head)
|
||||||
tpl = tpl.replace("<!--HTML-->", html)
|
tpl = tpl.replace("<!--HTML-->", html)
|
||||||
tpl = tpl.replace("<!--SSR.ERROR-->", error ? "<!--" + error + "-->" : "")
|
tpl = tpl.replace("<!--SSR.ERROR-->", error ? "<!--" + error + "-->" : "")
|
||||||
tpl = tpl.replace("<!--SSR.COMMENT-->", comment ? "<!--" + comment + "-->" : "")
|
tpl = tpl.replace("<!--SSR.COMMENT-->", comment ? "<!--" + comment + "-->" : "")
|
||||||
|
|
||||||
// save cache if adviced
|
|
||||||
if (cacheIt && !noCache) {
|
if (cacheIt && !noCache) {
|
||||||
|
// save cache
|
||||||
context.db.create("ssr", {
|
context.db.create("ssr", {
|
||||||
// context.debug.dump("ssr", {
|
|
||||||
path: url,
|
path: url,
|
||||||
content: tpl,
|
content: tpl,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// return html
|
|
||||||
throw {
|
throw {
|
||||||
status: status,
|
status: status,
|
||||||
log: false,
|
log: false,
|
||||||
html: addSentryTrace(tpl),
|
html: addSentryTrace(tpl),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// only admins are allowed to get without url parameter
|
// only admins are allowed
|
||||||
const auth = context.user.auth()
|
throw {
|
||||||
if (!auth || auth.role !== 0) {
|
status: 403,
|
||||||
throw {
|
message: "invalid auth",
|
||||||
status: 403,
|
auth: auth,
|
||||||
message: "invalid auth",
|
release: release,
|
||||||
auth: auth,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})()
|
})()
|
||||||
|
@ -1,16 +1,25 @@
|
|||||||
const utils = require("../lib/utils")
|
const { release } = require("../config-client")
|
||||||
|
var utils = require("../lib/utils")
|
||||||
|
|
||||||
;(function () {
|
;(function () {
|
||||||
if (context.request().query("clear")) {
|
if (context.request().query("clear")) {
|
||||||
utils.clearSSRCache()
|
console.log("CLEARING SSR CACHE")
|
||||||
throw {
|
var removed = utils.clearSSRCache()
|
||||||
|
var stats = {
|
||||||
status: 200,
|
status: 200,
|
||||||
message: "cache cleared",
|
message: "ok",
|
||||||
|
removed: removed,
|
||||||
|
release: release,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
utils.log(stats)
|
||||||
|
|
||||||
|
throw stats
|
||||||
}
|
}
|
||||||
|
|
||||||
throw {
|
throw {
|
||||||
status: 500,
|
status: 500,
|
||||||
message: "ssr is only a dummy collection",
|
message: "ssr is only a dummy collection",
|
||||||
|
release: release,
|
||||||
}
|
}
|
||||||
})()
|
})()
|
||||||
|
5
babel.config.server.json
Normal file
5
babel.config.server.json
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"sourceMaps": "inline",
|
||||||
|
"inputSourceMap": true,
|
||||||
|
"plugins": [["@babel/plugin-transform-async-to-generator"]]
|
||||||
|
}
|
@ -5,6 +5,9 @@ config.options.sourcemap = "inline"
|
|||||||
config.options.minify = false
|
config.options.minify = false
|
||||||
config.options.platform = "node"
|
config.options.platform = "node"
|
||||||
config.options.format = "cjs"
|
config.options.format = "cjs"
|
||||||
|
// es2015 will transform async/await to generators, but not working with svelte
|
||||||
|
// so we need babel to transform async/await to promises
|
||||||
|
// config.options.target = "es2015"
|
||||||
config.options.entryPoints = ["./frontend/src/ssr.ts"]
|
config.options.entryPoints = ["./frontend/src/ssr.ts"]
|
||||||
config.options.outfile = __dirname + "/_temp/app.server.js"
|
config.options.outfile = __dirname + "/_temp/app.server.js"
|
||||||
config.options.plugins = [
|
config.options.plugins = [
|
||||||
@ -16,6 +19,11 @@ config.options.plugins = [
|
|||||||
dev: (process.argv?.length > 2 ? process.argv[2] : "build") !== "build",
|
dev: (process.argv?.length > 2 ? process.argv[2] : "build") !== "build",
|
||||||
},
|
},
|
||||||
preprocess: svelteConfig.preprocess,
|
preprocess: svelteConfig.preprocess,
|
||||||
|
filterWarnings: (warning) => {
|
||||||
|
// filter out a11y
|
||||||
|
if (warning.code.match(/^a11y/)) return false
|
||||||
|
return true
|
||||||
|
},
|
||||||
}),
|
}),
|
||||||
config.resolvePlugin,
|
config.resolvePlugin,
|
||||||
]
|
]
|
||||||
|
@ -1,12 +1,77 @@
|
|||||||
import { apiRequest } from "../../api/hooks/lib/ssr"
|
import { apiRequest } from "../../api/hooks/lib/ssr"
|
||||||
|
import * as sentry from "./sentry"
|
||||||
|
|
||||||
|
// fetch polyfill
|
||||||
|
// [MIT License](LICENSE.md) © [Jason Miller](https://jasonformat.com/)
|
||||||
|
const _f = function (url: string, options?: { [key: string]: any }) {
|
||||||
|
if (typeof XMLHttpRequest === "undefined") {
|
||||||
|
return Promise.resolve(null)
|
||||||
|
}
|
||||||
|
|
||||||
|
options = options || {}
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
const request = new XMLHttpRequest()
|
||||||
|
const keys: string[] = []
|
||||||
|
const all = []
|
||||||
|
const headers = {}
|
||||||
|
|
||||||
|
const response = () => ({
|
||||||
|
ok: ((request.status / 100) | 0) == 2, // 200-299
|
||||||
|
statusText: request.statusText,
|
||||||
|
status: request.status,
|
||||||
|
url: request.responseURL,
|
||||||
|
text: () => Promise.resolve(request.responseText),
|
||||||
|
json: () => Promise.resolve(request.responseText).then(JSON.parse),
|
||||||
|
blob: () => Promise.resolve(new Blob([request.response])),
|
||||||
|
clone: response,
|
||||||
|
headers: {
|
||||||
|
// @ts-ignore
|
||||||
|
keys: () => keys,
|
||||||
|
// @ts-ignore
|
||||||
|
entries: () => all,
|
||||||
|
get: (n) => headers[n.toLowerCase()],
|
||||||
|
has: (n) => n.toLowerCase() in headers,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
request.open(options.method || "get", url, true)
|
||||||
|
|
||||||
|
request.onload = () => {
|
||||||
|
request
|
||||||
|
.getAllResponseHeaders()
|
||||||
|
// @ts-ignore
|
||||||
|
.replace(/^(.*?):[^\S\n]*([\s\S]*?)$/gm, (m, key, value) => {
|
||||||
|
keys.push((key = key.toLowerCase()))
|
||||||
|
all.push([key, value])
|
||||||
|
headers[key] = headers[key] ? `${headers[key]},${value}` : value
|
||||||
|
})
|
||||||
|
resolve(response())
|
||||||
|
}
|
||||||
|
|
||||||
|
request.onerror = reject
|
||||||
|
|
||||||
|
request.withCredentials = options.credentials == "include"
|
||||||
|
|
||||||
|
for (const i in options.headers) {
|
||||||
|
request.setRequestHeader(i, options.headers[i])
|
||||||
|
}
|
||||||
|
|
||||||
|
request.send(options.body || null)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// fetch must be declared after sentry import to get the hijacked fetch
|
||||||
|
// @ts-ignore
|
||||||
|
export const _fetch: typeof fetch =
|
||||||
|
typeof fetch === "undefined" ? (typeof window === "undefined" ? _f : window.fetch || _f) : fetch
|
||||||
|
|
||||||
export const api = async <T>(
|
export const api = async <T>(
|
||||||
endpoint: string,
|
endpoint: string,
|
||||||
options?: ApiOptions,
|
options?: ApiOptions,
|
||||||
body?: any
|
body?: any
|
||||||
): Promise<{ data: T; count: number } | any> => {
|
): Promise<{ data: T; count: number } | any> => {
|
||||||
let data = await apiRequest(endpoint, options, body)
|
let data = await apiRequest(endpoint, options, body, sentry, _fetch)
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
console.log(data, "data")
|
// console.log(data, "data")
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
@ -10,31 +10,33 @@
|
|||||||
unten: "flex-end",
|
unten: "flex-end",
|
||||||
}
|
}
|
||||||
|
|
||||||
window.addEventListener("ccAccept", (e) => {
|
if (typeof window !== "undefined") {
|
||||||
if ((e as CustomEvent).detail[1] == cookieName) contentShown = true
|
window.addEventListener("ccAccept", (e) => {
|
||||||
})
|
if ((e as CustomEvent).detail[1] == cookieName) contentShown = true
|
||||||
//isCookieSet isnt really precise
|
|
||||||
function checkCookie(cookieName: string) {
|
|
||||||
// Get all cookies
|
|
||||||
var allCookies = decodeURIComponent(document.cookie)
|
|
||||||
// Split into individual cookies
|
|
||||||
var cookies = allCookies.split(";")
|
|
||||||
var ccTagCookies: string[] = []
|
|
||||||
cookies.forEach((e) => {
|
|
||||||
e.includes("ccTags") ? (ccTagCookies = e.split(",")) : void 0
|
|
||||||
})
|
})
|
||||||
for (var i = 0; i < ccTagCookies.length; i++) {
|
//isCookieSet isnt really precise
|
||||||
var c = ccTagCookies[i]
|
function checkCookie(cookieName: string) {
|
||||||
// Trim whitespace
|
// Get all cookies
|
||||||
while (c.charAt(0) == " ") c = c.substring(1)
|
var allCookies = decodeURIComponent(document.cookie)
|
||||||
// If the cookie's name matches the given name
|
// Split into individual cookies
|
||||||
if (c == cookieName) return true
|
var cookies = allCookies.split(";")
|
||||||
|
var ccTagCookies: string[] = []
|
||||||
|
cookies.forEach((e) => {
|
||||||
|
e.includes("ccTags") ? (ccTagCookies = e.split(",")) : void 0
|
||||||
|
})
|
||||||
|
for (var i = 0; i < ccTagCookies.length; i++) {
|
||||||
|
var c = ccTagCookies[i]
|
||||||
|
// Trim whitespace
|
||||||
|
while (c.charAt(0) == " ") c = c.substring(1)
|
||||||
|
// If the cookie's name matches the given name
|
||||||
|
if (c == cookieName) return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// Verwendung
|
// Verwendung
|
||||||
if (checkCookie(cookieName)) contentShown = true
|
if (checkCookie(cookieName)) contentShown = true
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
{#if contentShown}
|
{#if contentShown}
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
let opened = -1
|
let opened = -1
|
||||||
let openMenu = false
|
let openMenu = false
|
||||||
$: {
|
$: if (typeof document !== "undefined") {
|
||||||
if (openMenu) document.body.style.overflow = "hidden"
|
if (openMenu) document.body.style.overflow = "hidden"
|
||||||
else document.body.style.overflow = "auto"
|
else document.body.style.overflow = "auto"
|
||||||
}
|
}
|
||||||
|
@ -90,24 +90,26 @@
|
|||||||
<!-- svelte-ignore a11y-click-events-have-key-events -->
|
<!-- svelte-ignore a11y-click-events-have-key-events -->
|
||||||
<!-- svelte-ignore a11y-no-static-element-interactions -->
|
<!-- svelte-ignore a11y-no-static-element-interactions -->
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<div
|
{#if typeof window !== "undefined"}
|
||||||
class="datepicker"
|
<div
|
||||||
on:click|preventDefault|stopPropagation
|
class="datepicker"
|
||||||
on:keydown|stopPropagation
|
on:click|preventDefault|stopPropagation
|
||||||
on:submit|stopPropagation|preventDefault
|
on:keydown|stopPropagation
|
||||||
>
|
on:submit|stopPropagation|preventDefault
|
||||||
<CalendarView
|
>
|
||||||
weekStart="{1}"
|
<CalendarView
|
||||||
bind:vaue="{value}"
|
weekStart="{1}"
|
||||||
on:change="{(e) => {
|
bind:vaue="{value}"
|
||||||
value = e.detail
|
on:change="{(e) => {
|
||||||
}}"
|
value = e.detail
|
||||||
min="{today}"
|
}}"
|
||||||
max="{oneYearFromNow}"
|
min="{today}"
|
||||||
blackout="{blackoutDates}"
|
max="{oneYearFromNow}"
|
||||||
locale="de-DE"
|
blackout="{blackoutDates}"
|
||||||
/>
|
locale="de-DE"
|
||||||
</div>
|
/>
|
||||||
|
</div>
|
||||||
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<style lang="less">
|
<style lang="less">
|
||||||
|
@ -2,14 +2,11 @@
|
|||||||
import { mediaLibrary, pages } from "../lib/store"
|
import { mediaLibrary, pages } from "../lib/store"
|
||||||
import Pagebuilder from "../lib/components/pagebuilder/Pagebuilder.svelte"
|
import Pagebuilder from "../lib/components/pagebuilder/Pagebuilder.svelte"
|
||||||
import { apiBaseURL, baseURL } from "../config"
|
import { apiBaseURL, baseURL } from "../config"
|
||||||
import { onMount } from "svelte"
|
|
||||||
import NotFound from "./NotFound.svelte"
|
import NotFound from "./NotFound.svelte"
|
||||||
export let path: string = "/"
|
export let path: string = "/"
|
||||||
export let isHomepage = true
|
export let isHomepage = true
|
||||||
let page: Content = $pages[path]
|
let page: Content = $pages[path]
|
||||||
$: page = $pages[path]
|
$: page = $pages[path]
|
||||||
let personPage = false
|
|
||||||
//test
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<svelte:head>
|
<svelte:head>
|
||||||
|
@ -14,7 +14,7 @@
|
|||||||
"build": "node scripts/esbuild-wrapper.js build",
|
"build": "node scripts/esbuild-wrapper.js build",
|
||||||
"build:admin": "node scripts/esbuild-wrapper.js build esbuild.config.admin.js",
|
"build:admin": "node scripts/esbuild-wrapper.js build esbuild.config.admin.js",
|
||||||
"build:legacy": "node scripts/esbuild-wrapper.js build esbuild.config.legacy.js && babel _temp/index.js -o _temp/index.babeled.js && esbuild _temp/index.babeled.js --outfile=frontend/dist/index.es5.js --target=es5 --bundle --minify --sourcemap",
|
"build:legacy": "node scripts/esbuild-wrapper.js build esbuild.config.legacy.js && babel _temp/index.js -o _temp/index.babeled.js && esbuild _temp/index.babeled.js --outfile=frontend/dist/index.es5.js --target=es5 --bundle --minify --sourcemap",
|
||||||
"build:server": "node scripts/esbuild-wrapper.js build esbuild.config.server.js && babel _temp/app.server.js -o _temp/app.server.babeled.js && esbuild _temp/app.server.babeled.js --outfile=api/hooks/lib/app.server.js --target=es5 --bundle --sourcemap --platform=node",
|
"build:server": "node scripts/esbuild-wrapper.js build esbuild.config.server.js && babel --config-file ./babel.config.server.json _temp/app.server.js -o _temp/app.server.babeled.js && esbuild _temp/app.server.babeled.js --outfile=api/hooks/lib/app.server.js --bundle --sourcemap --platform=node",
|
||||||
"build:test": "node scripts/esbuild-wrapper.js build esbuild.config.test.js && babel --config-file ./babel.config.test.json _temp/hook.test.js -o _temp/hook.test.babeled.js && esbuild _temp/hook.test.babeled.js --outfile=api/hooks/lib/hook.test.js --target=es5 --bundle --sourcemap --platform=node",
|
"build:test": "node scripts/esbuild-wrapper.js build esbuild.config.test.js && babel --config-file ./babel.config.test.json _temp/hook.test.js -o _temp/hook.test.babeled.js && esbuild _temp/hook.test.babeled.js --outfile=api/hooks/lib/hook.test.js --target=es5 --bundle --sourcemap --platform=node",
|
||||||
"upload:sourcemaps": "scripts/upload-sourcemaps.sh"
|
"upload:sourcemaps": "scripts/upload-sourcemaps.sh"
|
||||||
},
|
},
|
||||||
|
2
types/global.d.ts
vendored
2
types/global.d.ts
vendored
@ -2,7 +2,7 @@ interface Ssr {
|
|||||||
id?: string
|
id?: string
|
||||||
path: string
|
path: string
|
||||||
content: string
|
content: string
|
||||||
validUntil: any // go Time
|
// validUntil: any // go Time
|
||||||
}
|
}
|
||||||
|
|
||||||
interface ApiOptions {
|
interface ApiOptions {
|
||||||
|
Loading…
Reference in New Issue
Block a user