-
Notifications
You must be signed in to change notification settings - Fork 28.2k
/
Copy pathis-metadata-route.ts
167 lines (158 loc) · 5.35 KB
/
is-metadata-route.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import type { PageExtensions } from '../../build/page-extensions-type'
import { normalizePathSep } from '../../shared/lib/page-path/normalize-path-sep'
import { isAppRouteRoute } from '../is-app-route-route'
export const STATIC_METADATA_IMAGES = {
icon: {
filename: 'icon',
extensions: ['ico', 'jpg', 'jpeg', 'png', 'svg'],
},
apple: {
filename: 'apple-icon',
extensions: ['jpg', 'jpeg', 'png'],
},
favicon: {
filename: 'favicon',
extensions: ['ico'],
},
openGraph: {
filename: 'opengraph-image',
extensions: ['jpg', 'jpeg', 'png', 'gif'],
},
twitter: {
filename: 'twitter-image',
extensions: ['jpg', 'jpeg', 'png', 'gif'],
},
} as const
// Match routes that are metadata routes, e.g. /sitemap.xml, /favicon.<ext>, /<icon>.<ext>, etc.
// TODO-METADATA: support more metadata routes with more extensions
const defaultExtensions = ['js', 'jsx', 'ts', 'tsx']
// Match the file extension with the dynamic multi-routes extensions
// e.g. ([xml, js], null) -> can match `/sitemap.xml/route`, `sitemap.js/route`
// e.g. ([png], [ts]) -> can match `/opengrapg-image.png/route`, `/opengraph-image.ts[]/route`
export const getExtensionRegexString = (
staticExtensions: readonly string[],
dynamicExtensions: readonly string[] | null
) => {
// If there's no possible multi dynamic routes, will not match any <name>[].<ext> files
if (!dynamicExtensions) {
return `\\.(?:${staticExtensions.join('|')})`
}
return `(?:\\.(${staticExtensions.join('|')})|((\\[\\])?\\.(${dynamicExtensions.join('|')})))`
}
// When you only pass the file extension as `[]`, it will only match the static convention files
// e.g. /robots.txt, /sitemap.xml, /favicon.ico, /manifest.json
// When you pass the file extension as `['js', 'jsx', 'ts', 'tsx']`, it will also match the dynamic convention files
// e.g. /robots.js, /sitemap.tsx, /favicon.jsx, /manifest.ts
// When `withExtension` is false, it will match the static convention files without the extension, by default it's true
// e.g. /robots, /sitemap, /favicon, /manifest, use to match dynamic API routes like app/robots.ts
export function isMetadataRouteFile(
appDirRelativePath: string,
pageExtensions: PageExtensions,
withExtension: boolean
) {
const metadataRouteFilesRegex = [
new RegExp(
`^[\\\\/]robots${
withExtension
? `${getExtensionRegexString(pageExtensions.concat('txt'), null)}$`
: ''
}`
),
new RegExp(
`^[\\\\/]manifest${
withExtension
? `${getExtensionRegexString(
pageExtensions.concat('webmanifest', 'json'),
null
)}$`
: ''
}`
),
new RegExp(`^[\\\\/]favicon\\.ico$`),
new RegExp(
`[\\\\/]sitemap${
withExtension
? `${getExtensionRegexString(['xml'], pageExtensions)}$`
: ''
}`
),
new RegExp(
`[\\\\/]${STATIC_METADATA_IMAGES.icon.filename}\\d?${
withExtension
? `${getExtensionRegexString(
STATIC_METADATA_IMAGES.icon.extensions,
pageExtensions
)}$`
: ''
}`
),
new RegExp(
`[\\\\/]${STATIC_METADATA_IMAGES.apple.filename}\\d?${
withExtension
? `${getExtensionRegexString(
STATIC_METADATA_IMAGES.apple.extensions,
pageExtensions
)}$`
: ''
}`
),
new RegExp(
`[\\\\/]${STATIC_METADATA_IMAGES.openGraph.filename}\\d?${
withExtension
? `${getExtensionRegexString(
STATIC_METADATA_IMAGES.openGraph.extensions,
pageExtensions
)}$`
: ''
}`
),
new RegExp(
`[\\\\/]${STATIC_METADATA_IMAGES.twitter.filename}\\d?${
withExtension
? `${getExtensionRegexString(
STATIC_METADATA_IMAGES.twitter.extensions,
pageExtensions
)}$`
: ''
}`
),
]
const normalizedAppDirRelativePath = normalizePathSep(appDirRelativePath)
return metadataRouteFilesRegex.some((r) =>
r.test(normalizedAppDirRelativePath)
)
}
export function isStaticMetadataRoutePage(appDirRelativePath: string) {
return isMetadataRouteFile(appDirRelativePath, [], true)
}
// Check if the route is a static metadata route, with /route suffix
// e.g. /favicon.ico/route, /icon.png/route, etc.
// But skip the text routes like robots.txt since they might also be dynamic.
// Checking route path is not enough to determine if text routes is dynamic.
export function isStaticMetadataRoute(route: string) {
const pathname = route.slice(0, -'/route'.length)
return (
isAppRouteRoute(route) &&
isStaticMetadataRoutePage(pathname) &&
// These routes can either be built by static or dynamic entrypoints,
// so we assume they're dynamic
pathname !== '/robots.txt' &&
pathname !== '/manifest.webmanifest' &&
!pathname.endsWith('/sitemap.xml')
)
}
/*
* Remove the 'app' prefix or '/route' suffix, only check the route name since they're only allowed in root app directory
* e.g.
* /app/robots -> /robots
* app/robots -> /robots
* /robots -> /robots
*/
export function isMetadataRoute(route: string): boolean {
let page = route.replace(/^\/?app\//, '').replace(/\/route$/, '')
if (page[0] !== '/') page = '/' + page
return (
!page.endsWith('/page') &&
isMetadataRouteFile(page, defaultExtensions, false)
)
}