Skip to content

Commit

Permalink
Return head tags as array instead of string
Browse files Browse the repository at this point in the history
Fixes #4
  • Loading branch information
iksaku committed Oct 24, 2023
1 parent fdad291 commit f4981ce
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/createInertiaApp.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Page, PageResolver, setupProgress } from '@inertiajs/core'
import { renderTags } from '@solidjs/meta'
import { renderTags } from './meta'
import { Dynamic, createComponent, generateHydrationScript, isServer, renderToString } from 'solid-js/web'
import App, { InertiaAppProps } from './App'

Expand Down Expand Up @@ -27,7 +27,7 @@ type CreateInertiaSSROptions = CreateInertiaBaseOptions & {
setup?: never
progress?: never
}
export type CreateInertiaSSRReturnType = { head: string; body: string }
export type CreateInertiaSSRReturnType = { head: string[]; body: string }

export default async function createInertiaApp(options: CreateInertiaCSROptions): Promise<CreateInertiaCSRReturnType>
export default async function createInertiaApp(options: CreateInertiaSSROptions): Promise<CreateInertiaSSRReturnType>
Expand Down
42 changes: 42 additions & 0 deletions src/meta.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Original code by Ryan Carniato.
*
* Credits to the SolidJs team:
* https://github.com/solidjs/solid-meta/blob/main/src/index.tsx
*/

import { escape } from "solid-js/web";

interface TagDescription {
tag: string;
props: Record<string, unknown>;
setting?: {
escape?: boolean;
};
id: string;
name?: string;
ref?: Element;
}

// Render tags and return as an array instead of joining as a string
export function renderTags(tags: Array<TagDescription>): string[] {
return tags.map(tag => {
const keys = Object.keys(tag.props); // @ts-expect-error

const props = keys.map(k => k === "children" ? "" : ` ${k}="${escape(tag.props[k], true)}"`).join("");

if (tag.props.children) {
// Tags might contain multiple text children:
// <Title>example - {myCompany}</Title>
const children = Array.isArray(tag.props.children) ? tag.props.children.join("") : tag.props.children;

if (tag.setting?.escape && typeof children === "string") {
return `<${tag.tag} data-sm="${tag.id}"${props}>${escape(children)}</${tag.tag}>`;
}

return `<${tag.tag} data-sm="${tag.id}"${props}>${children}</${tag.tag}>`;
}

return `<${tag.tag} data-sm="${tag.id}"${props}/>`;
});
}

0 comments on commit f4981ce

Please sign in to comment.