UNPKG

@softr/sdk

Version:

The SDK for building Softr apps on TanStack Start. It lets an app interact with Softr: auth, datasources, workflows and integrations.

123 lines (122 loc) • 4.81 kB
//#region src/route/page.ts /** * Declares a route's settings: its name in Studio, who may open it, its title and its SEO tags. * * One call per route file, at module scope, with an object literal. Studio * reads and rewrites each setting written out as a literal; a computed one is * changed in the code. A head setting that depends on the route's data is a * function of its head context. The route's `beforeLoad` and `head` apply it. * * @example * const pageSettings = definePage({ * visibility: { userGroupTypes: ["LOGGED_IN_USERS"] }, * title: ({ loaderData }) => loaderData?.name, * }) * * export const Route = createFileRoute("/_app/projects/$projectId")({ * beforeLoad: guardRoute(pageSettings), * loader: ({ params }) => loadProject({ data: params.projectId }), * head: pageHead(pageSettings), * component: Project, * }) */ function definePage(settings) { return settings; } /** * The router keeps the deepest route's tag for each title, name and property, * so a layout's settings reach every page under it that does not state its own. * A canonical URL names one page, so only the page the visitor is on renders * its own. * * With `seo.enabled` in softr.json, a page also gets what search engines and * link previews expect when it states nothing else: a canonical link to its own * address, `og:url`, `og:title` and `og:description` from its title and * description, and an absolute `og:image`. * * Returned rather than called with the route's context: a `head` written as * `(ctx) => …` before `loader` stops TypeScript inferring the loader's types. * * @example * head: pageHead(pageSettings) */ function pageHead(page) { return (ctx) => { const resolve = (setting) => typeof setting === "function" ? setting(ctx) : setting; const text = (setting) => { const value = resolve(setting); return typeof value === "string" && value !== "" ? value : void 0; }; const defaults = typeof __SOFTR_CONFIG__ !== "undefined" && __SOFTR_CONFIG__.seo?.enabled === true; const { match } = ctx; const context = "context" in match ? match.context : void 0; const origin = typeof context === "object" && context !== null && "origin" in context && typeof context.origin === "string" ? context.origin : void 0; const absolute = (url) => defaults && origin !== void 0 && url.startsWith("/") && !url.startsWith("//") ? origin + url : url; const isVisitedPage = ctx.matches.at(-1)?.id === match.id; const indexing = resolve(page.searchEngineIndexing); const title = text(page.title); const description = text(page.description); const socialTitle = text(page.socialTitle) ?? (defaults ? title : void 0); const socialDescription = text(page.socialDescription) ?? (defaults ? description : void 0); const socialImage = text(page.socialImage); let ownAddress; if (defaults && origin !== void 0 && "pathname" in match && typeof match.pathname === "string") { const path = match.pathname.length > 1 ? match.pathname.replace(/\/+$/, "") : match.pathname; const search = "search" in match ? match.search : void 0; ownAddress = `${origin}${path}${typeof search === "object" && search !== null && "page" in search && (typeof search.page === "number" || typeof search.page === "string") ? `?page=${encodeURIComponent(String(search.page))}` : ""}`; } const stated = text(page.canonicalUrl); const canonical = !isVisitedPage ? void 0 : stated !== void 0 ? absolute(stated) : ownAddress; return { meta: [ ...title === void 0 ? [] : [{ title }], ...description === void 0 ? [] : [{ name: "description", content: description }], ...socialTitle === void 0 ? [] : [{ property: "og:title", content: socialTitle }], ...socialDescription === void 0 ? [] : [{ property: "og:description", content: socialDescription }], ...socialImage === void 0 ? [] : [{ property: "og:image", content: absolute(socialImage) }], ...defaults && canonical !== void 0 ? [{ property: "og:url", content: canonical }] : [], ...typeof indexing === "boolean" ? [{ name: "robots", content: indexing ? "index" : "noindex" }] : [] ], links: canonical === void 0 ? [] : [{ rel: "canonical", href: canonical }] }; }; } /** * Structured data for search engines, as a `head` script entry. Record text is * safe in it: a value such as `<\/script>` cannot close the tag. * * @example * head: (ctx) => ({ * ...pageHead(pageSettings)(ctx), * scripts: [jsonLdScript({ "@context": "https://schema.org", "@type": "Product", name: ctx.loaderData?.fields.name })], * }) */ function jsonLdScript(data) { return { type: "application/ld+json", children: JSON.stringify(data).replaceAll("<", "\\u003c") }; } //#endregion export { definePage, jsonLdScript, pageHead };