Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

perf: run every index redirect before its parents' loaders

Sidwebworks6a15250114a1
diff --git a/.claude/rules/coding-conventions.md b/.claude/rules/coding-conventions.md
index 1b05286516..dbe02d7364 100644
--- a/.claude/rules/coding-conventions.md
+++ b/.claude/rules/coding-conventions.md
@@ -112,6 +112,11 @@ MES is lighter: services live under `apps/mes/app/services/`, components under
component that stays mounted across records passes the record id as its third
argument (`useResolved(promise, null, itemId)`), or it shows the previous record's
value until the new one arrives.
+- An index route that only redirects (`/x/issue/:id` → `…/details`, a module root →
+ its first page) also exports `middleware = [redirectBeforeLoaders(loader)]` from
+ `@carbon/utils`. The loaders of a matched branch run in parallel, so without it every
+ parent loader runs for a request that is about to be redirected. Enforced by the
+ `index-redirect-before-loaders` check (`@carbon/checks`).
- On success an action throws a redirect (`throw redirect(...)`), not `return`.
Cached entities add a `clientAction`/`clientLoader` for cache control.
diff --git a/apps/erp/app/routes/onboarding+/_index.tsx b/apps/erp/app/routes/onboarding+/_index.tsx
index c8e6e7dd9c..94a446271e 100644
--- a/apps/erp/app/routes/onboarding+/_index.tsx
+++ b/apps/erp/app/routes/onboarding+/_index.tsx
@@ -2,9 +2,12 @@
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.
+import { redirectBeforeLoaders } from "@carbon/utils";
import { redirect } from "react-router";
import { onboardingSequence } from "~/utils/path";
export async function loader() {
throw redirect(onboardingSequence[0]);
}
+
+export const middleware = [redirectBeforeLoaders(loader)];
diff --git a/apps/erp/app/routes/x+/account+/_index.tsx b/apps/erp/app/routes/x+/account+/_index.tsx
index 1fabc51b76..cf94a15130 100644
--- a/apps/erp/app/routes/x+/account+/_index.tsx
+++ b/apps/erp/app/routes/x+/account+/_index.tsx
@@ -2,9 +2,12 @@
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.
+import { redirectBeforeLoaders } from "@carbon/utils";
import { redirect } from "react-router";
import { path } from "~/utils/path";
export async function loader() {
throw redirect(path.to.profile);
}
+
+export const middleware = [redirectBeforeLoaders(loader)];
diff --git a/apps/erp/app/routes/x+/accounting+/_index.tsx b/apps/erp/app/routes/x+/accounting+/_index.tsx
index 18f9dace05..cf2b643d6e 100644
--- a/apps/erp/app/routes/x+/accounting+/_index.tsx
+++ b/apps/erp/app/routes/x+/accounting+/_index.tsx
@@ -3,6 +3,7 @@
// including ports, remain AGPLv3; serving them over a network requires releasing their source.
import { requirePermissions } from "@carbon/auth/auth.server";
+import { redirectBeforeLoaders } from "@carbon/utils";
import type { LoaderFunctionArgs } from "react-router";
import { redirect } from "react-router";
import { getCompanySettings } from "~/modules/settings";
@@ -20,3 +21,5 @@ export async function loader({ request }: LoaderFunctionArgs) {
throw redirect(accountingEnabled ? path.to.reports : path.to.chartOfAccounts);
}
+
+export const middleware = [redirectBeforeLoaders(loader)];
diff --git a/apps/erp/app/routes/x+/consumable+/$itemId._index.tsx b/apps/erp/app/routes/x+/consumable+/$itemId._index.tsx
index c13f561246..1c8b6b8b16 100644
--- a/apps/erp/app/routes/x+/consumable+/$itemId._index.tsx
+++ b/apps/erp/app/routes/x+/consumable+/$itemId._index.tsx
@@ -2,10 +2,10 @@
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.
+import { redirectBeforeLoaders } from "@carbon/utils";
import type { LoaderFunctionArgs } from "react-router";
import { redirect } from "react-router";
import { path } from "~/utils/path";
-import { redirectBeforeLoaders } from "~/utils/redirect.server";
export async function loader({ params }: LoaderFunctionArgs) {
const { itemId } = params;
diff --git a/apps/erp/app/routes/x+/customer+/$customerId._index.tsx b/apps/erp/app/routes/x+/customer+/$customerId._index.tsx
index 6cefb7db34..9ee4e389fd 100644
--- a/apps/erp/app/routes/x+/customer+/$customerId._index.tsx
+++ b/apps/erp/app/routes/x+/customer+/$customerId._index.tsx
@@ -2,10 +2,10 @@
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.
+import { redirectBeforeLoaders } from "@carbon/utils";
import type { LoaderFunctionArgs } from "react-router";
import { redirect } from "react-router";
import { path } from "~/utils/path";
-import { redirectBeforeLoaders } from "~/utils/redirect.server";
export async function loader({ params }: LoaderFunctionArgs) {
const { customerId } = params;
diff --git a/apps/erp/app/routes/x+/documents+/_index.tsx b/apps/erp/app/routes/x+/documents+/_index.tsx
index ccf80d52d6..0640d3d5a9 100644
--- a/apps/erp/app/routes/x+/documents+/_index.tsx
+++ b/apps/erp/app/routes/x+/documents+/_index.tsx
@@ -2,9 +2,12 @@
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.
+import { redirectBeforeLoaders } from "@carbon/utils";
import { redirect } from "react-router";
import { path } from "~/utils/path";
export async function loader() {
throw redirect(path.to.documents);
}
+
+export const middleware = [redirectBeforeLoaders(loader)];
diff --git a/apps/erp/app/routes/x+/inventory+/_index.tsx b/apps/erp/app/routes/x+/inventory+/_index.tsx
index 3753d7b63e..54218b0092 100644
--- a/apps/erp/app/routes/x+/inventory+/_index.tsx
+++ b/apps/erp/app/routes/x+/inventory+/_index.tsx
@@ -2,9 +2,12 @@
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.
+import { redirectBeforeLoaders } from "@carbon/utils";
import { redirect } from "react-router";
import { path } from "~/utils/path";
export async function loader() {
throw redirect(path.to.inventory);
}
+
+export const middleware = [redirectBeforeLoaders(loader)];
diff --git a/apps/erp/app/routes/x+/issue+/$id._index.tsx b/apps/erp/app/routes/x+/issue+/$id._index.tsx
index 13d7806bd8..3990b4a5ad 100644
--- a/apps/erp/app/routes/x+/issue+/$id._index.tsx
+++ b/apps/erp/app/routes/x+/issue+/$id._index.tsx
@@ -2,10 +2,10 @@
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.
+import { redirectBeforeLoaders } from "@carbon/utils";
import type { LoaderFunctionArgs } from "react-router";
import { redirect } from "react-router";
import { path } from "~/utils/path";
-import { redirectBeforeLoaders } from "~/utils/redirect.server";
export async function loader({ params }: LoaderFunctionArgs) {
const { id } = params;
diff --git a/apps/erp/app/routes/x+/items+/_index.tsx b/apps/erp/app/routes/x+/items+/_index.tsx
index ebf702f4a4..71927bd099 100644
--- a/apps/erp/app/routes/x+/items+/_index.tsx
+++ b/apps/erp/app/routes/x+/items+/_index.tsx
@@ -2,9 +2,12 @@
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.
+import { redirectBeforeLoaders } from "@carbon/utils";
import { redirect } from "react-router";
import { path } from "~/utils/path";
export async function loader() {
throw redirect(path.to.parts);
}
+
+export const middleware = [redirectBeforeLoaders(loader)];
diff --git a/apps/erp/app/routes/x+/items+/change-notice+/$id._index.tsx b/apps/erp/app/routes/x+/items+/change-notice+/$id._index.tsx
index 605d9cd8f8..848dd670a6 100644
--- a/apps/erp/app/routes/x+/items+/change-notice+/$id._index.tsx
+++ b/apps/erp/app/routes/x+/items+/change-notice+/$id._index.tsx
@@ -2,10 +2,10 @@
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.
+import { redirectBeforeLoaders } from "@carbon/utils";
import type { LoaderFunctionArgs } from "react-router";
import { redirect } from "react-router";
import { path } from "~/utils/path";
-import { redirectBeforeLoaders } from "~/utils/redirect.server";
export async function loader({ params }: LoaderFunctionArgs) {
const { id } = params;
diff --git a/apps/erp/app/routes/x+/items+/change-orders+/_index.tsx b/apps/erp/app/routes/x+/items+/change-orders+/_index.tsx
index 325e86bfc0..38a2944a0f 100644
--- a/apps/erp/app/routes/x+/items+/change-orders+/_index.tsx
+++ b/apps/erp/app/routes/x+/items+/change-orders+/_index.tsx
@@ -2,6 +2,7 @@
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.
+import { redirectBeforeLoaders } from "@carbon/utils";
import { redirect } from "react-router";
import { path } from "~/utils/path";
@@ -9,3 +10,5 @@ import { path } from "~/utils/path";
export async function loader() {
throw redirect(path.to.changeNotices, 301);
}
+
+export const middleware = [redirectBeforeLoaders(loader)];
diff --git a/apps/erp/app/routes/x+/job+/$jobId._index.tsx b/apps/erp/app/routes/x+/job+/$jobId._index.tsx
index 97be47ef62..43130d2542 100644
--- a/apps/erp/app/routes/x+/job+/$jobId._index.tsx
+++ b/apps/erp/app/routes/x+/job+/$jobId._index.tsx
@@ -2,10 +2,10 @@
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.
+import { redirectBeforeLoaders } from "@carbon/utils";
import type { LoaderFunctionArgs } from "react-router";
import { redirect } from "react-router";
import { path } from "~/utils/path";
-import { redirectBeforeLoaders } from "~/utils/redirect.server";
export async function loader({ params }: LoaderFunctionArgs) {
const { jobId } = params;
diff --git a/apps/erp/app/routes/x+/journal-entry+/$journalEntryId._index.tsx b/apps/erp/app/routes/x+/journal-entry+/$journalEntryId._index.tsx
index ad736949c9..e1b323df49 100644
--- a/apps/erp/app/routes/x+/journal-entry+/$journalEntryId._index.tsx
+++ b/apps/erp/app/routes/x+/journal-entry+/$journalEntryId._index.tsx
@@ -2,10 +2,10 @@
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.
+import { redirectBeforeLoaders } from "@carbon/utils";
import type { LoaderFunctionArgs } from "react-router";
import { redirect } from "react-router";
import { path } from "~/utils/path";
-import { redirectBeforeLoaders } from "~/utils/redirect.server";
export async function loader({ params }: LoaderFunctionArgs) {
const { journalEntryId } = params;
diff --git a/apps/erp/app/routes/x+/material+/$itemId._index.tsx b/apps/erp/app/routes/x+/material+/$itemId._index.tsx
index 58ef397fac..9621d97e06 100644
--- a/apps/erp/app/routes/x+/material+/$itemId._index.tsx
+++ b/apps/erp/app/routes/x+/material+/$itemId._index.tsx
@@ -2,10 +2,10 @@
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.
+import { redirectBeforeLoaders } from "@carbon/utils";
import type { LoaderFunctionArgs } from "react-router";
import { redirect } from "react-router";
import { path } from "~/utils/path";
-import { redirectBeforeLoaders } from "~/utils/redirect.server";
export async function loader({ params }: LoaderFunctionArgs) {
const { itemId } = params;
diff --git a/apps/erp/app/routes/x+/part+/$itemId._index.tsx b/apps/erp/app/routes/x+/part+/$itemId._index.tsx
index 03abef67d9..afcc24c7bc 100644
--- a/apps/erp/app/routes/x+/part+/$itemId._index.tsx
+++ b/apps/erp/app/routes/x+/part+/$itemId._index.tsx
@@ -2,10 +2,10 @@
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.
+import { redirectBeforeLoaders } from "@carbon/utils";
import type { LoaderFunctionArgs } from "react-router";
import { redirect } from "react-router";
import { path } from "~/utils/path";
-import { redirectBeforeLoaders } from "~/utils/redirect.server";
export async function loader({ params }: LoaderFunctionArgs) {
const { itemId } = params;
diff --git a/apps/erp/app/routes/x+/people+/_index.tsx b/apps/erp/app/routes/x+/people+/_index.tsx
index 287c23a1d7..3f15de0d3a 100644
--- a/apps/erp/app/routes/x+/people+/_index.tsx
+++ b/apps/erp/app/routes/x+/people+/_index.tsx
@@ -2,9 +2,12 @@
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.
+import { redirectBeforeLoaders } from "@carbon/utils";
import { redirect } from "react-router";
import { path } from "~/utils/path";
export async function loader() {
throw redirect(path.to.people);
}
+
+export const middleware = [redirectBeforeLoaders(loader)];
diff --git a/apps/erp/app/routes/x+/person+/$personId._index.tsx b/apps/erp/app/routes/x+/person+/$personId._index.tsx
index 85a793ae6c..0c6dccb2ba 100644
--- a/apps/erp/app/routes/x+/person+/$personId._index.tsx
+++ b/apps/erp/app/routes/x+/person+/$personId._index.tsx
@@ -2,10 +2,10 @@
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.
+import { redirectBeforeLoaders } from "@carbon/utils";
import type { LoaderFunctionArgs } from "react-router";
import { redirect } from "react-router";
import { path } from "~/utils/path";
-import { redirectBeforeLoaders } from "~/utils/redirect.server";
export async function loader({ params }: LoaderFunctionArgs) {
const { personId } = params;
diff --git a/apps/erp/app/routes/x+/picking-list+/$pickingListId._index.tsx b/apps/erp/app/routes/x+/picking-list+/$pickingListId._index.tsx
index b3a9d936ad..92ba4a0cb6 100644
--- a/apps/erp/app/routes/x+/picking-list+/$pickingListId._index.tsx
+++ b/apps/erp/app/routes/x+/picking-list+/$pickingListId._index.tsx
@@ -2,10 +2,10 @@
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.
+import { redirectBeforeLoaders } from "@carbon/utils";
import type { LoaderFunctionArgs } from "react-router";
import { redirect } from "react-router";
import { path } from "~/utils/path";
-import { redirectBeforeLoaders } from "~/utils/redirect.server";
export async function loader({ params }: LoaderFunctionArgs) {
const { pickingListId } = params;
diff --git a/apps/erp/app/routes/x+/purchase-invoice+/$invoiceId._index.tsx b/apps/erp/app/routes/x+/purchase-invoice+/$invoiceId._index.tsx
index ae17b58c10..6bd5ff60a4 100644
--- a/apps/erp/app/routes/x+/purchase-invoice+/$invoiceId._index.tsx
+++ b/apps/erp/app/routes/x+/purchase-invoice+/$invoiceId._index.tsx
@@ -3,10 +3,10 @@
// including ports, remain AGPLv3; serving them over a network requires releasing their source.
import { notFound } from "@carbon/auth";
+import { redirectBeforeLoaders } from "@carbon/utils";
import type { LoaderFunctionArgs } from "react-router";
import { redirect } from "react-router";
import { path } from "~/utils/path";
-import { redirectBeforeLoaders } from "~/utils/redirect.server";
export async function loader({ params }: LoaderFunctionArgs) {
const { invoiceId } = params;
diff --git a/apps/erp/app/routes/x+/purchase-order+/$orderId._index.tsx b/apps/erp/app/routes/x+/purchase-order+/$orderId._index.tsx
index 74365fb16d..a47e720207 100644
--- a/apps/erp/app/routes/x+/purchase-order+/$orderId._index.tsx
+++ b/apps/erp/app/routes/x+/purchase-order+/$orderId._index.tsx
@@ -2,10 +2,10 @@
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.
+import { redirectBeforeLoaders } from "@carbon/utils";
import type { LoaderFunctionArgs } from "react-router";
import { redirect } from "react-router";
import { path } from "~/utils/path";
-import { redirectBeforeLoaders } from "~/utils/redirect.server";
export async function loader({ params }: LoaderFunctionArgs) {
const { orderId } = params;
diff --git a/apps/erp/app/routes/x+/purchase-return-order+/$id._index.tsx b/apps/erp/app/routes/x+/purchase-return-order+/$id._index.tsx
index 5a78177637..1fdd9a38c0 100644
--- a/apps/erp/app/routes/x+/purchase-return-order+/$id._index.tsx
+++ b/apps/erp/app/routes/x+/purchase-return-order+/$id._index.tsx
@@ -2,10 +2,10 @@
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.
+import { redirectBeforeLoaders } from "@carbon/utils";
import type { LoaderFunctionArgs } from "react-router";
import { redirect } from "react-router";
import { path } from "~/utils/path";
-import { redirectBeforeLoaders } from "~/utils/redirect.server";
export async function loader({ params }: LoaderFunctionArgs) {
const { id } = params;
diff --git a/apps/erp/app/routes/x+/quote+/$quoteId._index.tsx b/apps/erp/app/routes/x+/quote+/$quoteId._index.tsx
index ab007d53b5..48de5f89d2 100644
--- a/apps/erp/app/routes/x+/quote+/$quoteId._index.tsx
+++ b/apps/erp/app/routes/x+/quote+/$quoteId._index.tsx
@@ -2,10 +2,10 @@
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.
+import { redirectBeforeLoaders } from "@carbon/utils";
import type { LoaderFunctionArgs } from "react-router";
import { redirect } from "react-router";
import { path } from "~/utils/path";
-import { redirectBeforeLoaders } from "~/utils/redirect.server";
export async function loader({ params }: LoaderFunctionArgs) {
const { quoteId } = params;
diff --git a/apps/erp/app/routes/x+/receipt+/$receiptId._index.tsx b/apps/erp/app/routes/x+/receipt+/$receiptId._index.tsx
index 3af735c594..6e22b9734c 100644
--- a/apps/erp/app/routes/x+/receipt+/$receiptId._index.tsx
+++ b/apps/erp/app/routes/x+/receipt+/$receiptId._index.tsx
@@ -2,10 +2,10 @@
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.
+import { redirectBeforeLoaders } from "@carbon/utils";
import type { LoaderFunctionArgs } from "react-router";
import { redirect } from "react-router";
import { path } from "~/utils/path";
-import { redirectBeforeLoaders } from "~/utils/redirect.server";
export async function loader({ params }: LoaderFunctionArgs) {
const { receiptId } = params;
diff --git a/apps/erp/app/routes/x+/reports+/_index.tsx b/apps/erp/app/routes/x+/reports+/_index.tsx
index 2640fa7123..28d6dcfd67 100644
--- a/apps/erp/app/routes/x+/reports+/_index.tsx
+++ b/apps/erp/app/routes/x+/reports+/_index.tsx
@@ -3,6 +3,7 @@
// including ports, remain AGPLv3; serving them over a network requires releasing their source.
import { requirePermissions } from "@carbon/auth/auth.server";
+import { redirectBeforeLoaders } from "@carbon/utils";
import type { LoaderFunctionArgs } from "react-router";
import { redirect } from "react-router";
import { path } from "~/utils/path";
@@ -16,3 +17,5 @@ export async function loader({ request }: LoaderFunctionArgs) {
});
throw redirect(path.to.reports);
}
+
+export const middleware = [redirectBeforeLoaders(loader)];
diff --git a/apps/erp/app/routes/x+/sales-invoice+/$invoiceId._index.tsx b/apps/erp/app/routes/x+/sales-invoice+/$invoiceId._index.tsx
index 719dbe1a98..8fb8339e03 100644
--- a/apps/erp/app/routes/x+/sales-invoice+/$invoiceId._index.tsx
+++ b/apps/erp/app/routes/x+/sales-invoice+/$invoiceId._index.tsx
@@ -3,10 +3,10 @@
// including ports, remain AGPLv3; serving them over a network requires releasing their source.
import { notFound } from "@carbon/auth";
+import { redirectBeforeLoaders } from "@carbon/utils";
import type { LoaderFunctionArgs } from "react-router";
import { redirect } from "react-router";
import { path } from "~/utils/path";
-import { redirectBeforeLoaders } from "~/utils/redirect.server";
export async function loader({ params }: LoaderFunctionArgs) {
const { invoiceId } = params;
diff --git a/apps/erp/app/routes/x+/sales-order+/$orderId._index.tsx b/apps/erp/app/routes/x+/sales-order+/$orderId._index.tsx
index 8f89ff98d2..3fb36053f9 100644
--- a/apps/erp/app/routes/x+/sales-order+/$orderId._index.tsx
+++ b/apps/erp/app/routes/x+/sales-order+/$orderId._index.tsx
@@ -2,10 +2,10 @@
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.
+import { redirectBeforeLoaders } from "@carbon/utils";
import type { LoaderFunctionArgs } from "react-router";
import { redirect } from "react-router";
import { path } from "~/utils/path";
-import { redirectBeforeLoaders } from "~/utils/redirect.server";
export async function loader({ params }: LoaderFunctionArgs) {
const { orderId } = params;
diff --git a/apps/erp/app/routes/x+/sales-return-order+/$id._index.tsx b/apps/erp/app/routes/x+/sales-return-order+/$id._index.tsx
index df4a969461..4f049d15c2 100644
--- a/apps/erp/app/routes/x+/sales-return-order+/$id._index.tsx
+++ b/apps/erp/app/routes/x+/sales-return-order+/$id._index.tsx
@@ -2,10 +2,10 @@
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.
+import { redirectBeforeLoaders } from "@carbon/utils";
import type { LoaderFunctionArgs } from "react-router";
import { redirect } from "react-router";
import { path } from "~/utils/path";
-import { redirectBeforeLoaders } from "~/utils/redirect.server";
export async function loader({ params }: LoaderFunctionArgs) {
const { id } = params;
diff --git a/apps/erp/app/routes/x+/sales-rfq+/$rfqId._index.tsx b/apps/erp/app/routes/x+/sales-rfq+/$rfqId._index.tsx
index 0c81ea0acc..36e397bc3c 100644
--- a/apps/erp/app/routes/x+/sales-rfq+/$rfqId._index.tsx
+++ b/apps/erp/app/routes/x+/sales-rfq+/$rfqId._index.tsx
@@ -2,10 +2,10 @@
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.
+import { redirectBeforeLoaders } from "@carbon/utils";
import type { LoaderFunctionArgs } from "react-router";
import { redirect } from "react-router";
import { path } from "~/utils/path";
-import { redirectBeforeLoaders } from "~/utils/redirect.server";
export async function loader({ params }: LoaderFunctionArgs) {
const { rfqId } = params;
diff --git a/apps/erp/app/routes/x+/service+/$itemId._index.tsx b/apps/erp/app/routes/x+/service+/$itemId._index.tsx
index 70ceaa81cd..f4220e4eec 100644
--- a/apps/erp/app/routes/x+/service+/$itemId._index.tsx
+++ b/apps/erp/app/routes/x+/service+/$itemId._index.tsx
@@ -2,10 +2,10 @@
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.
+import { redirectBeforeLoaders } from "@carbon/utils";
import type { LoaderFunctionArgs } from "react-router";
import { redirect } from "react-router";
import { path } from "~/utils/path";
-import { redirectBeforeLoaders } from "~/utils/redirect.server";
export async function loader({ params }: LoaderFunctionArgs) {
const { itemId } = params;
diff --git a/apps/erp/app/routes/x+/settings+/_index.tsx b/apps/erp/app/routes/x+/settings+/_index.tsx
index 4ae8cb4aea..8e5ffe27ca 100644
--- a/apps/erp/app/routes/x+/settings+/_index.tsx
+++ b/apps/erp/app/routes/x+/settings+/_index.tsx
@@ -2,9 +2,12 @@
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.
+import { redirectBeforeLoaders } from "@carbon/utils";
import { redirect } from "react-router";
import { path } from "~/utils/path";
export async function loader() {
throw redirect(path.to.company);
}
+
+export const middleware = [redirectBeforeLoaders(loader)];
diff --git a/apps/erp/app/routes/x+/shipment+/$shipmentId._index.tsx b/apps/erp/app/routes/x+/shipment+/$shipmentId._index.tsx
index 1a24eb0159..0e8d00497a 100644
--- a/apps/erp/app/routes/x+/shipment+/$shipmentId._index.tsx
+++ b/apps/erp/app/routes/x+/shipment+/$shipmentId._index.tsx
@@ -2,10 +2,10 @@
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.
+import { redirectBeforeLoaders } from "@carbon/utils";
import type { LoaderFunctionArgs } from "react-router";
import { redirect } from "react-router";
import { path } from "~/utils/path";
-import { redirectBeforeLoaders } from "~/utils/redirect.server";
export async function loader({ params }: LoaderFunctionArgs) {
const { shipmentId } = params;
diff --git a/apps/erp/app/routes/x+/supplier+/$supplierId._index.tsx b/apps/erp/app/routes/x+/supplier+/$supplierId._index.tsx
index 7ea7b1feb7..4b99dcadee 100644
--- a/apps/erp/app/routes/x+/supplier+/$supplierId._index.tsx
+++ b/apps/erp/app/routes/x+/supplier+/$supplierId._index.tsx
@@ -2,10 +2,10 @@
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.
+import { redirectBeforeLoaders } from "@carbon/utils";
import type { LoaderFunctionArgs } from "react-router";
import { redirect } from "react-router";
import { path } from "~/utils/path";
-import { redirectBeforeLoaders } from "~/utils/redirect.server";
export async function loader({ params }: LoaderFunctionArgs) {
const { supplierId } = params;
diff --git a/apps/erp/app/routes/x+/supplier-quote+/$id._index.tsx b/apps/erp/app/routes/x+/supplier-quote+/$id._index.tsx
index 10f423d59d..b4b1a978e9 100644
--- a/apps/erp/app/routes/x+/supplier-quote+/$id._index.tsx
+++ b/apps/erp/app/routes/x+/supplier-quote+/$id._index.tsx
@@ -2,10 +2,10 @@
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.
+import { redirectBeforeLoaders } from "@carbon/utils";
import type { LoaderFunctionArgs } from "react-router";
import { redirect } from "react-router";
import { path } from "~/utils/path";
-import { redirectBeforeLoaders } from "~/utils/redirect.server";
export async function loader({ params }: LoaderFunctionArgs) {
const { id } = params;
diff --git a/apps/erp/app/routes/x+/tool+/$itemId._index.tsx b/apps/erp/app/routes/x+/tool+/$itemId._index.tsx
index 5d9adea4f7..87c3b3bf8a 100644
--- a/apps/erp/app/routes/x+/tool+/$itemId._index.tsx
+++ b/apps/erp/app/routes/x+/tool+/$itemId._index.tsx
@@ -2,10 +2,10 @@
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.
+import { redirectBeforeLoaders } from "@carbon/utils";
import type { LoaderFunctionArgs } from "react-router";
import { redirect } from "react-router";
import { path } from "~/utils/path";
-import { redirectBeforeLoaders } from "~/utils/redirect.server";
export async function loader({ params }: LoaderFunctionArgs) {
const { itemId } = params;
diff --git a/apps/erp/app/routes/x+/users+/_index.tsx b/apps/erp/app/routes/x+/users+/_index.tsx
index e8f17fde19..307bde44f3 100644
--- a/apps/erp/app/routes/x+/users+/_index.tsx
+++ b/apps/erp/app/routes/x+/users+/_index.tsx
@@ -2,9 +2,12 @@
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.
+import { redirectBeforeLoaders } from "@carbon/utils";
import { redirect } from "react-router";
import { path } from "~/utils/path";
export async function loader() {
throw redirect(path.to.employeeAccounts);
}
+
+export const middleware = [redirectBeforeLoaders(loader)];
diff --git a/apps/erp/app/routes/x+/warehouse-transfer+/$transferId._index.tsx b/apps/erp/app/routes/x+/warehouse-transfer+/$transferId._index.tsx
index 8587c2c4ee..ee55565161 100644
--- a/apps/erp/app/routes/x+/warehouse-transfer+/$transferId._index.tsx
+++ b/apps/erp/app/routes/x+/warehouse-transfer+/$transferId._index.tsx
@@ -2,10 +2,10 @@
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.
+import { redirectBeforeLoaders } from "@carbon/utils";
import type { LoaderFunctionArgs } from "react-router";
import { redirect } from "react-router";
import { path } from "~/utils/path";
-import { redirectBeforeLoaders } from "~/utils/redirect.server";
export async function loader({ params }: LoaderFunctionArgs) {
const { transferId } = params;
diff --git a/apps/erp/app/utils/redirect.server.test.ts b/apps/erp/app/utils/redirect-before-loaders.test.ts
similarity index 71%
rename from apps/erp/app/utils/redirect.server.test.ts
rename to apps/erp/app/utils/redirect-before-loaders.test.ts
index 63a52a5b0e..f1c04c1a17 100644
--- a/apps/erp/app/utils/redirect.server.test.ts
+++ b/apps/erp/app/utils/redirect-before-loaders.test.ts
@@ -2,6 +2,7 @@
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.
+import { redirectBeforeLoaders } from "@carbon/utils";
import {
createStaticHandler,
type LoaderFunctionArgs,
@@ -9,7 +10,6 @@ import {
redirect
} from "react-router";
import { describe, expect, it, vi } from "vitest";
-import { redirectBeforeLoaders } from "./redirect.server";
// The shape of every entity page: a layout with a loader and an action, and an
// index route that only redirects to the details child.
@@ -66,6 +66,36 @@ describe("redirectBeforeLoaders", () => {
expect(layoutAction).toHaveBeenCalledTimes(1);
});
+ it("honours a loader that returns its redirect instead of throwing it", async () => {
+ const parentLoader = vi.fn(async () => ({}));
+ const indexLoader = () => redirect("/x/operations");
+ const handler = createStaticHandler([
+ {
+ id: "shell",
+ path: "/x",
+ loader: parentLoader,
+ children: [
+ {
+ id: "index",
+ index: true,
+ loader: indexLoader,
+ middleware: [redirectBeforeLoaders(indexLoader)]
+ }
+ ]
+ }
+ ]);
+ const request = new Request("http://mes/x");
+ const response = (await handler.query(request, {
+ requestContext: new RouterContextProvider(),
+ generateMiddlewareResponse: async (query) => {
+ const inner = await query(request);
+ return inner instanceof Response ? inner : new Response("rendered");
+ }
+ })) as Response;
+ expect(response.headers.get("Location")).toBe("/x/operations");
+ expect(parentLoader).not.toHaveBeenCalled();
+ });
+
it("leaves the details URL alone", async () => {
const { layoutLoader, run } = entityRoutes();
const response = await run(new Request("http://erp/x/issue/nc_1/details"));
diff --git a/apps/erp/app/utils/redirect.server.ts b/apps/erp/app/utils/redirect.server.ts
deleted file mode 100644
index fe76426ff1..0000000000
--- a/apps/erp/app/utils/redirect.server.ts
+++ /dev/null
@@ -1,32 +0,0 @@
-// SPDX-License-Identifier: AGPL-3.0-only
-// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
-// including ports, remain AGPLv3; serving them over a network requires releasing their source.
-
-import type { LoaderFunctionArgs, MiddlewareFunction } from "react-router";
-
-/**
- * For an index route whose loader only redirects to a child
- * (`/x/issue/:id` → `/x/issue/:id/details`):
- *
- * export const middleware = [redirectBeforeLoaders(loader)];
- *
- * The loaders of a matched branch run in parallel, so the parent layout's
- * loader ran in full for a request the index was about to redirect — once per
- * hover on a prefetching link to the bare URL, and twice per click. Middleware
- * runs before any loader, so the redirect is all such a request costs.
- *
- * GET and HEAD only: a form may post to the bare URL to reach the layout's
- * action. The loader stays exported, since it is what makes a client
- * navigation to the bare URL ask the server at all. A fetcher that LOADS the
- * bare URL to read the layout's data would be redirected as well.
- */
-export function redirectBeforeLoaders<Result>(
- loader: (args: LoaderFunctionArgs) => unknown
-): MiddlewareFunction<Result> {
- return async (args, next) => {
- if (args.request.method === "GET" || args.request.method === "HEAD") {
- await loader(args);
- }
- return next();
- };
-}
diff --git a/apps/mes/app/routes/x+/_index.tsx b/apps/mes/app/routes/x+/_index.tsx
index db788cf52f..24e836fb6e 100644
--- a/apps/mes/app/routes/x+/_index.tsx
+++ b/apps/mes/app/routes/x+/_index.tsx
@@ -2,6 +2,7 @@
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.
+import { redirectBeforeLoaders } from "@carbon/utils";
import type { LoaderFunction } from "react-router";
import { redirect } from "react-router";
import { path } from "~/utils/path";
@@ -9,3 +10,5 @@ import { path } from "~/utils/path";
export const loader: LoaderFunction = async ({ request }) => {
return redirect(path.to.operations);
};
+
+export const middleware = [redirectBeforeLoaders(loader)];
diff --git a/packages/checks/src/conformance/index-redirect-before-loaders.test.ts b/packages/checks/src/conformance/index-redirect-before-loaders.test.ts
new file mode 100644
index 0000000000..65891b35e4
--- /dev/null
+++ b/packages/checks/src/conformance/index-redirect-before-loaders.test.ts
@@ -0,0 +1,65 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
+// including ports, remain AGPLv3; serving them over a network requires releasing their source.
+
+import { describe, expect, it } from "vitest";
+import { indexRedirectBeforeLoaders } from "./index-redirect-before-loaders";
+
+const INDEX = "apps/erp/app/routes/x+/issue+/$id._index.tsx";
+const scan = (contents: string, file = INDEX) =>
+ indexRedirectBeforeLoaders.scan(file, contents);
+
+const REDIRECT_ONLY = `import { redirect } from "react-router";
+
+export async function loader({ params }: LoaderFunctionArgs) {
+ throw redirect(path.to.issueDetails(params.id));
+}
+`;
+
+describe("indexRedirectBeforeLoaders", () => {
+ it("flags the shape that ran the issue layout for every hovered link", () => {
+ const violations = scan(REDIRECT_ONLY);
+ expect(violations).toHaveLength(1);
+ expect(violations[0]!.line).toBe(3);
+ });
+
+ it("accepts the route once the redirect is exported as middleware", () => {
+ expect(
+ scan(
+ `${REDIRECT_ONLY}\nexport const middleware = [redirectBeforeLoaders(loader)];\n`
+ )
+ ).toEqual([]);
+ });
+
+ it("covers a module root and a const loader that returns its redirect", () => {
+ expect(
+ scan(
+ `export const loader: LoaderFunction = async () => {\n return redirect(path.to.operations);\n};\n`,
+ "apps/mes/app/routes/x+/_index.tsx"
+ )
+ ).toHaveLength(1);
+ });
+
+ it("ignores an index route that renders a page", () => {
+ expect(
+ scan(
+ `${REDIRECT_ONLY}\nexport default function Page() {\n return null;\n}\n`
+ )
+ ).toEqual([]);
+ });
+
+ it("ignores a resource route, and any file that is not an index route", () => {
+ expect(
+ scan(`export async function loader() {\n return { ok: true };\n}\n`)
+ ).toEqual([]);
+ expect(
+ scan(REDIRECT_ONLY, "apps/erp/app/routes/x+/issue+/$id.tsx")
+ ).toEqual([]);
+ });
+
+ it("is not satisfied by a middleware export that does something else", () => {
+ expect(
+ scan(`${REDIRECT_ONLY}\nexport const middleware = [authMiddleware];\n`)
+ ).toHaveLength(1);
+ });
+});
diff --git a/packages/checks/src/conformance/index-redirect-before-loaders.ts b/packages/checks/src/conformance/index-redirect-before-loaders.ts
new file mode 100644
index 0000000000..f507905c0a
--- /dev/null
+++ b/packages/checks/src/conformance/index-redirect-before-loaders.ts
@@ -0,0 +1,51 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
+// including ports, remain AGPLv3; serving them over a network requires releasing their source.
+
+import type { ConformanceCheck, Violation } from "../check";
+
+// An index route that renders nothing exists to redirect (`/x/issue/:id` →
+// `…/details`). React Router runs the loaders of a matched branch in parallel,
+// so its parents' loaders ran in full for a request that was about to be
+// redirected: one hover prefetch of a bare issue link cost ~20 queries, thrown
+// away, and a click cost them twice. Exporting the redirect as middleware runs
+// it before any loader.
+const INDEX_ROUTE = /\/app\/routes\/(?:.*[/.])?_index\.tsx$/;
+const LOADER = /^export\s+(?:async\s+function|function|const)\s+loader\b/m;
+const DEFAULT_EXPORT = /^export\s+default\b|^export\s*\{[^}]*\bdefault\b/m;
+const REDIRECT = /\bredirect\s*\(/;
+const MIDDLEWARE =
+ /^export\s+const\s+middleware\b[^;]*\bredirectBeforeLoaders\s*\(\s*loader\s*\)/m;
+
+export const indexRedirectBeforeLoaders: ConformanceCheck = {
+ id: "index-redirect-before-loaders",
+ description:
+ "An index route that only redirects must run the redirect before its parents' loaders",
+ provenance: {
+ deprecates:
+ "a redirect-only index route whose redirect runs alongside its parents' loaders",
+ replacedBy:
+ "export const middleware = [redirectBeforeLoaders(loader)] (@carbon/utils)",
+ since: "2026-10-02"
+ },
+ scan(file: string, contents: string): Violation[] {
+ if (!INDEX_ROUTE.test(file)) return [];
+ const loader = LOADER.exec(contents);
+ // A route with a default export renders a page; one without a redirect is
+ // a resource route. Neither is a redirect-only route.
+ if (!loader || DEFAULT_EXPORT.test(contents) || !REDIRECT.test(contents)) {
+ return [];
+ }
+ if (MIDDLEWARE.test(contents)) return [];
+ const line = contents.slice(0, loader.index).split("\n").length;
+ return [
+ {
+ file,
+ line,
+ snippet: contents.split("\n")[line - 1]?.trim() ?? "",
+ message:
+ "Add `export const middleware = [redirectBeforeLoaders(loader)];` (@carbon/utils) so the redirect runs before the parents' loaders"
+ }
+ ];
+ }
+};
diff --git a/packages/checks/src/index.ts b/packages/checks/src/index.ts
index b3d6488c0f..c11730e333 100644
--- a/packages/checks/src/index.ts
+++ b/packages/checks/src/index.ts
@@ -11,6 +11,7 @@ export type {
} from "./check";
export { findClobbers, objectRefs } from "./clobber";
export { edgeFunctionAuthorizesCaller } from "./conformance/edge-function-authorizes-caller";
+export { indexRedirectBeforeLoaders } from "./conformance/index-redirect-before-loaders";
export { moduleShape } from "./conformance/module-shape";
export { noDbClientInService } from "./conformance/no-db-client-in-service";
export { noDefaultOnEffects } from "./conformance/no-default-on-effects";
diff --git a/packages/checks/src/run.ts b/packages/checks/src/run.ts
index 94db3d038c..83cd5a5e63 100644
--- a/packages/checks/src/run.ts
+++ b/packages/checks/src/run.ts
@@ -13,6 +13,7 @@ import type {
Violation
} from "./check";
import { edgeFunctionAuthorizesCaller } from "./conformance/edge-function-authorizes-caller";
+import { indexRedirectBeforeLoaders } from "./conformance/index-redirect-before-loaders";
import { moduleShape } from "./conformance/module-shape";
import {
MANAGED_FUNCTION_SETS,
@@ -45,7 +46,8 @@ import { loadModules, modulesDir } from "./sources/modules";
import { loadServerFiles } from "./sources/server-files";
import {
loadTypescriptFiles,
- REQUEST_HANDLING_ROOTS
+ REQUEST_HANDLING_ROOTS,
+ ROUTE_ROOTS
} from "./sources/typescript";
export const CONFORMANCE_CHECKS: ConformanceCheck[] = [
@@ -149,6 +151,9 @@ export function collectFindings(root: string = repoRoot()): Finding[] {
...scanAll(loadTypescriptFiles(root, REQUEST_HANDLING_ROOTS), [
noRawForwardedHeaders
]),
+ ...scanAll(loadTypescriptFiles(root, ROUTE_ROOTS), [
+ indexRedirectBeforeLoaders
+ ]),
...scanAll(loadEdgeFunctions(root), EDGE_FUNCTION_CHECKS),
...scanAll(loadLicenseFiles(root), [spdxLicenseHeader])
];
diff --git a/packages/checks/src/sources/typescript.ts b/packages/checks/src/sources/typescript.ts
index afc9e27924..2b92ad9d3b 100644
--- a/packages/checks/src/sources/typescript.ts
+++ b/packages/checks/src/sources/typescript.ts
@@ -75,6 +75,14 @@ export const REQUEST_HANDLING_ROOTS = [
"packages/utils/src"
];
+// The route trees of every React Router app: index-redirect-before-loaders.
+export const ROUTE_ROOTS = [
+ "apps/erp/app/routes",
+ "apps/mes/app/routes",
+ "apps/academy/app/routes",
+ "apps/starter/app/routes"
+];
+
export function loadTypescriptFiles(
root: string,
roots: string[] = TYPESCRIPT_ROOTS
diff --git a/packages/utils/AGENTS.md b/packages/utils/AGENTS.md
index eacb4a67ca..97f50c9e2d 100644
--- a/packages/utils/AGENTS.md
+++ b/packages/utils/AGENTS.md
@@ -49,6 +49,7 @@ pnpm --filter @carbon/utils typecheck
| `format` | The ONLY place display/input digit counts are chosen: `moneyFormatOptions` (settlement — the currency's decimals are floor AND ceiling), `rateFormatOptions` (per-unit RATE — those decimals are only the floor, ceiling is `SCALE`), the `PERCENT_FORMAT` / `PERCENT_POINTS_FORMAT` / `SCALE_FORMAT` constants, `cldrCurrencyDecimals`, their `format*` helpers, and `INPUT_FORMAT` / `INPUT_STEP` for editable fields. Call sites pick a KIND, never a digit count |
| `string` | Slugify, truncate, camelCase/titleCase conversions |
| `revalidate` | `shouldRevalidate` predicates: `isSearchParamOnlyNavigation` (root loaders), `isUnaffectedByNavigation` (detail layouts — names the route/search params the loader reads) |
+| `redirect` | `redirectBeforeLoaders(loader)` — route middleware for an index route that only redirects, so the redirect runs before its parents' loaders |
| `status` | Status resolution, status color mapping |
| `rules` | Rule engine: condition AST, the shared `Operator` vocabulary, JIT-compiled evaluator + surfaces for storage rules and sales rules |
| `rule-filters` | Item scoping for broadcast rules (`ItemFilter`, `ruleAppliesToItem`, `toItemFilter`) — family-neutral, split out of `rules.ts` |
diff --git a/packages/utils/src/index.ts b/packages/utils/src/index.ts
index d9cc0a8a08..2581faa1ef 100644
--- a/packages/utils/src/index.ts
+++ b/packages/utils/src/index.ts
@@ -35,6 +35,7 @@ export * from "./object";
export * from "./payment-funding";
export * from "./precision";
export * from "./receiving";
+export * from "./redirect";
export * from "./revalidate";
export * from "./rule-filters";
export * from "./rules";
diff --git a/packages/utils/src/redirect.ts b/packages/utils/src/redirect.ts
new file mode 100644
index 0000000000..c586d20f6d
--- /dev/null
+++ b/packages/utils/src/redirect.ts
@@ -0,0 +1,39 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
+// including ports, remain AGPLv3; serving them over a network requires releasing their source.
+
+/**
+ * For an index route whose loader only redirects
+ * (`/x/issue/:id` → `/x/issue/:id/details`, `/x/settings` → its first page):
+ *
+ * export const middleware = [redirectBeforeLoaders(loader)];
+ *
+ * The loaders of a matched branch run in parallel, so every parent's loader
+ * ran in full for a request the index was about to redirect — once per
+ * prefetch of the bare URL, and twice per click. Middleware runs before any
+ * loader, so the redirect is all such a request costs.
+ *
+ * GET and HEAD only: a form may post to the bare URL to reach a parent's
+ * action. The loader stays exported, since it is what makes a client
+ * navigation to the bare URL ask the server at all. A fetcher that LOADS the
+ * bare URL to read a parent's data would be redirected as well.
+ *
+ * Every index route that has a loader and renders nothing must export this:
+ * the `index-redirect-before-loaders` check (`@carbon/checks`) enforces it.
+ */
+export function redirectBeforeLoaders<Args extends { request: Request }>(
+ loader: (args: Args) => unknown
+) {
+ return async <Result>(
+ args: Args,
+ next: () => Promise<Result>
+ ): Promise<Result> => {
+ const { method } = args.request;
+ if (method === "GET" || method === "HEAD") {
+ // A loader may return its redirect instead of throwing it.
+ const result = await loader(args);
+ if (result instanceof Response) return result as Result;
+ }
+ return next();
+ };
+}