SSR
All checks were successful
deploy to production / deploy (push) Successful in 1m7s

This commit is contained in:
Robin Grenzdörfer 2023-12-06 20:11:49 +00:00
parent 82f2bf9233
commit d3b36a6716
24 changed files with 166 additions and 119 deletions

1
.env
View File

@ -5,3 +5,4 @@ UID=100
GID=101 GID=101
RELEASE_ORG_SLUG=webmakers-gmbh RELEASE_ORG_SLUG=webmakers-gmbh
RELEASE_PROJECT_SLUG=allkids_erfurt RELEASE_PROJECT_SLUG=allkids_erfurt
START_SCRIPT=:ssr

View File

@ -6,13 +6,10 @@ module.exports = {
// / is de home // / is de home
if (path == "/") return 1 if (path == "/") return 1
// all other sites are in db
path = path?.replace(/^\//, "")
// 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 }, { "alternativePaths.path": path }], $or: [{ path }],
}, },
selector: { _id: 1 }, selector: { _id: 1 },
}) })

View File

@ -71,6 +71,7 @@ const { obj2str, log } = require("../lib/utils")
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 rendering, if error output plain html
@ -137,6 +138,7 @@ const { obj2str, log } = require("../lib/utils")
// @ts-ignore // @ts-ignore
if (context.is404) { if (context.is404) {
status = 404 status = 404
console.log("IS 404")
} else { } else {
cacheIt = true cacheIt = true
} }
@ -154,7 +156,7 @@ const { obj2str, log } = require("../lib/utils")
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", {

View File

@ -30,7 +30,7 @@ services:
- ./tmp/nonexistent:/nonexistent - ./tmp/nonexistent:/nonexistent
- ./tmp/.npm:/.npm - ./tmp/.npm:/.npm
working_dir: /data working_dir: /data
command: sh -c "yarn install && API_BASE=http://tibiserver:8080/api/v1/_/${TIBI_NAMESPACE} yarn start" command: sh -c "yarn install && API_BASE=http://tibiserver:8080/api/v1/_/${TIBI_NAMESPACE} yarn start${START_SCRIPT}"
expose: expose:
- 3000 - 3000
labels: labels:

View File

