Page MenuHomeSealhub

customUrlView.tsx
No OneTemporary

customUrlView.tsx

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";
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.page
.list(ctx.$context)
.filter({
url: ctx.url.split("?")[0],
...(ctx.hostname !== main_domain
? { domain: ctx.hostname }
: { domain: "" }),
})
.fetch();
const pageContent = Array.isArray(page?.get("content"))
? (page?.get("content") as RawJDDocument)
: null;
if (page && pageContent) {
const jdd = await JDD.fromStorage(
registry,
makeJDDContext(ctx),
pageContent
);
const title = page.get("title");
const heading = page.get("heading");
const hideNavigation = page.get("hideNavigation") || false;
ctx.body = html({
ctx,
title: title || "Aspazja",
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: await imageRouter.singleImage(
await (page.get("imageForMetadata") as FilePointer).getPath(),
400,
"jpeg",
false
),
});
}
} catch (err) {
// eslint-disable-next-line no-console
console.log("error in custom url router middleware:", err);
} finally {
await next();
}
};

File Metadata

Mime Type
text/x-java
Expires
Tue, Dec 24, 14:02 (16 h, 46 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
556582
Default Alt Text
customUrlView.tsx (2 KB)

Event Timeline