ssr
All checks were successful
deploy to production / deploy (push) Successful in 1m23s

This commit is contained in:
Robin Grenzdörfer 2023-12-15 12:35:45 +00:00
parent 35168ddaab
commit 241513e32f
20 changed files with 161 additions and 94 deletions

View File

@ -29,6 +29,13 @@ hooks:
create: create:
type: javascript type: javascript
file: hooks/backups/post_create.js file: hooks/backups/post_create.js
return:
type: javascript
file: hooks/clear_cache.js
put:
return:
type: javascript
file: hooks/clear_cache.js
fields: fields:
- name: collectionName - name: collectionName

View File

@ -25,6 +25,15 @@ permissions:
post: true post: true
put: true put: true
delete: true delete: true
hooks:
post:
return:
type: javascript
file: hooks/clear_cache.js
put:
return:
type: javascript
file: hooks/clear_cache.js
fields: fields:
- name: banner - name: banner

View File

@ -98,6 +98,16 @@ projections:
select: select:
path: 1 path: 1
hooks:
post:
return:
type: javascript
file: hooks/clear_cache.js
put:
return:
type: javascript
file: hooks/clear_cache.js
fields: fields:
- type: string - type: string
name: path name: path

View File

@ -82,6 +82,15 @@ x-seite: &seite
mapping: mapping:
id: id id: id
name: path name: path
hooks:
post:
return:
type: javascript
file: hooks/clear_cache.js
put:
return:
type: javascript
file: hooks/clear_cache.js
fields: fields:
- name: tree - name: tree

5
api/hooks/clear_cache.js Normal file
View File

@ -0,0 +1,5 @@
var utils = require("./lib/utils")
;(function () {
utils.clearSSRCache()
})()

View File