@ -83,7 +83,7 @@ if (process.argv[2] == "start") {
) )
// if SSR env variable is set // if SSR env variable is set
console.log(process.env) console.log(process.env, "=========================ENV")
if (process.env.SSR) { if (process.env.SSR) {
// read api/config.yml.env and read SSR_TOKEN variable from it // read api/config.yml.env and read SSR_TOKEN variable from it
const configEnv = fs.readFileSync(__dirname + "/api/config.yml.env", "utf8") const configEnv = fs.readFileSync(__dirname + "/api/config.yml.env", "utf8")

View File

@ -1,6 +1,7 @@
AddType application/javascript .mjs AddType application/javascript .mjs
#DirectoryIndex spa.html #DirectoryIndex spa.html
# notwendig, da sonst über normale url spa.html aufgerufen wird, muss nur datei name sei, der nicht existiert
DirectoryIndex noindex DirectoryIndex noindex
<ifModule mod_rewrite.c> <ifModule mod_rewrite.c>
@ -11,6 +12,8 @@ DirectoryIndex noindex
RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-d
# leitet initale request an backend und nicht an spa.html weiter
RewriteRule ^/?(.*)$ http://tibi-server:8080/api/v1/_/allkids_erfurt/ssr?token=owshwerNwoa&url=/$1 [P,QSA,L] RewriteRule ^/?(.*)$ http://tibi-server:8080/api/v1/_/allkids_erfurt/ssr?token=owshwerNwoa&url=/$1 [P,QSA,L]
# standardmäßig wegen deeplink aus google notwendig, da sonst 404
#RewriteRule (.*) /spa.html [QSA,L] #RewriteRule (.*) /spa.html [QSA,L]
</ifModule> </ifModule>

View File

@ -64,34 +64,50 @@
}) })
let cookiesAllowed = false let cookiesAllowed = false
window.addEventListener("ccAccept", (e) => { if (typeof window !== "undefined") {
// @ts-ignore test window.addEventListener("ccAccept", (e) => {
if (e.detail[1] == cookieName) cookiesAllowed = true // @ts-ignore test
}) if (e.detail[1] == cookieName) cookiesAllowed = 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 cookiesAllowed = checkCookie("googleAnalytics")
} }
cookiesAllowed = checkCookie("googleAnalytics")
</script> </script>
<svelte:head> <svelte:head>
{#if cookiesAllowed} {#if cookiesAllowed}
<!-- Google tag (gtag.js) -->
<!-- Google tag (gtag.js) -->
<!-- Google tag (gtag.js) -->
<!-- Google tag (gtag.js) -->
<!-- Google tag (gtag.js) -->
<!-- Google tag (gtag.js) -->
<!-- Google tag (gtag.js) -->
<!-- Google tag (gtag.js) -->
<!-- Google tag (gtag.js) -->
<!-- Google tag (gtag.js) -->
<!-- Google tag (gtag.js) -->
<!-- Google tag (gtag.js) -->
<!-- Google tag (gtag.js) -->
<!-- Google tag (gtag.js) -->
<!-- Google tag (gtag.js) --> <!-- Google tag (gtag.js) -->
<!-- Google tag (gtag.js) --> <!-- Google tag (gtag.js) -->
<!-- Google tag (gtag.js) --> <!-- Google tag (gtag.js) -->

View File

@ -9,11 +9,12 @@
mitte: "center", mitte: "center",
unten: "flex-end", unten: "flex-end",
} }
if (typeof window !== "undefined") {
window.addEventListener("ccAccept", (e) => { window.addEventListener("ccAccept", (e) => {
// @ts-ignore // @ts-ignore
if (e.detail[1] == cookieName) contentShown = true if (e.detail[1] == cookieName) contentShown = true
}) })
}
//isCookieSet isnt really precise //isCookieSet isnt really precise
function checkCookie(cookieName: string) { function checkCookie(cookieName: string) {
// Get all cookies // Get all cookies

View File

@ -29,15 +29,16 @@
// Initial check // Initial check
checkHomePage() checkHomePage()
checkScroll() checkScroll()
if (typeof window !== "undefined") {
// Listen for changes
window.addEventListener("scroll", checkScroll)
window.addEventListener("popstate", checkHomePage)
// Listen for changes return () => {
window.addEventListener("scroll", checkScroll) // Cleanup event listeners
window.addEventListener("popstate", checkHomePage) window.removeEventListener("scroll", checkScroll)
window.removeEventListener("popstate", checkHomePage)
return () => { }
// Cleanup event listeners
window.removeEventListener("scroll", checkScroll)
window.removeEventListener("popstate", checkHomePage)
} }
}) })
let show = false let show = false

View File

@ -8,7 +8,6 @@
function changeStateOfSite(menuOn: boolean) { function changeStateOfSite(menuOn: boolean) {
let siteContainer = document.body let siteContainer = document.body
show = menuOn show = menuOn
console.log("TEST")
if (menuOn) siteContainer.style.overflow = "hidden" if (menuOn) siteContainer.style.overflow = "hidden"
else siteContainer.style.overflow = "initial" else siteContainer.style.overflow = "initial"
} }
@ -59,8 +58,9 @@
textElement.style.letterSpacing = `${letterSpacing}px` textElement.style.letterSpacing = `${letterSpacing}px`
}*/ }*/
} }
if (typeof window !== "undefined") {
window.addEventListener("resize", stretchText) window.addEventListener("resize", stretchText)
}
onMount(stretchText) onMount(stretchText)
</script> </script>
@ -186,7 +186,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' window?.innerWidth > 500 ? 'xl' : 'm'
}`}" }`}"
alt="img" alt="img"
/> />

View File

