Skip to content

Commit

Permalink
qual: improvements around getTitle
Browse files Browse the repository at this point in the history
  • Loading branch information
lourot committed Oct 24, 2023
1 parent 7f7930e commit d91999e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
12 changes: 10 additions & 2 deletions vike-solid/renderer/+config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,15 @@ const toggleSsrRelatedConfig: ConfigEffect = ({
export default {
onRenderHtml: "import:vike-solid/renderer/onRenderHtml:onRenderHtml",
onRenderClient: "import:vike-solid/renderer/onRenderClient:onRenderClient",

// A page can define an onBeforeRender() hook to be run on the server, which
// can fetch data and return it as additional page context. Typically it will
// return the page's root Solid component's props and additional data that can
// be used by the renderers.
// It is a cumulative config option, so a web app using vike-solid can extend
// this list.
passToClient: ["pageProps", "title"],

clientRouting: true,
hydrationCanBeAborted: true,
meta: {
Expand All @@ -46,7 +54,7 @@ export default {
env: "server-only",
},
favicon: {
env: 'server-only'
env: "server-only",
},
lang: {
env: "server-only",
Expand All @@ -67,7 +75,7 @@ declare global {
Layout?: Component;
title?: string | ((pageContext: PageContext) => string);
description?: string;
favicon?: string
favicon?: string;
/**
* @default 'en'
*/
Expand Down
16 changes: 11 additions & 5 deletions vike-solid/renderer/getTitle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,27 @@ export { getTitle };

import type { PageContext } from "vike/types";

/**
* Get the page's title if defined, either from the additional data fetched by
* the page's onBeforeRender() hook or from the config.
*/
function getTitle(pageContext: PageContext): null | string {
if (typeof pageContext.title === "string") {
if (pageContext.title !== undefined) {
return pageContext.title;
}
if (pageContext.title) {
throw new Error("pageContext.title should be a string");

const titleConfig = pageContext.configEntries.title?.[0];
if (!titleConfig) {
return null;
}
const { title } = pageContext.config;
const title = titleConfig.configValue;
if (typeof title === "string") {
return title;
}
if (!title) {
return null;
}
const { configDefinedAt } = pageContext.configEntries.title![0]!;
const { configDefinedAt } = titleConfig;
if (typeof title === "function") {
const val = title(pageContext);
if (typeof val === "string") {
Expand Down
10 changes: 8 additions & 2 deletions vike-solid/renderer/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@ type PageProps = Record<string, unknown>;
declare global {
namespace Vike {
interface PageContext {
Page: Page;
pageProps: Record<string, unknown>;
// Note: Page will typically be undefined in onRenderHtml() when setting the `ssr` config flag
// to `false` (SPA mode).
Page?: Page;

/** Properties of the page's root Solid component. */
pageProps?: Record<string, unknown>;

/** &lt;title>${title}&lt;/title> - has precedence over the config */
title?: string;
}
}
Expand Down

0 comments on commit d91999e

Please sign in to comment.