@softr/sdk
Version:
The SDK for building Softr apps on TanStack Start. It lets an app interact with Softr: auth, datasources, workflows and integrations.
79 lines (78 loc) • 2.46 kB
JavaScript
//#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.
*
* 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 tag = (setting, make) => {
const value = resolve(setting);
return typeof value === "string" && value !== "" ? [make(value)] : [];
};
const indexing = resolve(page.searchEngineIndexing);
return {
meta: [
...tag(page.title, (title) => ({ title })),
...tag(page.description, (content) => ({
name: "description",
content
})),
...tag(page.socialTitle, (content) => ({
property: "og:title",
content
})),
...tag(page.socialDescription, (content) => ({
property: "og:description",
content
})),
...tag(page.socialImage, (content) => ({
property: "og:image",
content
})),
...typeof indexing === "boolean" ? [{
name: "robots",
content: indexing ? "index" : "noindex"
}] : []
],
links: ctx.matches.at(-1)?.id === ctx.match.id ? tag(page.canonicalUrl, (href) => ({
rel: "canonical",
href
})) : []
};
};
}
//#endregion
export { definePage, pageHead };