@ -30,13 +30,15 @@
function imageSlide(images: HTMLImageElement[]) { function imageSlide(images: HTMLImageElement[]) {
let currentImage = 0 let currentImage = 0
images[0].classList.add("show-img") images[0].classList.add("show-img")
let interval = setInterval(() => { if (typeof window !== "undefined") {
images[currentImage].classList.remove("show-img") let interval = setInterval(() => {
currentImage += 1 images[currentImage].classList.remove("show-img")
if (images.length == currentImage) currentImage = 0 currentImage += 1
images[currentImage].classList.add("show-img") if (images.length == currentImage) currentImage = 0
}, 4000) images[currentImage].classList.add("show-img")
return () => clearInterval(interval) }, 4000)
return () => clearInterval(interval)
}
} }
function pushImages(node: HTMLImageElement) { function pushImages(node: HTMLImageElement) {
@ -44,11 +46,14 @@
} }
$: { $: {
const images = Array.from(document.getElementsByClassName("img-menu")) as HTMLImageElement[] if (typeof window !== "undefined") {
if (images.length !== 0) { const images = Array.from(document.getElementsByClassName("img-menu")) as HTMLImageElement[]
imageSlide(images) if (images.length !== 0) {
imageSlide(images)
}
} }
} }
function stretchText() { function stretchText() {
let container = document.getElementById("logo-container") let container = document.getElementById("logo-container")
let textElement = document.getElementById("upper") let textElement = document.getElementById("upper")
@ -83,9 +88,10 @@
textElement.style.letterSpacing = `${newLetterSpacing}px` textElement.style.letterSpacing = `${newLetterSpacing}px`
} }
} }
if (typeof window !== "undefined") {
window.addEventListener("resize", stretchText) window.addEventListener("resize", stretchText)
onMount(stretchText) onMount(stretchText)
}
</script> </script>
<ul> <ul>
@ -111,7 +117,7 @@
<button <button
class="button-three" class="button-three"
on:click="{(e) => { on:click="{(e) => {
let element = document.getElementById('menu') let element = document?.getElementById('menu')
if (element) changeStateOfSite(element.classList.contains('show-menu')) if (element) changeStateOfSite(element.classList.contains('show-menu'))
}}" }}"
aria-controls="primary-navigation" aria-controls="primary-navigation"
@ -210,7 +216,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' window?.innerWidth > 500 ? 'xl' : 'm'
}`}" }`}"
alt="img" alt="img"
class="img img-menu" class="img img-menu"

View File

@ -31,13 +31,16 @@
firstSlideH1.classList.add("active") firstSlideH1.classList.add("active")
} }
} }
const el = document.getElementById("HP") if (typeof window !== "undefined") {
let heightOfEl = el?.clientHeight const el = document.getElementById("HP")
const hp = document.getElementById("Homepage") let heightOfEl = el?.clientHeight
const homepage_placeholder = document.getElementById("homepage-placeholder") const hp = document.getElementById("Homepage")
if (hp && window.innerHeight < 600) { const homepage_placeholder = document.getElementById("homepage-placeholder")
hp.style.minHeight = (heightOfEl || window.innerHeight) + 200 + "px"
homepage_placeholder.style.minHeight = (heightOfEl || window.innerHeight) + 350 + "px" if (hp && window.innerHeight < 600) {
hp.style.minHeight = (heightOfEl || window.innerHeight) + 200 + "px"
homepage_placeholder.style.minHeight = (heightOfEl || window.innerHeight) + 350 + "px"
}
} }
}) })

View File

@ -13,11 +13,13 @@
} }
onMount(() => { onMount(() => {
interval = setInterval(() => { if (typeof window !== "undefined") {
currentIndex = (currentIndex + 1) % $banner.length interval = setInterval(() => {
}, 7000) currentIndex = (currentIndex + 1) % $banner.length
}, 7000)
return () => clearInterval(interval) return () => clearInterval(interval)
}
}) })
</script> </script>

View File

@ -71,20 +71,22 @@
sendForm(formObj) sendForm(formObj)
} }
} }
let innerWidth = window.innerWidth
onMount(() => { let innerWidth = window?.innerWidth || 0
const handleResize = () => { if (typeof window !== "undefined") {
innerWidth = window.innerWidth onMount(() => {
} const handleResize = () => {
innerWidth = window.innerWidth
}
window.addEventListener("resize", handleResize) window.addEventListener("resize", handleResize)
// Cleanup function // Cleanup function
return () => { return () => {
window.removeEventListener("resize", handleResize) window.removeEventListener("resize", handleResize)
} }
}) })
}
</script> </script>
{#if formSend} {#if formSend}

View File

@ -47,13 +47,15 @@
} }
onMount(() => { onMount(() => {
const inputEl = document.querySelector(".svelte-select input") if (typeof window !== "undefined") {
if (inputEl) { const inputEl = document.querySelector(".svelte-select input")
inputEl.addEventListener("input", handleInputChange)
}
return () => {
if (inputEl) { if (inputEl) {
inputEl.removeEventListener("input", handleInputChange) inputEl.addEventListener("input", handleInputChange)
}
return () => {
if (inputEl) {
inputEl.removeEventListener("input", handleInputChange)
}
} }
} }
}) })