@ -1,18 +1,26 @@
const apiSsrBaseURL = "http://localhost:8080/api/v1/_/allkids_erfurt"
module.exports = { module.exports = {
apiSsrBaseURL,
ssrValidatePath: function (path) { ssrValidatePath: function (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
// / is de home // // / is de home
if (path == "/") return 1 // if (path == "/") return 1
// // all other sites are in db
//path = path?.replace(/^\//, "")
console.log("PATH:", path)
// filter for path or alternativePaths // filter for path or alternativePaths
const resp = context.db.find("content", { const resp = context.db.find("content", {
filter: { filter: {
$or: [{ path }], $and: [{ path }],
}, },
selector: { _id: 1 }, selector: { _id: 1 },
}) })
console.log("RESP:", resp?.length)
if (resp && resp.length) { if (resp && resp.length) {
return 1 return 1
} }
@ -20,5 +28,5 @@ module.exports = {
// not found // not found
return -1 return -1
}, },
ssrAllowedAPIEndpoints: ["content", "medialib"], ssrPublishCheckCollections: ["content"],
} }

View File

@ -0,0 +1,36 @@
const { apiSsrBaseURL, ssrPublishCheckCollections } = require("../config")
/**
* api request via server, cache result in context.ssrCache
* should be elimated in client code via tree shaking
*
* @param {string} cacheKey
* @param {string} endpoint
* @param {ApiOptions} options
* @returns {ApiResult}
*/
function ssrRequest(cacheKey, endpoint, query, options) {
let url = endpoint + (query ? "?" + query : "")
// console.log("############ FETCHING ", apiSsrBaseURL + url)
const response = context.http.fetch(apiSsrBaseURL + "/" + url, {
method: options.method,
headers: options.headers,
})
const json = response.body.json()
const count = parseInt(response.headers["x-results-count"] || "0")
// 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 }
// @ts-ignore
context.ssrCache[cacheKey] = r
return r
}
module.exports = {
ssrRequest,
}

View File

@ -110,6 +110,7 @@ function apiRequest(endpoint, options, body) {
// 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"
// @ts-ignore // @ts-ignore
if (typeof window !== "undefined" && window.__SSR_CACHE__ && options?.method === "GET") { if (typeof window !== "undefined" && window.__SSR_CACHE__ && options?.method === "GET") {
@ -153,6 +154,7 @@ function apiRequest(endpoint, options, body) {
} else { } else {
// client // client
let url = endpoint + (query ? "?" + query : "") let url = endpoint + (query ? "?" + query : "")
console.log("URL:", url)
const requestOptions = { const requestOptions = {
method, method,
mode: "cors", mode: "cors",

View File

@ -38,8 +38,6 @@ function obj2str(obj) {
if (obj) return obj if (obj) return obj
} }
/** /**
* clear SSR cache * clear SSR cache
*/ */

View File

@ -1,16 +1,19 @@
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.js")
const { obj2str, log } = require("../lib/utils")
;(function () { ;(function () {
/** @type {HookResponse} */ /** @type {HookResponse} */
var response = null let response = null
console.log("SSR GET READ")
var request = context.request() const request = context.request()
var url = request.query("url") let url = request.query("url")
var noCache = request.query("noCache") const noCache = request.query("noCache")
// add sentry trace id to head // add sentry trace id to head
var trace_id = context.debug.sentryTraceId() const trace_id = context.debug.sentryTraceId()
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>')
} }
@ -18,7 +21,9 @@ const { obj2str, log } = require("../lib/utils")
if (url) { if (url) {
// comment will be printed to html later // comment will be printed to html later
var comment = "" let comment = ""
/** @type {Date} */ // @ts-ignore
context.ssrCacheValidUntil = null
url = url.split("?")[0] url = url.split("?")[0]
comment += "url: " + url comment += "url: " + url
@ -31,7 +36,8 @@ const { obj2str, log } = require("../lib/utils")
} }
// check if url is in cache // check if url is in cache
var cache = /** @type {Ssr[]} */ // @ts-ignore
const cache =
!noCache && !noCache &&
context.db.find("ssr", { context.db.find("ssr", {
filter: { filter: {
@ -40,6 +46,7 @@ const { obj2str, log } = require("../lib/utils")
}) })
if (cache && cache.length) { if (cache && cache.length) {
// use cache // use cache
context.response.header("X-SSR-Cache", "true")
throw { throw {
status: 200, status: 200,
log: false, log: false,
@ -48,85 +55,50 @@ const { obj2str, log } = require("../lib/utils")
} }
// validate url // validate url
var status = 200 let status = 200
var pNorender = false let pNorender = false
var pNotfound = false let pNotfound = false
var pR = ssrValidatePath(url) const pR = ssrValidatePath(url)
if (pR < 0) { if (pR < 0) {
pNotfound = true pNotfound = true
} else if (!pR) { } else if (!pR) {
pNorender = true pNorender = true
} }
var head = "" let head = ""
var html = "" let html = ""
var error = "" let error = ""
comment += ", path: " + url comment += ", path: " + url
var cacheIt = false let cacheIt = false
if (pNorender) { if (pNorender) {
html = "<!-- NO SSR RENDERING -->" html = "<!-- NO SSR RENDERING -->"
} else if (pNotfound) { } else if (pNotfound) {
status = 404 status = 404
console.log("IS 404")
html = "404 NOT FOUND" html = "404 NOT FOUND"
} else { } else {
// try rendering, if error output plain html
try {
// @ts-ignore // @ts-ignore
context.ssrCache = {} context.ssrCache = {}
// @ts-ignore // @ts-ignore
context.ssrFetch = function (endpoint, options) { context.ssrRequest = ssrRequest
var data
if (ssrAllowedAPIEndpoints.indexOf(endpoint) > -1) {
var _options = Object.assign({}, options)
if (_options.sort) _options.sort = [_options.sort]
// try rendering, if error output plain html
try { 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 // include App.svelte and render it
// @ts-ignore // @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, url: url,
}) })
head = rendered.head head = rendered.head
html = rendered.html html = rendered.html
// add ssrCache to head // 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__ = " +
@ -137,8 +109,8 @@ const { obj2str, log } = require("../lib/utils")
// status from webapp // status from webapp
// @ts-ignore // @ts-ignore
if (context.is404) { if (context.is404) {
// console.log("########## 404")
status = 404 status = 404
console.log("IS 404")
} else { } else {
cacheIt = true cacheIt = true
} }
@ -151,15 +123,16 @@ const { obj2str, log } = require("../lib/utils")
} }
// read html template and replace placeholders // 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 + "-->" : "")
console.log("CACHE", cacheIt, noCache)
// save cache if adviced // save cache if adviced
if (cacheIt && !noCache) { if (cacheIt && !noCache) {
context.db.create("ssr", { context.db.create("ssr", {
// context.debug.dump("ssr", {
path: url, path: url,
content: tpl, content: tpl,
}) })
@ -173,7 +146,7 @@ const { obj2str, log } = require("../lib/utils")
} }
} else { } else {
// only admins are allowed to get without url parameter // only admins are allowed to get without url parameter
var auth = context.user.auth() const auth = context.user.auth()
if (!auth || auth.role !== 0) { if (!auth || auth.role !== 0) {
throw { throw {
status: 403, status: 403,

View File

@ -27,10 +27,10 @@
} }
onMount(() => { onMount(() => {
if (typeof window !== "undefined") {
// Initial check // Initial check
checkHomePage() checkHomePage()
checkScroll() checkScroll()
if (typeof window !== "undefined") {
// Listen for changes // Listen for changes
window.addEventListener("scroll", checkScroll) window.addEventListener("scroll", checkScroll)
window.addEventListener("popstate", checkHomePage) window.addEventListener("popstate", checkHomePage)
@ -45,9 +45,11 @@
$: { $: {
console.log($refresh) console.log($refresh)
if (typeof window !== "undefined") {
checkHomePage() checkHomePage()
checkScroll() checkScroll()
} }
}
let show = false let show = false
$: console.log(show) $: console.log(show)
</script> </script>

View File

@ -183,7 +183,7 @@
<div class="submenu-img"> <div class="submenu-img">
<img <img
src="{`${apiBaseURL}navigation/${$navigation?.id}/${submenu.image?.src}?filter=${ src="{`${apiBaseURL}navigation/${$navigation?.id}/${submenu.image?.src}?filter=${
window?.innerWidth > 500 ? 'xl' : 'm' typeof window !== 'undefined' && window?.innerWidth > 500 ? 'xl' : 'm'
}`}" }`}"
alt="img" alt="img"
/> />

View File

@ -218,7 +218,7 @@
<img <img
use:pushImages use:pushImages
src="{`${apiBaseURL}navigation/${$navigation.id}/${imgSrc}?filter=${ src="{`${apiBaseURL}navigation/${$navigation.id}/${imgSrc}?filter=${
window?.innerWidth > 500 ? 'xl' : 'm' typeof window !== 'undefined' && window?.innerWidth > 500 ? 'xl' : 'm'
}`}" }`}"
alt="img" alt="img"
class="img img-menu" class="img img-menu"

View File

@ -72,7 +72,7 @@
} }
} }
let innerWidth = window?.innerWidth || 0 let innerWidth = typeof window !== "undefined" ? window?.innerWidth || 0 : 0
if (typeof window !== "undefined") { if (typeof window !== "undefined") {
onMount(() => { onMount(() => {
const handleResize = () => { const handleResize = () => {

View File

@ -48,12 +48,13 @@
{#each siteImages as image, i (i)} {#each siteImages as image, i (i)}
<swiper-slide class="relative" id="imageSlide"> <swiper-slide class="relative" id="imageSlide">
<div class="image-container"> <div class="image-container">
{#if typeof window !== "undefined"}
<img <img
src="{`${apiBaseURL}content/${siteId}/${image.image?.src}?filter=${ src="{`${apiBaseURL}content/${siteId}/${image.image?.src}?filter=${
window?.innerWidth > 500 ? 'xl' : 'm' typeof window !== 'undefined' && window?.innerWidth > 500 ? 'xl' : 'm'
}`}" }`}"
alt="Bild" alt="Bild"
/> />{/if}
</div> </div>
</swiper-slide> </swiper-slide>
{/each} {/each}
@ -62,7 +63,9 @@
{:else if image} {:else if image}
<div class="image-container single flex"> <div class="image-container single flex">
<img <img
src="{`${apiBaseURL}content/${siteId}/${image.image?.src}?filter=${window?.innerWidth > 500 ? 'xl' : 'm'}`}" src="{`${apiBaseURL}content/${siteId}/${image.image?.src}?filter=${
typeof window !== 'undefined' && window?.innerWidth > 500 ? 'xl' : 'm'
}`}"
alt="Bild" alt="Bild"
/> />
</div> </div>

View File

@ -22,7 +22,7 @@
<div class="img-container"> <div class="img-container">
<img <img
src="{`${apiBaseURL}content/${siteId}/${col.image?.src}?filter=${ src="{`${apiBaseURL}content/${siteId}/${col.image?.src}?filter=${
window?.innerWidth > 500 ? 'xl' : 'm' typeof window !== 'undefined' && window?.innerWidth > 500 ? 'xl' : 'm'
}`}" }`}"
alt="img" alt="img"
/> />

View File

@ -9,7 +9,7 @@
<div class="imgContainer"> <div class="imgContainer">
<img <img
src="{`${apiBaseURL}content/${siteId}/${col.mainPicture?.src}?filter=${ src="{`${apiBaseURL}content/${siteId}/${col.mainPicture?.src}?filter=${
window?.innerWidth > 500 ? 'xl' : 'm' typeof window !== 'undefined' && window?.innerWidth > 500 ? 'xl' : 'm'
}`}" }`}"
alt="img" alt="img"
/> />

View File

@ -48,7 +48,7 @@
<div class="img-container"> <div class="img-container">
<img <img
src="{`${apiBaseURL}content/${siteId}/${product.image?.src}?filter=${ src="{`${apiBaseURL}content/${siteId}/${product.image?.src}?filter=${
window?.innerWidth > 500 ? 'xl' : 'm' typeof window !== 'undefined' && window?.innerWidth > 500 ? 'xl' : 'm'
}`}" }`}"
alt="img" alt="img"
/> />

View File

@ -6,7 +6,7 @@
</script> </script>
<main class="teaser"> <main class="teaser">
{#if index % 2 == 0 || window?.innerWidth < 1023} {#if index % 2 == 0 || typeof window !== 'undefined' && window?.innerWidth < 1023}
<Image siteId="{site?.id}" siteImages="{site?.teaserImages || []}" /> <Image siteId="{site?.id}" siteImages="{site?.teaserImages || []}" />
{/if} {/if}
<div class="content"> <div class="content">
@ -14,7 +14,7 @@
<p>{site?.teaserDescription}</p> <p>{site?.teaserDescription}</p>
<button on:click="{() => navigate(site.path)}">MEHR</button> <button on:click="{() => navigate(site.path)}">MEHR</button>
</div> </div>
{#if index % 2 == 1 && window?.innerWidth > 1023} {#if index % 2 == 1 && typeof window !== 'undefined' && window?.innerWidth > 1023}
<Image siteId="{site?.id}" siteImages="{site?.teaserImages || []}" /> <Image siteId="{site?.id}" siteImages="{site?.teaserImages || []}" />
{/if} {/if}
</main> </main>

7
types/global.d.ts vendored
View File

@ -6,7 +6,12 @@ interface ApiResult<T> {
data: T data: T
count: number count: number
} }
interface Ssr {
id?: string
path: string
content: string
validUntil: any // go Time
}
interface ApiOptions { interface ApiOptions {
method?: string method?: string
filter?: any filter?: any