Skip to content

Commit 5b39869

Browse files
committed
readme
1 parent 6b47385 commit 5b39869

1 file changed

Lines changed: 69 additions & 2 deletions

File tree

packages/meta/README.md

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export const Route = createFileRoute("/")({
4242
})
4343
```
4444

45-
You can use it almost the same way as Next.js's [`generateMetadata`](https://nextjs.org/docs/app/api-reference/functions/generate-metadata) function, but note that currently there is no equivalent option for `metadataBase`.
45+
You can use it almost the same way as Next.js's [`generateMetadata`](https://nextjs.org/docs/app/api-reference/functions/generate-metadata) function.
4646

4747
### Title Template
4848

@@ -79,6 +79,63 @@ generateMetadata({ title: { absolute: "Home" } })
7979

8080
`%s` placeholders are all replaced. For example, `template: "%s | %s | My Site"` with `title: "Docs"` renders `<title>Docs | Docs | My Site</title>`.
8181

82+
### Base URL
83+
84+
Similar to Next.js's `metadataBase`, you can use the `baseUrl` option to resolve relative URLs to absolute URLs for metadata fields like `icons`, `openGraph`, `twitter`, `alternates`, `appLinks`, `manifest`, `assets`, `archives`, and `bookmarks`:
85+
86+
```ts
87+
import { createMetadataGenerator } from "tanstack-meta";
88+
89+
const generateMetadata = createMetadataGenerator({
90+
baseUrl: "https://example.com"
91+
});
92+
93+
// Relative URLs are resolved to absolute URLs
94+
generateMetadata({
95+
icons: "/favicon.ico",
96+
openGraph: {
97+
images: "/og.png"
98+
}
99+
})
100+
// Output:
101+
// <link rel="icon" href="https://example.com/favicon.ico" />
102+
// <meta property="og:image" content="https://example.com/og.png" />
103+
```
104+
105+
You can also pass a `URL` object:
106+
107+
```ts
108+
const generateMetadata = createMetadataGenerator({
109+
baseUrl: new URL("https://example.com")
110+
});
111+
```
112+
113+
Absolute URLs are preserved unchanged:
114+
115+
```ts
116+
generateMetadata({
117+
icons: "https://cdn.example.com/favicon.ico"
118+
})
119+
// Output: <link rel="icon" href="https://cdn.example.com/favicon.ico" />
120+
```
121+
122+
You can combine `baseUrl` with `titleTemplate`:
123+
124+
```ts
125+
const generateMetadata = createMetadataGenerator({
126+
titleTemplate: { default: "My Site", template: "%s | My Site" },
127+
baseUrl: "https://example.com"
128+
});
129+
130+
generateMetadata({
131+
title: "About",
132+
icons: "/favicon.ico"
133+
})
134+
// Output:
135+
// <title>About | My Site</title>
136+
// <link rel="icon" href="https://example.com/favicon.ico" />
137+
```
138+
82139
## Reference
83140

84141
### `generateMetadata`
@@ -95,7 +152,7 @@ An object containing `meta` and `links` properties, which can be used as the ret
95152

96153
### `createMetadataGenerator`
97154

98-
Creates a customized metadata generator with options like title templates.
155+
Creates a customized metadata generator with options like title templates and base URL resolution.
99156

100157
#### Parameters
101158

@@ -104,6 +161,16 @@ An options object with the following properties:
104161
- `titleTemplate` (optional): An object containing:
105162
- `default`: The default title used when no title is provided
106163
- `template`: A template string where `%s` is replaced with the page title
164+
- `baseUrl` (optional): A string or `URL` object used to resolve relative URLs to absolute URLs. Applies to:
165+
- `icons`
166+
- `openGraph` (images, audio, videos, url)
167+
- `twitter` (images, players, app URLs)
168+
- `alternates` (canonical, languages, media, types)
169+
- `appLinks` (URLs for ios, android, web, windows, windows_phone, windows_universal)
170+
- `manifest`
171+
- `assets`
172+
- `archives`
173+
- `bookmarks`
107174

108175
#### Return Value
109176

0 commit comments

Comments
 (0)