View File

@ -50,7 +50,7 @@
<div class="image-container"> <div class="image-container">
<img <img
src="{`${apiBaseURL}content/${siteId}/${image.image?.src}?filter=${ src="{`${apiBaseURL}content/${siteId}/${image.image?.src}?filter=${
window.innerWidth > 500 ? 'xl' : 'm' window?.innerWidth > 500 ? 'xl' : 'm'
}`}" }`}"
alt="Bild" alt="Bild"
/> />
@ -62,7 +62,7 @@
{: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=${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' 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' 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' window?.innerWidth > 500 ? 'xl' : 'm'
}`}" }`}"
alt="img" alt="img"
/> />

View File

@ -13,22 +13,25 @@
// Jump down by 100vh // Jump down by 100vh
window.scrollTo({ top: window.innerHeight, behavior: "smooth" }) window.scrollTo({ top: window.innerHeight, behavior: "smooth" })
} }
if (typeof window !== "undefined") {
onMount(() => {
// Attach scroll event listener when component is mounted
window.addEventListener("scroll", checkScroll)
})
onMount(() => { onDestroy(() => {
// Attach scroll event listener when component is mounted // Remove scroll event listener when component is destroyed
window.addEventListener("scroll", checkScroll) window.removeEventListener("scroll", checkScroll)
}) })
}
onDestroy(() => {
// Remove scroll event listener when component is destroyed
window.removeEventListener("scroll", checkScroll)
})
let force = true let force = true
setInterval(() => { if (typeof window !== "undefined") {
if (location.pathname != "/") { setInterval(() => {
force = false if (location.pathname != "/") {
} else force = true force = false
}, 1000) } else force = true
}, 1000)
}
$: { $: {
/* if ($rerender) { /* if ($rerender) {
if (location.pathname != "/") { if (location.pathname != "/") {

View File

@ -12,16 +12,17 @@
// Scroll smoothly to the top // Scroll smoothly to the top
window.scrollTo({ top: 0, behavior: "smooth" }) window.scrollTo({ top: 0, behavior: "smooth" })
} }
if (typeof window !== "undefined") {
onMount(() => {
// Attach scroll event listener when component is mounted
window.addEventListener("scroll", checkScroll)
})
onMount(() => { onDestroy(() => {
// Attach scroll event listener when component is mounted // Remove scroll event listener when component is destroyed
window.addEventListener("scroll", checkScroll) window.removeEventListener("scroll", checkScroll)
}) })
}
onDestroy(() => {
// Remove scroll event listener when component is destroyed
window.removeEventListener("scroll", checkScroll)
})
</script> </script>
{#if showButton} {#if showButton}

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 || 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 && window?.innerWidth > 1023}
<Image siteId="{site?.id}" siteImages="{site?.teaserImages || []}" /> <Image siteId="{site?.id}" siteImages="{site?.teaserImages || []}" />
{/if} {/if}
</main> </main>

View File

@ -1,5 +1,10 @@
<script> <script>
import { navigate } from "svelte-routing" import { navigate } from "svelte-routing"
// set 404 for ssr
if (typeof window === "undefined") {
// @ts-ignore
if (context) context.is404 = true
}
</script> </script>
<svelte:head> <svelte:head>

View File

@ -9,8 +9,10 @@
"scripts": { "scripts": {
"validate": "svelte-check && tsc --noEmit", "validate": "svelte-check && tsc --noEmit",
"dev": "node scripts/esbuild-wrapper.js watch", "dev": "node scripts/esbuild-wrapper.js watch",
"start": "NAMESPACE=renz_shop node scripts/esbuild-wrapper.js start", "start": "node scripts/esbuild-wrapper.js start",
"start:ssr": "SSR=1 node scripts/esbuild-wrapper.js start",
"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: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 _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: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",