diff --git a/src/back/config.ts b/src/back/config.ts index 4ce61c6..c78464e 100644 --- a/src/back/config.ts +++ b/src/back/config.ts @@ -1,34 +1,37 @@ import _locreq from "locreq"; const locreq = _locreq(import.meta.dirname); import dotenv from "dotenv"; dotenv.config(); export const SEALIOUS_SANITY = Boolean(process.env.SEALIOUS_SANITY); export const PORT = process.env.SEALIOUS_PORT ? parseInt(process.env.SEALIOUS_PORT) : 8080; export const BASE_URL = process.env.SEALIOUS_BASE_URL || `http://localhost:${PORT}`; export const MONGO_PORT = process.env.SEALIOUS_MONGO_PORT ? parseInt(process.env.SEALIOUS_MONGO_PORT) : 20767; export const MONGO_HOST = process.env.SEALIOUS_MONGO_HOST || "127.0.0.1"; export const MAILCATCHER_HOST = process.env.SEALIOUS_MAILCATCHER_HOST || "127.0.0.1"; export const MAILCATCHER_SMTP_PORT = parseInt( process.env.SEALIOUS_MAILCATCHER_SMTP_PORT || "1026" ); export const MAILCATCHER_API_PORT = parseInt( process.env.SEALIOUS_MAILCATCHER_API_PORT || "1082" ); export const MAILER = process.env.SEALIOUS_MAILER || "mailcatcher"; export const DEFAULT_HTML_LANG = "pl"; +// if enabled, domain name will be checked when matching cusom pages from DB by URL. Otherwise only the path part of the URL is used +export const MULTI_DOMAIN_ENABLED = process.env.MULTI_DOMAIN_ENABLED == "true" || false; + export const IMAGE_CACHE_FS_DIR = process.env.IMAGE_CACHE_FS_DIR || locreq.resolve("cache/images"); export const SMARTCROP_CACHE_FS_DIR = process.env.IMAGE_CACHE_FS_DIR || locreq.resolve("cache/smartcrop"); export const UPLOADS_FS_DIR = process.env.UPLOADS_FS_DIR || locreq.resolve("uploaded_files"); export const MEILISEARCH_MASTER_KEY = process.env.MEILISEARCH_MASTER_KEY || "qwerty"; export const MEILISEARCH_HOST = process.env.MEILISEARCH_HOST || "http://localhost:1098"; diff --git a/src/back/routes/middlewares/customUrlView.tsx b/src/back/routes/middlewares/customUrlView.tsx index 0a6292b..27d5707 100644 --- a/src/back/routes/middlewares/customUrlView.tsx +++ b/src/back/routes/middlewares/customUrlView.tsx @@ -1,83 +1,88 @@ import type { Context, Next } from "koa"; import { TempstreamJSX } from "tempstream"; import { JDD } from "@sealcode/jdd"; import type TheApp from "../../app.js"; import { registry } from "../../jdd-components/components.js"; import { makeJDDContext } from "../../jdd-context.js"; import type { RawJDDocument } from "@sealcode/jdd"; import { imageRouter } from "../../image-router.js"; import type { FilePointer } from "@sealcode/file-manager"; import html from "../../html.js"; import { tempstream } from "tempstream"; import { defaultHead } from "../../defaultHead.js"; -import { BASE_URL } from "../../config.js"; +import { BASE_URL, MULTI_DOMAIN_ENABLED } from "../../config.js"; interface ContextState { jddNames: string[]; } /* eslint-disable @typescript-eslint/consistent-type-assertions */ export const customUrlView = (app: TheApp) => async (ctx: Context & ContextState, next: Next) => { try { if (ctx.body) return; const main_domain = new URL(BASE_URL).hostname; - const { - items: [page], - } = await app.collections.pages - .list(ctx.$context) - .filter({ - url: ctx.url.split("?")[0], + let filter = { + url: ctx.url.split("?")[0], + }; + + if (MULTI_DOMAIN_ENABLED) { + filter = { + ...filter, ...(ctx.hostname !== main_domain ? { domain: ctx.hostname } : { domain: "" }), - }) - .fetch(); + }; + } + + const { + items: [page], + } = await app.collections.pages.list(ctx.$context).filter(filter).fetch(); const pageContent = Array.isArray(page?.get("content")) ? (page?.get("content") as RawJDDocument) : null; if (page && pageContent) { const jdd_context = makeJDDContext(ctx); const jdd = await JDD.fromStorage(registry, jdd_context, pageContent); const title = page.get("title"); const heading = page.get("heading"); const hideNavigation = page.get("hideNavigation") || false; const metaImage = (page.get("imageForMetadata") as FilePointer) || undefined; ctx.body = html({ ctx, title: title || ctx.$app.manifest.name, description: page.get("description") || "", css_clumps: jdd.getAllCSSClumps(), hideNavigation, htmlOptions: { showFooter: !hideNavigation, }, body: ( <main class="custom-page jdd-outer-container"> {heading?.trim() ? <h1>{heading}</h1> : ""} <div class="jdd-container">{jdd.render()}</div> </main> ), makeHead: (...args) => tempstream`${defaultHead(...args)}${jdd.renderEarlyAssets()}`, metaImage: metaImage ? await imageRouter.singleImage( await metaImage.getPath(), 400, "jpeg", false ) : "", }); } } catch (err) { // eslint-disable-next-line no-console console.log("error in custom url router middleware:", err); } finally { await next(); } };