1 import mdx from "@next/mdx"
2 import rehypeMdxCodeProps from "rehype-mdx-code-props"
3 import rehypeSlug from "rehype-slug"
4 import {
5 brokenLinkCheckerPlugin,
6 localLinksRehypePlugin,
7 cloudinaryImgRehypePlugin,
8 pageNumberRehypePlugin,
9 crossProjectLinksPlugin,
10 recmaInjectMdxDataPlugin,
11 remarkAttachFrontmatterDataPlugin,
12 validateHighlightsPlugin,
13 } from "remark-rehype-plugins"
14 import path from "path"
15 import redirects from "./utils/redirects.mjs"
16 import { generatedSidebars } from "./generated/sidebar.mjs"
17 import { catchBadRedirects } from "build-scripts"
18 import remarkFrontmatter from "remark-frontmatter"
19 import withExtractedTableOfContents from "@stefanprobst/rehype-extract-toc"
20
21 const withMDX = mdx({
22 extension: /\.mdx?$/,
23 options: {
24 rehypePlugins: [
25 [
26 brokenLinkCheckerPlugin,
27 {
28 crossProjects: {
29 resources: {
30 projectPath: path.resolve("..", "resources"),
31 hasGeneratedSlugs: true,
32 },
33 ui: {
34 projectPath: path.resolve("..", "ui"),
35 },
36 "user-guide": {
37 projectPath: path.resolve("..", "user-guide"),
38 },
39 api: {
40 projectPath: path.resolve("..", "api-reference"),
41 skipSlugValidation: true,
42 },
43 cloud: {
44 projectPath: path.resolve("..", "cloud"),
45 },
46 },
47 },
48 ],
49 [
50 crossProjectLinksPlugin,
51 {
52 baseUrl: process.env.NEXT_PUBLIC_BASE_URL,
53 projectUrls: {
54 resources: {
55 url: process.env.NEXT_PUBLIC_RESOURCES_URL,
56 },
57 "user-guide": {
58 url: process.env.NEXT_PUBLIC_USER_GUIDE_URL,
59 },
60 ui: {
61 url: process.env.NEXT_PUBLIC_UI_URL,
62 },
63 api: {
64 url: process.env.NEXT_PUBLIC_API_URL,
65 },
66 cloud: {
67 url: process.env.NEXT_PUBLIC_CLOUD_URL,
68 },
69 },
70 useBaseUrl:
71 process.env.NODE_ENV === "production" ||
72 process.env.VERCEL_ENV === "production" ||
73 !!process.env.CLOUDFLARE_ENV,
74 },
75 ],
76 [localLinksRehypePlugin],
77 [
78 rehypeMdxCodeProps,
79 {
80 tagName: "code",
81 },
82 ],
83 [validateHighlightsPlugin, { verbose: false }],
84 [rehypeSlug],
85 [
86 cloudinaryImgRehypePlugin,
87 {
88 cloudinaryConfig: {
89 cloudName: process.env.CLOUDINARY_CLOUD_NAME || "",
90 flags: ["fl_lossy", "f_auto"],
91 resize: {
92 action: "pad",
93 aspectRatio: "16:9",
94 },
95 roundCorners: 16,
96 },
97 },
98 ],
99 [
100 pageNumberRehypePlugin,
101 {
102 sidebar: generatedSidebars[0].items,
103 },
104 ],
105 [withExtractedTableOfContents],
106 ],
107 remarkPlugins: [[remarkFrontmatter], [remarkAttachFrontmatterDataPlugin]],
108 recmaPlugins: [[recmaInjectMdxDataPlugin]],
109 jsx: true,
110 },
111 })
112
113 /** @type {import('next').NextConfig} */
114 const nextConfig = {
115 // Configure `pageExtensions` to include MDX files
116 pageExtensions: ["js", "jsx", "mdx", "ts", "tsx"],
117
118 transpilePackages: ["docs-ui"],
119 async rewrites() {
120 return {
121 beforeFiles: [
122 {
123 source: "/:file(llms\\.txt|llms-full\\.txt)",
124 destination: "/llms-content/:file",
125 },
126 {
127 source: "/index.html.md",
128 destination: "/md-content",
129 },
130 {
131 source: "/index.md",
132 destination: "/md-content",
133 },
134 {
135 source:
136 "/:first((?!resources|api|ui|user-guide|cloud|md-content)[^/]+)/:rest*/index.html.md",
137 destination: "/md-content/:first/:rest*",
138 },
139 {
140 source:
141 "/:first((?!resources|api|ui|user-guide|cloud|md-content)[^/]+)/:rest*/index.md",
142 destination: "/md-content/:first/:rest*",
143 },
144 {
145 source:
146 "/:first((?!resources|api|ui|user-guide|cloud|md-content)[^/]+)/:rest*.md",
147 destination: "/md-content/:first/:rest*",
148 },
149 {
150 source:
151 "/:first((?!resources|api|ui|user-guide|cloud|md-content)[^/]+)/:rest*/",
152 has: [
153 {
154 type: "header",
155 key: "Accept",
156 value: ".*(text/markdown|text/plain).*",
157 },
158 ],
159 destination: "/md-content/:first/:rest*",
160 },
161 {
162 source: "/",
163 has: [
164 {
165 type: "header",
166 key: "Accept",
167 value: ".*(text/markdown|text/plain).*",
168 },
169 ],
170 destination: "/md-content",
171 },
172 {
173 source:
174 "/:first((?!resources|api|ui|user-guide|cloud|md-content)[^/]+)/:rest*",
175 has: [
176 {
177 type: "header",
178 key: "Accept",
179 value: ".*(text/markdown|text/plain).*",
180 },
181 ],
182 destination: "/md-content/:first/:rest*",
183 },
184 ],
185 fallback: [
186 {
187 source: "/resources",
188 destination: `${
189 process.env.NEXT_PUBLIC_RESOURCES_URL || "https://localhost:3001"
190 }/resources`,
191 basePath: false,
192 },
193 {
194 source: "/resources/:path*",
195 destination: `${
196 process.env.NEXT_PUBLIC_RESOURCES_URL || "https://localhost:3001"
197 }/resources/:path*`,
198 basePath: false,
199 },
200 {
201 source: "/api",
202 destination: `${
203 process.env.NEXT_PUBLIC_API_URL || "https://localhost:3001"
204 }/api`,
205 basePath: false,
206 },
207 {
208 source: "/api/:path*",
209 destination: `${
210 process.env.NEXT_PUBLIC_API_URL || "https://localhost:3001"
211 }/api/:path*`,
212 basePath: false,
213 },
214 {
215 source: "/ui",
216 destination: `${
217 process.env.NEXT_PUBLIC_UI_URL || "https://localhost:3001"
218 }/ui`,
219 basePath: false,
220 },
221 {
222 source: "/ui/:path*",
223 destination: `${
224 process.env.NEXT_PUBLIC_UI_URL || "https://localhost:3001"
225 }/ui/:path*`,
226 basePath: false,
227 },
228 {
229 source: "/v1/api",
230 destination: `${
231 process.env.NEXT_PUBLIC_API_V1_URL || "https://localhost:3001"
232 }/v1/api`,
233 basePath: false,
234 },
235 {
236 source: "/v1/api/:path*",
237 destination: `${
238 process.env.NEXT_PUBLIC_API_V1_URL || "https://localhost:3001"
239 }/v1/api/:path*`,
240 basePath: false,
241 },
242 {
243 source: "/v1",
244 destination: `${
245 process.env.NEXT_PUBLIC_DOCS_V1_URL || "https://localhost:3001"
246 }/v1`,
247 basePath: false,
248 },
249 {
250 source: "/v1/:path*",
251 destination: `${
252 process.env.NEXT_PUBLIC_DOCS_V1_URL || "https://localhost:3001"
253 }/v1/:path*`,
254 basePath: false,
255 },
256 {
257 source: "/user-guide",
258 destination: `${process.env.NEXT_PUBLIC_USER_GUIDE_URL || "https://localhost:3001"}/user-guide`,
259 basePath: false,
260 },
261 {
262 source: "/user-guide/:path*",
263 destination: `${process.env.NEXT_PUBLIC_USER_GUIDE_URL || "https://localhost:3001"}/user-guide/:path*`,
264 basePath: false,
265 },
266 {
267 source: "/cloud",
268 destination: `${process.env.NEXT_PUBLIC_CLOUD_URL || "https://localhost:3001"}/cloud`,
269 basePath: false,
270 },
271 {
272 source: "/cloud/:path*",
273 destination: `${process.env.NEXT_PUBLIC_CLOUD_URL || "https://localhost:3001"}/cloud/:path*`,
274 basePath: false,
275 },
276 // MCP OAuth discovery — RFC 9728 path-component variants (strip /mcp suffix)
277 {
278 source: "/.well-known/oauth-protected-resource/mcp",
279 destination: `${
280 process.env.NEXT_MCP_BASE_URL || "http://localhost:3001"
281 }/.well-known/oauth-protected-resource`,
282 basePath: false,
283 },
284 {
285 source: "/.well-known/oauth-authorization-server/mcp",
286 destination: `${
287 process.env.NEXT_MCP_BASE_URL || "http://localhost:3001"
288 }/.well-known/oauth-authorization-server`,
289 basePath: false,
290 },
291 {
292 source: "/.well-known/openid-configuration/mcp",
293 destination: `${
294 process.env.NEXT_MCP_BASE_URL || "http://localhost:3001"
295 }/.well-known/openid-configuration`,
296 basePath: false,
297 },
298 // MCP OAuth discovery — standard paths
299 {
300 source: "/.well-known/oauth-protected-resource",
301 destination: `${
302 process.env.NEXT_MCP_BASE_URL || "http://localhost:3001"
303 }/.well-known/oauth-protected-resource`,
304 basePath: false,
305 },
306 {
307 source: "/.well-known/oauth-authorization-server",
308 destination: `${
309 process.env.NEXT_MCP_BASE_URL || "http://localhost:3001"
310 }/.well-known/oauth-authorization-server`,
311 basePath: false,
312 },
313 {
314 source: "/.well-known/openid-configuration",
315 destination: `${
316 process.env.NEXT_MCP_BASE_URL || "http://localhost:3001"
317 }/.well-known/openid-configuration`,
318 basePath: false,
319 },
320 // MCP Dynamic Client Registration
321 {
322 source: "/register",
323 destination: `${
324 process.env.NEXT_MCP_BASE_URL || "http://localhost:3001"
325 }/oauth/register`,
326 basePath: false,
327 },
328 {
329 source: "/oauth/register",
330 destination: `${
331 process.env.NEXT_MCP_BASE_URL || "http://localhost:3001"
332 }/oauth/register`,
333 basePath: false,
334 },
335 {
336 source: "/mcp",
337 destination:
338 process.env.NEXT_MCP_SERVER_URL || "http://localhost:3001/mcp",
339 basePath: false,
340 },
341 ],
342 }
343 },
344 async headers() {
345 return [
346 {
347 source: "/",
348 headers: [
349 {
350 key: "Cache-Control",
351 value: "public, max-age=3600, must-revalidate",
352 },
353 ],
354 },
355 ]
356 },
357 redirects: async () => {
358 const result = await redirects()
359
360 return catchBadRedirects(result)
361 },
362 outputFileTracingRoot: new URL("../../", import.meta.url).pathname,
363 outputFileTracingIncludes: {
364 "/md\\-content/\\[\\.\\.\\.slug\\]": ["./app/**/*.mdx"],
365 },
366 outputFileTracingExcludes: {
367 "*": [
368 "node_modules/@medusajs/icons",
369 "../**/.open-next/**",
370 "../!(book)/.next/**",
371 ],
372 },
373 experimental: {
374 optimizePackageImports: ["@medusajs/icons", "@medusajs/ui"],
375 },
376 }
377
378 export default withMDX(nextConfig)