Skip to content

Commit

Permalink
feat: support setting {body,html}Attributes based on pageContext
Browse files Browse the repository at this point in the history
  • Loading branch information
brillout committed Aug 12, 2024
1 parent 4c342bb commit ce05a56
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
2 changes: 1 addition & 1 deletion vike-solid/hooks/useConfig/useConfig-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function useConfig(): (config: ConfigFromHook) => void {
}

const configsClientSide = ["title"];
const configsCumulative = ["Head"] as const;
const configsCumulative = ["Head", "bodyAttributes", "htmlAttributes"] as const;
function setPageContextConfigFromHook(config: ConfigFromHook, pageContext: PageContext & PageContextInternal) {
pageContext._configFromHook ??= {};
objectKeys(config).forEach((configName) => {
Expand Down
4 changes: 2 additions & 2 deletions vike-solid/renderer/onRenderHtml.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ function getTagAttributes(pageContext: PageContextServer) {
// Don't set `lang` to its default value if it's `null` (so that users can set it to `null` in order to remove the default value)
if (lang === undefined) lang = "en";

const bodyAttributes = mergeTagAttributesList(pageContext.config.bodyAttributes);
const htmlAttributes = mergeTagAttributesList(pageContext.config.htmlAttributes);
const bodyAttributes = mergeTagAttributesList(getHeadSetting<TagAttributes[]>("bodyAttributes", pageContext));
const htmlAttributes = mergeTagAttributesList(getHeadSetting<TagAttributes[]>("htmlAttributes", pageContext));

const bodyAttributesString = getTagAttributesString(bodyAttributes);
const htmlAttributesString = getTagAttributesString({ ...htmlAttributes, lang: lang ?? htmlAttributes.lang });
Expand Down
6 changes: 3 additions & 3 deletions vike-solid/types/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,14 @@ declare global {
*
* https://vike.dev/htmlAttributes
*/
htmlAttributes?: TagAttributes;
htmlAttributes?: TagAttributes | ((pageContext: PageContextServer) => TagAttributes | undefined);

/**
* Add tag attributes such as `<body class="dark">`.
*
* https://vike.dev/bodyAttributes
*/
bodyAttributes?: TagAttributes;
bodyAttributes?: TagAttributes | ((pageContext: PageContextServer) => TagAttributes | undefined);

/**
* If `true`, the page is rendered twice: on the server-side (to HTML) and on the client-side (hydration).
Expand Down Expand Up @@ -177,7 +177,7 @@ type PickWithoutGetter<T, K extends keyof T> = {
};
export type ConfigFromHook = PickWithoutGetter<
Vike.Config,
"Head" | "title" | "description" | "image" | "favicon" | "lang" | "viewport"
"Head" | "title" | "description" | "image" | "favicon" | "lang" | "viewport" | "bodyAttributes" | "htmlAttributes"
>;
export type ConfigFromHookResolved = Omit<ConfigFromHook, ConfigFromHookCumulative> &
Pick<Vike.ConfigResolved, ConfigFromHookCumulative>;

0 comments on commit ce05a56

Please sign in to comment.