@mandujs/core 0.9.39 → 0.9.40
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.ko.md +27 -0
- package/README.md +21 -5
- package/package.json +1 -1
- package/src/config/index.ts +1 -0
- package/src/config/mandu.ts +60 -0
- package/src/contract/client-safe.test.ts +42 -0
- package/src/contract/client-safe.ts +114 -0
- package/src/contract/client.ts +12 -11
- package/src/contract/handler.ts +10 -11
- package/src/contract/index.ts +25 -16
- package/src/contract/registry.test.ts +206 -0
- package/src/contract/registry.ts +568 -0
- package/src/contract/schema.ts +48 -12
- package/src/contract/types.ts +58 -35
- package/src/contract/validator.ts +32 -17
- package/src/filling/context.ts +103 -0
- package/src/generator/templates.ts +70 -17
- package/src/guard/analyzer.ts +9 -4
- package/src/guard/check.ts +66 -30
- package/src/guard/contract-guard.ts +9 -9
- package/src/guard/file-type.test.ts +24 -0
- package/src/guard/presets/index.ts +193 -60
- package/src/guard/rules.ts +12 -6
- package/src/guard/statistics.ts +6 -0
- package/src/guard/suggestions.ts +9 -2
- package/src/guard/types.ts +11 -1
- package/src/guard/validator.ts +160 -9
- package/src/guard/watcher.ts +2 -0
- package/src/index.ts +8 -1
- package/src/runtime/index.ts +1 -0
- package/src/runtime/streaming-ssr.ts +123 -2
- package/src/seo/index.ts +214 -0
- package/src/seo/integration/ssr.ts +307 -0
- package/src/seo/render/basic.ts +427 -0
- package/src/seo/render/index.ts +143 -0
- package/src/seo/render/jsonld.ts +539 -0
- package/src/seo/render/opengraph.ts +191 -0
- package/src/seo/render/robots.ts +116 -0
- package/src/seo/render/sitemap.ts +137 -0
- package/src/seo/render/twitter.ts +126 -0
- package/src/seo/resolve/index.ts +353 -0
- package/src/seo/resolve/opengraph.ts +143 -0
- package/src/seo/resolve/robots.ts +73 -0
- package/src/seo/resolve/title.ts +94 -0
- package/src/seo/resolve/twitter.ts +73 -0
- package/src/seo/resolve/url.ts +97 -0
- package/src/seo/routes/index.ts +290 -0
- package/src/seo/types.ts +575 -0
- package/src/slot/validator.ts +39 -16
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mandu SEO - Twitter Card Resolution
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import type {
|
|
6
|
+
Twitter,
|
|
7
|
+
TwitterImage,
|
|
8
|
+
ResolvedTwitter,
|
|
9
|
+
} from '../types'
|
|
10
|
+
import { resolveUrl } from './url'
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Twitter 이미지 배열로 정규화
|
|
14
|
+
*/
|
|
15
|
+
function resolveTwitterImages(
|
|
16
|
+
images: Twitter['images'],
|
|
17
|
+
metadataBase: URL | null
|
|
18
|
+
): TwitterImage[] | null {
|
|
19
|
+
if (!images) return null
|
|
20
|
+
|
|
21
|
+
const arr = Array.isArray(images) ? images : [images]
|
|
22
|
+
const resolved: TwitterImage[] = []
|
|
23
|
+
|
|
24
|
+
for (const image of arr) {
|
|
25
|
+
if (typeof image === 'string' || image instanceof URL) {
|
|
26
|
+
const url = resolveUrl(image, metadataBase)
|
|
27
|
+
if (url) {
|
|
28
|
+
resolved.push({ url: url.href })
|
|
29
|
+
}
|
|
30
|
+
} else {
|
|
31
|
+
const url = resolveUrl(image.url, metadataBase)
|
|
32
|
+
if (url) {
|
|
33
|
+
resolved.push({
|
|
34
|
+
url: url.href,
|
|
35
|
+
alt: image.alt,
|
|
36
|
+
})
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return resolved.length > 0 ? resolved : null
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Twitter Card 메타데이터 해석
|
|
46
|
+
*/
|
|
47
|
+
export function resolveTwitter(
|
|
48
|
+
twitter: Twitter | null | undefined,
|
|
49
|
+
metadataBase: URL | null
|
|
50
|
+
): ResolvedTwitter | null {
|
|
51
|
+
if (!twitter) return null
|
|
52
|
+
|
|
53
|
+
// 이미지가 있으면 기본 card는 summary_large_image
|
|
54
|
+
const hasImages = twitter.images !== undefined
|
|
55
|
+
const defaultCard = hasImages ? 'summary_large_image' : 'summary'
|
|
56
|
+
|
|
57
|
+
return {
|
|
58
|
+
card: twitter.card || defaultCard,
|
|
59
|
+
site: twitter.site || null,
|
|
60
|
+
siteId: twitter.siteId || null,
|
|
61
|
+
creator: twitter.creator || null,
|
|
62
|
+
creatorId: twitter.creatorId || null,
|
|
63
|
+
title: twitter.title || null,
|
|
64
|
+
description: twitter.description || null,
|
|
65
|
+
images: resolveTwitterImages(twitter.images, metadataBase),
|
|
66
|
+
players: twitter.players
|
|
67
|
+
? Array.isArray(twitter.players)
|
|
68
|
+
? twitter.players
|
|
69
|
+
: [twitter.players]
|
|
70
|
+
: null,
|
|
71
|
+
app: twitter.app || null,
|
|
72
|
+
}
|
|
73
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mandu SEO - URL Resolution
|
|
3
|
+
*
|
|
4
|
+
* 상대 URL을 metadataBase 기준 절대 URL로 변환
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* metadataBase를 URL 객체로 정규화
|
|
9
|
+
*/
|
|
10
|
+
export function normalizeMetadataBase(
|
|
11
|
+
metadataBase: string | URL | null | undefined
|
|
12
|
+
): URL | null {
|
|
13
|
+
if (!metadataBase) return null
|
|
14
|
+
|
|
15
|
+
if (metadataBase instanceof URL) {
|
|
16
|
+
return metadataBase
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
try {
|
|
20
|
+
return new URL(metadataBase)
|
|
21
|
+
} catch {
|
|
22
|
+
console.warn(`[Mandu SEO] Invalid metadataBase: ${metadataBase}`)
|
|
23
|
+
return null
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* 상대 URL을 절대 URL로 변환
|
|
29
|
+
*/
|
|
30
|
+
export function resolveUrl(
|
|
31
|
+
url: string | URL | null | undefined,
|
|
32
|
+
metadataBase: URL | null
|
|
33
|
+
): URL | null {
|
|
34
|
+
if (!url) return null
|
|
35
|
+
|
|
36
|
+
// 이미 URL 객체
|
|
37
|
+
if (url instanceof URL) {
|
|
38
|
+
return url
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// 이미 절대 URL
|
|
42
|
+
if (url.startsWith('http://') || url.startsWith('https://')) {
|
|
43
|
+
try {
|
|
44
|
+
return new URL(url)
|
|
45
|
+
} catch {
|
|
46
|
+
return null
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// 상대 URL - metadataBase 필요
|
|
51
|
+
if (!metadataBase) {
|
|
52
|
+
// metadataBase 없이 상대 URL 사용 시 경고
|
|
53
|
+
if (url.startsWith('/')) {
|
|
54
|
+
console.warn(
|
|
55
|
+
`[Mandu SEO] Relative URL "${url}" requires metadataBase to be set. ` +
|
|
56
|
+
`Add metadataBase to your root layout's metadata.`
|
|
57
|
+
)
|
|
58
|
+
}
|
|
59
|
+
return null
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
try {
|
|
63
|
+
return new URL(url, metadataBase)
|
|
64
|
+
} catch {
|
|
65
|
+
return null
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* URL을 문자열로 변환 (렌더링용)
|
|
71
|
+
*/
|
|
72
|
+
export function urlToString(url: URL | string | null | undefined): string | null {
|
|
73
|
+
if (!url) return null
|
|
74
|
+
if (url instanceof URL) return url.href
|
|
75
|
+
return url
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* 배열의 URL들을 모두 절대 URL로 변환
|
|
80
|
+
*/
|
|
81
|
+
export function resolveUrls<T extends { url: string | URL }>(
|
|
82
|
+
items: T[] | null | undefined,
|
|
83
|
+
metadataBase: URL | null
|
|
84
|
+
): (T & { url: URL })[] | null {
|
|
85
|
+
if (!items || items.length === 0) return null
|
|
86
|
+
|
|
87
|
+
const resolved: (T & { url: URL })[] = []
|
|
88
|
+
|
|
89
|
+
for (const item of items) {
|
|
90
|
+
const resolvedUrl = resolveUrl(item.url, metadataBase)
|
|
91
|
+
if (resolvedUrl) {
|
|
92
|
+
resolved.push({ ...item, url: resolvedUrl })
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return resolved.length > 0 ? resolved : null
|
|
97
|
+
}
|
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mandu SEO - Metadata Routes
|
|
3
|
+
*
|
|
4
|
+
* sitemap.ts, robots.ts 파일 컨벤션을 라우트로 변환
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```typescript
|
|
8
|
+
* // app/sitemap.ts
|
|
9
|
+
* import type { MetadataRoute } from '@mandujs/core'
|
|
10
|
+
*
|
|
11
|
+
* export default function sitemap(): MetadataRoute.Sitemap {
|
|
12
|
+
* return [
|
|
13
|
+
* { url: 'https://example.com', lastModified: new Date(), priority: 1.0 },
|
|
14
|
+
* { url: 'https://example.com/about', changeFrequency: 'monthly' },
|
|
15
|
+
* ]
|
|
16
|
+
* }
|
|
17
|
+
* ```
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```typescript
|
|
21
|
+
* // app/robots.ts
|
|
22
|
+
* import type { MetadataRoute } from '@mandujs/core'
|
|
23
|
+
*
|
|
24
|
+
* export default function robots(): MetadataRoute.Robots {
|
|
25
|
+
* return {
|
|
26
|
+
* rules: { userAgent: '*', allow: '/', disallow: '/admin' },
|
|
27
|
+
* sitemap: 'https://example.com/sitemap.xml',
|
|
28
|
+
* }
|
|
29
|
+
* }
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
import type { Sitemap, RobotsFile } from '../types'
|
|
34
|
+
import { renderSitemap, renderSitemapIndex } from '../render/sitemap'
|
|
35
|
+
import { renderRobots } from '../render/robots'
|
|
36
|
+
|
|
37
|
+
// ============================================================================
|
|
38
|
+
// Types
|
|
39
|
+
// ============================================================================
|
|
40
|
+
|
|
41
|
+
export type SitemapFunction = () => Sitemap | Promise<Sitemap>
|
|
42
|
+
export type RobotsFunction = () => RobotsFile | Promise<RobotsFile>
|
|
43
|
+
|
|
44
|
+
export interface MetadataRouteModule {
|
|
45
|
+
default: SitemapFunction | RobotsFunction
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface MetadataRouteHandler {
|
|
49
|
+
path: string
|
|
50
|
+
contentType: string
|
|
51
|
+
handler: () => Promise<string>
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// ============================================================================
|
|
55
|
+
// Route Handlers
|
|
56
|
+
// ============================================================================
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* sitemap.ts 모듈에서 라우트 핸들러 생성
|
|
60
|
+
*
|
|
61
|
+
* @param sitemapFn - sitemap 함수 (default export)
|
|
62
|
+
* @returns Request handler
|
|
63
|
+
*/
|
|
64
|
+
export function createSitemapHandler(sitemapFn: SitemapFunction): () => Promise<Response> {
|
|
65
|
+
return async () => {
|
|
66
|
+
try {
|
|
67
|
+
const sitemap = await sitemapFn()
|
|
68
|
+
const xml = renderSitemap(sitemap)
|
|
69
|
+
|
|
70
|
+
return new Response(xml, {
|
|
71
|
+
headers: {
|
|
72
|
+
'Content-Type': 'application/xml; charset=utf-8',
|
|
73
|
+
'Cache-Control': 'public, max-age=3600, s-maxage=86400',
|
|
74
|
+
},
|
|
75
|
+
})
|
|
76
|
+
} catch (error) {
|
|
77
|
+
console.error('[Mandu SEO] Sitemap generation failed:', error)
|
|
78
|
+
return new Response('<!-- Sitemap generation error -->', {
|
|
79
|
+
status: 500,
|
|
80
|
+
headers: { 'Content-Type': 'application/xml; charset=utf-8' },
|
|
81
|
+
})
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* robots.ts 모듈에서 라우트 핸들러 생성
|
|
88
|
+
*
|
|
89
|
+
* @param robotsFn - robots 함수 (default export)
|
|
90
|
+
* @returns Request handler
|
|
91
|
+
*/
|
|
92
|
+
export function createRobotsHandler(robotsFn: RobotsFunction): () => Promise<Response> {
|
|
93
|
+
return async () => {
|
|
94
|
+
try {
|
|
95
|
+
const robots = await robotsFn()
|
|
96
|
+
const txt = renderRobots(robots)
|
|
97
|
+
|
|
98
|
+
return new Response(txt, {
|
|
99
|
+
headers: {
|
|
100
|
+
'Content-Type': 'text/plain; charset=utf-8',
|
|
101
|
+
'Cache-Control': 'public, max-age=3600, s-maxage=86400',
|
|
102
|
+
},
|
|
103
|
+
})
|
|
104
|
+
} catch (error) {
|
|
105
|
+
console.error('[Mandu SEO] Robots.txt generation failed:', error)
|
|
106
|
+
return new Response('# Robots.txt generation error', {
|
|
107
|
+
status: 500,
|
|
108
|
+
headers: { 'Content-Type': 'text/plain; charset=utf-8' },
|
|
109
|
+
})
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* sitemap index 핸들러 생성 (대규모 사이트용)
|
|
116
|
+
*
|
|
117
|
+
* @param sitemapUrls - 개별 sitemap URL 목록
|
|
118
|
+
* @returns Request handler
|
|
119
|
+
*/
|
|
120
|
+
export function createSitemapIndexHandler(
|
|
121
|
+
sitemapUrls: Array<{ url: string; lastModified?: string | Date }>
|
|
122
|
+
): () => Promise<Response> {
|
|
123
|
+
return async () => {
|
|
124
|
+
try {
|
|
125
|
+
const xml = renderSitemapIndex(sitemapUrls)
|
|
126
|
+
|
|
127
|
+
return new Response(xml, {
|
|
128
|
+
headers: {
|
|
129
|
+
'Content-Type': 'application/xml; charset=utf-8',
|
|
130
|
+
'Cache-Control': 'public, max-age=3600, s-maxage=86400',
|
|
131
|
+
},
|
|
132
|
+
})
|
|
133
|
+
} catch (error) {
|
|
134
|
+
console.error('[Mandu SEO] Sitemap index generation failed:', error)
|
|
135
|
+
return new Response('<!-- Sitemap index generation error -->', {
|
|
136
|
+
status: 500,
|
|
137
|
+
headers: { 'Content-Type': 'application/xml; charset=utf-8' },
|
|
138
|
+
})
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// ============================================================================
|
|
144
|
+
// Route Discovery
|
|
145
|
+
// ============================================================================
|
|
146
|
+
|
|
147
|
+
export interface MetadataRouteConfig {
|
|
148
|
+
/** app 디렉토리 경로 */
|
|
149
|
+
appDir: string
|
|
150
|
+
/** 베이스 URL (sitemap의 절대 URL 생성용) */
|
|
151
|
+
baseUrl?: string
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export interface DiscoveredRoutes {
|
|
155
|
+
sitemap?: MetadataRouteHandler
|
|
156
|
+
robots?: MetadataRouteHandler
|
|
157
|
+
dynamicSitemaps?: MetadataRouteHandler[]
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* 메타데이터 라우트 파일 패턴
|
|
162
|
+
*/
|
|
163
|
+
export const METADATA_ROUTE_PATTERNS = {
|
|
164
|
+
sitemap: /^sitemap\.(ts|tsx|js|jsx)$/,
|
|
165
|
+
dynamicSitemap: /^sitemap\[([^\]]+)\]\.(ts|tsx|js|jsx)$/,
|
|
166
|
+
robots: /^robots\.(ts|tsx|js|jsx)$/,
|
|
167
|
+
} as const
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* 파일 경로에서 라우트 타입 결정
|
|
171
|
+
*/
|
|
172
|
+
export function getMetadataRouteType(
|
|
173
|
+
filename: string
|
|
174
|
+
): 'sitemap' | 'dynamicSitemap' | 'robots' | null {
|
|
175
|
+
if (METADATA_ROUTE_PATTERNS.sitemap.test(filename)) {
|
|
176
|
+
return 'sitemap'
|
|
177
|
+
}
|
|
178
|
+
if (METADATA_ROUTE_PATTERNS.dynamicSitemap.test(filename)) {
|
|
179
|
+
return 'dynamicSitemap'
|
|
180
|
+
}
|
|
181
|
+
if (METADATA_ROUTE_PATTERNS.robots.test(filename)) {
|
|
182
|
+
return 'robots'
|
|
183
|
+
}
|
|
184
|
+
return null
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* 메타데이터 라우트 정보 생성
|
|
189
|
+
*/
|
|
190
|
+
export function createMetadataRouteInfo(
|
|
191
|
+
type: 'sitemap' | 'robots',
|
|
192
|
+
modulePath: string
|
|
193
|
+
): {
|
|
194
|
+
path: string
|
|
195
|
+
contentType: string
|
|
196
|
+
modulePath: string
|
|
197
|
+
} {
|
|
198
|
+
switch (type) {
|
|
199
|
+
case 'sitemap':
|
|
200
|
+
return {
|
|
201
|
+
path: '/sitemap.xml',
|
|
202
|
+
contentType: 'application/xml',
|
|
203
|
+
modulePath,
|
|
204
|
+
}
|
|
205
|
+
case 'robots':
|
|
206
|
+
return {
|
|
207
|
+
path: '/robots.txt',
|
|
208
|
+
contentType: 'text/plain',
|
|
209
|
+
modulePath,
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* 동적 sitemap 라우트 정보 생성
|
|
216
|
+
*
|
|
217
|
+
* @example
|
|
218
|
+
* sitemap[id].ts → /sitemap/0.xml, /sitemap/1.xml, ...
|
|
219
|
+
*/
|
|
220
|
+
export function createDynamicSitemapRouteInfo(
|
|
221
|
+
filename: string,
|
|
222
|
+
modulePath: string
|
|
223
|
+
): {
|
|
224
|
+
paramName: string
|
|
225
|
+
pathPattern: string
|
|
226
|
+
contentType: string
|
|
227
|
+
modulePath: string
|
|
228
|
+
} | null {
|
|
229
|
+
const match = filename.match(METADATA_ROUTE_PATTERNS.dynamicSitemap)
|
|
230
|
+
if (!match) return null
|
|
231
|
+
|
|
232
|
+
const paramName = match[1]
|
|
233
|
+
|
|
234
|
+
return {
|
|
235
|
+
paramName,
|
|
236
|
+
pathPattern: `/sitemap/:${paramName}.xml`,
|
|
237
|
+
contentType: 'application/xml',
|
|
238
|
+
modulePath,
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// ============================================================================
|
|
243
|
+
// Integration with Router
|
|
244
|
+
// ============================================================================
|
|
245
|
+
|
|
246
|
+
export interface MetadataRouteDefinition {
|
|
247
|
+
path: string
|
|
248
|
+
method: 'GET'
|
|
249
|
+
handler: () => Promise<Response>
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* 메타데이터 라우트를 라우터에 등록할 수 있는 형태로 변환
|
|
254
|
+
*
|
|
255
|
+
* @example
|
|
256
|
+
* ```typescript
|
|
257
|
+
* import sitemap from './app/sitemap'
|
|
258
|
+
* import robots from './app/robots'
|
|
259
|
+
*
|
|
260
|
+
* const routes = buildMetadataRoutes({ sitemap, robots })
|
|
261
|
+
* // → [
|
|
262
|
+
* // { path: '/sitemap.xml', method: 'GET', handler: ... },
|
|
263
|
+
* // { path: '/robots.txt', method: 'GET', handler: ... },
|
|
264
|
+
* // ]
|
|
265
|
+
* ```
|
|
266
|
+
*/
|
|
267
|
+
export function buildMetadataRoutes(modules: {
|
|
268
|
+
sitemap?: SitemapFunction
|
|
269
|
+
robots?: RobotsFunction
|
|
270
|
+
}): MetadataRouteDefinition[] {
|
|
271
|
+
const routes: MetadataRouteDefinition[] = []
|
|
272
|
+
|
|
273
|
+
if (modules.sitemap) {
|
|
274
|
+
routes.push({
|
|
275
|
+
path: '/sitemap.xml',
|
|
276
|
+
method: 'GET',
|
|
277
|
+
handler: createSitemapHandler(modules.sitemap),
|
|
278
|
+
})
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
if (modules.robots) {
|
|
282
|
+
routes.push({
|
|
283
|
+
path: '/robots.txt',
|
|
284
|
+
method: 'GET',
|
|
285
|
+
handler: createRobotsHandler(modules.robots),
|
|
286
|
+
})
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
return routes
|
|
290
|
+
}
|