✨ refactor: streamline SSR setup and remove Babel configuration
- Updated import path for app.server module in SSR hook. - Removed babel.config.server.json as Babel is no longer needed for async/await transformation. - Adjusted esbuild configuration to target ESNext and modified output paths. - Enhanced App.svelte to handle initial content loading during SSR. - Updated SSR script to load messages synchronously before rendering. - Simplified build:server script in package.json by removing Babel step.
This commit is contained in:
@@ -5,7 +5,18 @@ var utils = require("./lib/utils")
|
||||
const collectionName = col && col.name ? col.name : null
|
||||
const req = context.request()
|
||||
const method = req.method
|
||||
const entryId = (context.data && !Array.isArray(context.data) && context.data.id) || req.param("id") || null
|
||||
const data = /** @type {Record<string, any>} */ (Array.isArray(context.data) ? {} : context.data || {})
|
||||
let entryId = data.id || req.param("id") || null
|
||||
|
||||
// DELETE hooks may not populate context.data.id or route params consistently.
|
||||
if (!entryId) {
|
||||
const path = req.path
|
||||
const segments = path.split("/")
|
||||
const last = segments[segments.length - 1]
|
||||
if (last && last.length >= 24) {
|
||||
entryId = last
|
||||
}
|
||||
}
|
||||
|
||||
utils.clearSSRCache(collectionName, entryId, method)
|
||||
})()
|
||||
|
||||
+29
-33
@@ -33,43 +33,39 @@ module.exports = {
|
||||
publishedFilter,
|
||||
ssrValidatePath: function (/** @type {string} */ path) {
|
||||
// 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
|
||||
// Starter project: SSR the app shell for language roots and all published content pages.
|
||||
if (path === "/" || path.match(/^\/(de|en)\/?$/)) {
|
||||
return 1
|
||||
}
|
||||
|
||||
// // / is fixed url
|
||||
// if (path == "/cart") return 1
|
||||
const langMatch = path.match(/^\/(de|en)(\/|$)/)
|
||||
const lang = langMatch ? langMatch[1] : null
|
||||
const routePath = lang ? path.replace(/^\/(de|en)(?=\/|$)/, "") || "/" : path
|
||||
|
||||
// // path starts with /products/ is product
|
||||
// if (path?.startsWith("/products/")) {
|
||||
// const slug = path?.replace(/^\/products\//, "")
|
||||
// const resp = context.db.find("product", {
|
||||
// filter: {
|
||||
// $and: [{ slug: slug }, publishedFilter],
|
||||
// },
|
||||
const resp = context.db.find("content", {
|
||||
filter: {
|
||||
$and: [
|
||||
...(lang ? [{ lang: lang }] : []),
|
||||
{ $or: [{ path: routePath }, { "alternativePaths.path": routePath }] },
|
||||
publishedFilter,
|
||||
],
|
||||
},
|
||||
selector: { _id: 1, path: 1, lang: 1 },
|
||||
})
|
||||
if (resp && resp.length) {
|
||||
const canonicalLang = resp[0].lang || lang
|
||||
const canonicalPath = resp[0].path || routePath
|
||||
const canonicalUrl = canonicalLang
|
||||
? `/${canonicalLang}${canonicalPath === "/" ? "" : canonicalPath}`
|
||||
: canonicalPath
|
||||
|
||||
// selector: { _id: 1 },
|
||||
// })
|
||||
// if (resp && resp.length) {
|
||||
// return 1
|
||||
// }
|
||||
// }
|
||||
if (canonicalUrl && canonicalUrl !== path) {
|
||||
return canonicalUrl
|
||||
}
|
||||
return 1
|
||||
}
|
||||
|
||||
// // // all other sites are in db
|
||||
// //path = path?.replace(/^\//, "")
|
||||
// const resp = context.db.find("content", {
|
||||
// filter: {
|
||||
// $and: [{ $or: [{ path }, { "alternativePaths.path": path }] }, publishedFilter],
|
||||
// },
|
||||
|
||||
// selector: { _id: 1 },
|
||||
// })
|
||||
// if (resp && resp.length) {
|
||||
// return 1
|
||||
// }
|
||||
|
||||
// not found
|
||||
return -1
|
||||
},
|
||||
ssrPublishCheckCollections: [
|
||||
/*"content", "product"*/
|
||||
],
|
||||
ssrPublishCheckCollections: ["content"],
|
||||
}
|
||||
|
||||
+11774
File diff suppressed because it is too large
Load Diff
@@ -4,6 +4,7 @@ const { release } = require("../config-client")
|
||||
|
||||
const { ssrValidatePath } = require("../config")
|
||||
const { ssrRequest } = require("../lib/ssr-server")
|
||||
const APP_SERVER_MODULE_PATH = "../lib/app.server"
|
||||
;(function () {
|
||||
var request = context.request()
|
||||
|
||||
@@ -128,7 +129,7 @@ const { ssrRequest } = require("../lib/ssr-server")
|
||||
try {
|
||||
// if error, output plain html without prerendering
|
||||
// @ts-ignore
|
||||
const app = require("../lib/app.server")
|
||||
const app = require(APP_SERVER_MODULE_PATH)
|
||||
|
||||
const rendered = app.default.render({
|
||||
url: url,
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
{
|
||||
"sourceMaps": "inline",
|
||||
"inputSourceMap": true,
|
||||
"plugins": [["@babel/plugin-transform-async-to-generator"]]
|
||||
}
|
||||
@@ -10,15 +10,21 @@ if (!fs.existsSync(__dirname + "/api/hooks/lib/buildInfo.js")) {
|
||||
}
|
||||
config.writeBuildInfo = null
|
||||
|
||||
config.options.sourcemap = "inline"
|
||||
config.options.sourcemap = true
|
||||
config.options.minify = false
|
||||
config.options.platform = "node"
|
||||
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"
|
||||
// Keep modern syntax that goja supports natively and downlevel only unsupported features.
|
||||
config.options.target = "esnext"
|
||||
config.options.supported = { "async-await": false, "async-generator": false, "dynamic-import": false }
|
||||
config.options.entryPoints = ["./frontend/src/ssr.ts"]
|
||||
config.options.outfile = __dirname + "/_temp/app.server.js"
|
||||
config.options.outfile = __dirname + "/api/hooks/lib/app.server.js"
|
||||
// Remove splitting-related options inherited from the frontend build.
|
||||
delete config.options.outdir
|
||||
delete config.options.splitting
|
||||
delete config.options.entryNames
|
||||
delete config.options.chunkNames
|
||||
delete config.options.outExtension
|
||||
config.options.plugins = [
|
||||
config.sveltePlugin({
|
||||
compilerOptions: {
|
||||
|
||||
@@ -213,8 +213,21 @@
|
||||
loading = false
|
||||
}
|
||||
|
||||
if (typeof window === "undefined") {
|
||||
;(() => {
|
||||
const initialPath = url.split("?")[0] || "/"
|
||||
const initialLang = extractLanguageFromPath(initialPath) || DEFAULT_LANGUAGE
|
||||
const initialRoutePath = stripLanguageFromPath(initialPath) || "/"
|
||||
loadContent(initialLang, initialRoutePath)
|
||||
})()
|
||||
}
|
||||
|
||||
// Re-load content when path or language changes
|
||||
$effect(() => {
|
||||
if (typeof window === "undefined") {
|
||||
return
|
||||
}
|
||||
|
||||
const lang = $currentLanguage
|
||||
const routePath = stripLanguageFromPath($location.path)
|
||||
loadContent(lang, routePath || "/")
|
||||
|
||||
+1
-1
@@ -5,7 +5,7 @@ import deLocale from "./lib/i18n/locales/de.json"
|
||||
import enLocale from "./lib/i18n/locales/en.json"
|
||||
import App from "./App.svelte"
|
||||
|
||||
// SSR: load messages synchronously (Babel transforms import → require)
|
||||
// SSR: load messages synchronously before rendering.
|
||||
addMessages("de", deLocale)
|
||||
addMessages("en", enLocale)
|
||||
|
||||
|
||||
+2
-5
@@ -12,7 +12,7 @@
|
||||
"start:mock": "MOCK=1 node scripts/esbuild-wrapper.js start",
|
||||
"start:ssr": "SSR=1 node scripts/esbuild-wrapper.js start",
|
||||
"build": "node scripts/esbuild-wrapper.js build",
|
||||
"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 --banner:js='// @ts-nocheck'",
|
||||
"build:server": "node scripts/esbuild-wrapper.js build esbuild.config.server.js",
|
||||
"test": "playwright test",
|
||||
"test:e2e": "playwright test tests/e2e",
|
||||
"test:api": "playwright test tests/api",
|
||||
@@ -23,9 +23,6 @@
|
||||
"tour:mobile": "playwright test --config=video-tours/playwright.config.ts --project=tour-mobile"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/cli": "^7.28.6",
|
||||
"@babel/core": "^7.29.0",
|
||||
"@babel/preset-env": "^7.29.5",
|
||||
"@playwright/test": "^1.59.1",
|
||||
"@tailwindcss/postcss": "^4.3.0",
|
||||
"@tsconfig/svelte": "^5.0.8",
|
||||
@@ -57,4 +54,4 @@
|
||||
"svelte-i18n": "^4.0.1"
|
||||
},
|
||||
"packageManager": "yarn@4.7.0"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user