From f25a4a5e343cfb33f58c3945dd0aca3631ac22fe Mon Sep 17 00:00:00 2001 From: Shahed Nasser Date: Mon, 5 May 2025 12:49:05 +0300 Subject: [PATCH] docs: add script to catch bad redirects (#12361) * docs: add script to catch bad redirects * add missing dependency --- www/apps/api-reference/next.config.mjs | 5 +++-- www/apps/api-reference/package.json | 1 + www/apps/book/next.config.mjs | 7 ++++++- www/apps/book/utils/redirects.mjs | 2 +- www/apps/resources/next.config.mjs | 5 +++-- www/packages/build-scripts/package.json | 1 + .../build-scripts/src/catch-bad-redirects.ts | 17 +++++++++++++++++ www/packages/build-scripts/src/index.ts | 1 + www/yarn.lock | 2 ++ 9 files changed, 35 insertions(+), 6 deletions(-) create mode 100644 www/packages/build-scripts/src/catch-bad-redirects.ts diff --git a/www/apps/api-reference/next.config.mjs b/www/apps/api-reference/next.config.mjs index b58c767e1f..b7a17b3f1a 100644 --- a/www/apps/api-reference/next.config.mjs +++ b/www/apps/api-reference/next.config.mjs @@ -7,6 +7,7 @@ import { crossProjectLinksPlugin, } from "remark-rehype-plugins" import path from "path" +import { catchBadRedirects } from "build-scripts" /** @type {import('next').NextConfig} */ const nextConfig = { @@ -23,13 +24,13 @@ const nextConfig = { optimizePackageImports: ["docs-utils"], }, async redirects() { - return [ + return catchBadRedirects([ { source: "/api/download/:path", destination: "/download/:path", permanent: true, }, - ] + ]) }, } diff --git a/www/apps/api-reference/package.json b/www/apps/api-reference/package.json index 883b41ebdb..a6a3d493b2 100644 --- a/www/apps/api-reference/package.json +++ b/www/apps/api-reference/package.json @@ -59,6 +59,7 @@ "@types/pluralize": "^0.0.33", "@types/react": "npm:types-react@rc", "@types/react-dom": "npm:types-react@rc", + "build-scripts": "*", "eslint": "^9.13.0", "eslint-plugin-prettier": "^5.2.1", "eslint-plugin-react-hooks": "^5.0.0", diff --git a/www/apps/book/next.config.mjs b/www/apps/book/next.config.mjs index 18df4cfb7a..c7b5f437ab 100644 --- a/www/apps/book/next.config.mjs +++ b/www/apps/book/next.config.mjs @@ -11,6 +11,7 @@ import { import path from "path" import redirects from "./utils/redirects.mjs" import { generatedSidebars } from "./generated/sidebar.mjs" +import { catchBadRedirects } from "build-scripts" const withMDX = mdx({ extension: /\.mdx?$/, @@ -168,7 +169,11 @@ const nextConfig = { ], } }, - redirects, + redirects: async () => { + const result = await redirects() + + return catchBadRedirects(result) + }, outputFileTracingIncludes: { "/md\\-content/\\[\\.\\.\\.slug\\]": ["./app/**/*.mdx"], }, diff --git a/www/apps/book/utils/redirects.mjs b/www/apps/book/utils/redirects.mjs index 5aae159c71..bc7a356641 100644 --- a/www/apps/book/utils/redirects.mjs +++ b/www/apps/book/utils/redirects.mjs @@ -1,5 +1,5 @@ /** - * @returns {Promise} + * @type {import("next").NextConfig["redirects"]} */ const redirects = async () => { return [ diff --git a/www/apps/resources/next.config.mjs b/www/apps/resources/next.config.mjs index 724f700862..509ac4e4c9 100644 --- a/www/apps/resources/next.config.mjs +++ b/www/apps/resources/next.config.mjs @@ -10,6 +10,7 @@ import bundleAnalyzer from "@next/bundle-analyzer" import mdx from "@next/mdx" import mdxPluginOptions from "./mdx-options.mjs" import path from "node:path" +import { catchBadRedirects } from "build-scripts" const withMDX = mdx({ extension: /\.mdx?$/, @@ -62,7 +63,7 @@ const nextConfig = { basePath: process.env.NEXT_PUBLIC_BASE_PATH || "/resources", async redirects() { - return [ + return catchBadRedirects([ { source: "/commerce-modules/order/relations-to-other-modules", destination: "/commerce-modules/order/links-to-other-modules", @@ -228,7 +229,7 @@ const nextConfig = { destination: "/references/user/events", permanent: true, }, - ] + ]) }, outputFileTracingExcludes: { "*": ["node_modules/@medusajs/icons"], diff --git a/www/packages/build-scripts/package.json b/www/packages/build-scripts/package.json index f3309fd6ea..da5bab0664 100644 --- a/www/packages/build-scripts/package.json +++ b/www/packages/build-scripts/package.json @@ -38,6 +38,7 @@ }, "devDependencies": { "@types/node": "^20.11.20", + "next": "15.3.1", "rimraf": "^5.0.5", "tsconfig": "*", "types": "*", diff --git a/www/packages/build-scripts/src/catch-bad-redirects.ts b/www/packages/build-scripts/src/catch-bad-redirects.ts new file mode 100644 index 0000000000..db7fd9c404 --- /dev/null +++ b/www/packages/build-scripts/src/catch-bad-redirects.ts @@ -0,0 +1,17 @@ +import type { NextConfig } from "next" + +type CatchBadRedirectsOptions = Awaited< + ReturnType> +> + +export const catchBadRedirects = (redirects: CatchBadRedirectsOptions) => { + for (const redirect of redirects) { + if (redirect.source === redirect.destination) { + throw new Error( + `Redirect source and destination are the same: ${JSON.stringify(redirect, null, 2)}` + ) + } + } + + return redirects +} diff --git a/www/packages/build-scripts/src/index.ts b/www/packages/build-scripts/src/index.ts index 170b6b13ae..0fdada8299 100644 --- a/www/packages/build-scripts/src/index.ts +++ b/www/packages/build-scripts/src/index.ts @@ -3,6 +3,7 @@ export * from "./generate-llms-full.js" export * from "./generate-sidebar.js" export * from "./generate-split-sidebars.js" export * from "./retrieve-mdx-pages.js" +export * from "./catch-bad-redirects.js" export * from "./utils/get-core-flows-ref-sidebar-children.js" export * from "./utils/get-sidebar-item-link.js" diff --git a/www/yarn.lock b/www/yarn.lock index 5c87225b16..62f2e509bc 100644 --- a/www/yarn.lock +++ b/www/yarn.lock @@ -6252,6 +6252,7 @@ __metadata: "@types/react-dom": "npm:types-react@rc" algoliasearch: 4 autoprefixer: 10.4.14 + build-scripts: "*" clsx: ^2.0.0 docs-ui: "*" docs-utils: "*" @@ -6703,6 +6704,7 @@ __metadata: "@types/node": ^20.11.20 docs-utils: "*" fdir: ^6.4.3 + next: 15.3.1 pluralize: ^8.0.0 rimraf: ^5.0.5 slugify: ^1.6.6