@@ -39,7 +39,7 @@ export const Route = createFileRoute("/")({
3939
4040 return { meta , links };
4141 },
42- })
42+ });
4343```
4444
4545You can use it almost the same way as Next.js's [ ` generateMetadata ` ] ( https://nextjs.org/docs/app/api-reference/functions/generate-metadata ) function.
@@ -55,25 +55,50 @@ import { createMetadataGenerator } from "tanstack-meta";
5555const generateMetadata = createMetadataGenerator ({
5656 titleTemplate: {
5757 default: " Default Title" , // Used when title is not provided
58- template: " %s | My Site" // %s is replaced with the page title
59- }
58+ template: " %s | My Site" , // %s is replaced with the page title
59+ },
6060});
6161
6262// In your routes:
63- generateMetadata ({ title: " About" })
63+ generateMetadata ({ title: " About" });
6464// Output: <title>About | My Site</title>
6565
66- generateMetadata ({ title: null })
66+ generateMetadata ({ title: null });
6767// Output: <title>Default Title</title>
6868
69- generateMetadata ({})
69+ generateMetadata ({});
7070// Output: <title>Default Title</title>
7171```
7272
73+ You can also use a function for more complex title transformations:
74+
75+ ``` ts
76+ const generateMetadata = createMetadataGenerator ({
77+ titleTemplate: {
78+ default: " My Site" ,
79+ template : (title ) => ` ${title .toUpperCase ()} — My Site ` ,
80+ },
81+ });
82+
83+ generateMetadata ({ title: " about" });
84+ // Output: <title>ABOUT — My Site</title>
85+
86+ // Conditional logic example
87+ const generateMetadata = createMetadataGenerator ({
88+ titleTemplate: {
89+ default: " My Site" ,
90+ template : (title ) =>
91+ title .length > 50
92+ ? ` ${title .slice (0 , 47 )}... | My Site `
93+ : ` ${title } | My Site ` ,
94+ },
95+ });
96+ ```
97+
7398To opt out of the title template on a specific page, use ` title.absolute ` :
7499
75100``` ts
76- generateMetadata ({ title: { absolute: " Home" } })
101+ generateMetadata ({ title: { absolute: " Home" } });
77102// Output: <title>Home</title> (template is ignored)
78103```
79104
@@ -87,18 +112,18 @@ Similar to Next.js's `metadataBase`, you can use the `baseUrl` option to resolve
87112import { createMetadataGenerator } from " tanstack-meta" ;
88113
89114const generateMetadata = createMetadataGenerator ({
90- baseUrl: " https://example.com"
115+ baseUrl: " https://example.com" ,
91116});
92117
93118// Relative URLs are resolved to absolute URLs
94119generateMetadata ({
95120 openGraph: {
96- images: " /og.png"
121+ images: " /og.png" ,
97122 },
98123 alternates: {
99- canonical: " /about"
100- }
101- })
124+ canonical: " /about" ,
125+ },
126+ });
102127// Output:
103128// <meta property="og:image" content="https://example.com/og.png" />
104129// <link rel="canonical" href="https://example.com/about" />
@@ -108,7 +133,7 @@ You can also pass a `URL` object:
108133
109134``` ts
110135const generateMetadata = createMetadataGenerator ({
111- baseUrl: new URL (" https://example.com" )
136+ baseUrl: new URL (" https://example.com" ),
112137});
113138```
114139
@@ -117,9 +142,9 @@ Absolute URLs are preserved unchanged:
117142``` ts
118143generateMetadata ({
119144 openGraph: {
120- images: " https://cdn.example.com/og.png"
121- }
122- })
145+ images: " https://cdn.example.com/og.png" ,
146+ },
147+ });
123148// Output: <meta property="og:image" content="https://cdn.example.com/og.png" />
124149```
125150
@@ -128,15 +153,15 @@ You can combine `baseUrl` with `titleTemplate`:
128153``` ts
129154const generateMetadata = createMetadataGenerator ({
130155 titleTemplate: { default: " My Site" , template: " %s | My Site" },
131- baseUrl: " https://example.com"
156+ baseUrl: " https://example.com" ,
132157});
133158
134159generateMetadata ({
135160 title: " About" ,
136161 openGraph: {
137- images: " /og.png"
138- }
139- })
162+ images: " /og.png" ,
163+ },
164+ });
140165// Output:
141166// <title>About | My Site</title>
142167// <meta property="og:image" content="https://example.com/og.png" />
@@ -166,7 +191,7 @@ An options object with the following properties:
166191
167192- ` titleTemplate ` (optional): An object containing:
168193 - ` default ` : The default title used when no title is provided
169- - ` template ` : A template string where ` %s ` is replaced with the page title
194+ - ` template ` : A template string where ` %s ` is replaced with the page title, or a function that receives the page title and returns the formatted title
170195- ` baseUrl ` (optional): A string or ` URL ` object used to resolve relative URLs to absolute URLs. Applies to:
171196 - ` openGraph ` (images, audio, videos, url)
172197 - ` twitter ` (images, players, app URLs)
0 commit comments