Skip to content

Commit

Permalink
feat: new settings [image](https://vike.dev/image)
Browse files Browse the repository at this point in the history
  • Loading branch information
brillout committed Aug 2, 2024
1 parent c9a033a commit 48a0d1f
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 1 deletion.
3 changes: 3 additions & 0 deletions vike-solid/+config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ export default {
description: {
env: { server: true },
},
image: {
env: { server: true },
},
favicon: {
env: { server: true, client: true },
},
Expand Down
2 changes: 1 addition & 1 deletion vike-solid/renderer/getHeadSetting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { PageContext } from "vike/types";
import { isCallable } from "../utils/isCallable.js";

function getHeadSetting(
headSetting: "title" | "description" | "favicon" | "lang",
headSetting: "title" | "description" | "image" | "favicon" | "lang",
pageContext: PageContext,
): undefined | null | string {
const config = pageContext.configEntries[headSetting]?.[0];
Expand Down
5 changes: 5 additions & 0 deletions vike-solid/renderer/onRenderHtml.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@ const onRenderHtml: OnRenderHtmlAsync = async (pageContext): ReturnType<OnRender
const title = getHeadSetting("title", pageContext);
const favicon = getHeadSetting("favicon", pageContext);
const description = getHeadSetting("description", pageContext);
const image = getHeadSetting("image", pageContext);

const titleTag = !title ? "" : escapeInject`<title>${title}</title><meta property="og:title" content="${title}">`;
const faviconTag = !favicon ? "" : escapeInject`<link rel="icon" href="${favicon}" />`;
const descriptionTags = !description
? ""
: escapeInject`<meta name="description" content="${description}"><meta property="og:description" content="${description}">`;
const imageTags = !image
? ""
: escapeInject`<meta property="og:image" content="${image}"><meta name="twitter:card" content="summary_large_image">`;

const Head = pageContext.config.Head || (() => <></>);
const head = renderToString(() => (
Expand Down Expand Up @@ -56,6 +60,7 @@ const onRenderHtml: OnRenderHtmlAsync = async (pageContext): ReturnType<OnRender
${headHtml}
${faviconTag}
${descriptionTags}
${imageTags}
${dangerouslySkipEscape(generateHydrationScript())}
</head>
<body${dangerouslySkipEscape(bodyAttributesString)}>
Expand Down
15 changes: 15 additions & 0 deletions vike-solid/types/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,21 @@ declare global {
*/
description?: string | ((pageContext: PageContextServer) => string);

/**
* Set the page's preview image upon URL sharing.
*
* Generates:
* ```jsx
* <head>
* <meta property="og:image" content={image}>
* <meta name="twitter:card" content="summary_large_image">
* </head>
* ```
*
* https://vike.dev/image
*/
image?: string | ((pageContext: PageContextServer) => string);

/**
* Set the page's favicon.
*
Expand Down

0 comments on commit 48a0d1f

Please sign in to comment.