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(erp): redirect bare entity URLs before the layout loader runs

Sidwebworks543a65b7b003
diff --git a/apps/erp/app/routes/x+/consumable+/$itemId._index.tsx b/apps/erp/app/routes/x+/consumable+/$itemId._index.tsx
index 38d1246340..c13f561246 100644
--- a/apps/erp/app/routes/x+/consumable+/$itemId._index.tsx
+++ b/apps/erp/app/routes/x+/consumable+/$itemId._index.tsx
@@ -5,9 +5,12 @@
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;
if (!itemId) throw new Error("Could not find itemId");
throw redirect(path.to.consumableDetails(itemId));
}
+
+export const middleware = [redirectBeforeLoaders(loader)];
diff --git a/apps/erp/app/routes/x+/customer+/$customerId._index.tsx b/apps/erp/app/routes/x+/customer+/$customerId._index.tsx
index 4a076c8f42..6cefb7db34 100644
--- a/apps/erp/app/routes/x+/customer+/$customerId._index.tsx
+++ b/apps/erp/app/routes/x+/customer+/$customerId._index.tsx
@@ -5,9 +5,12 @@
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;
if (!customerId) throw new Error("Could not find customerId");
throw redirect(path.to.customerDetails(customerId));
}
+
+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 da40949395..13d7806bd8 100644
--- a/apps/erp/app/routes/x+/issue+/$id._index.tsx
+++ b/apps/erp/app/routes/x+/issue+/$id._index.tsx
@@ -5,9 +5,12 @@
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;
if (!id) throw new Error("Could not find id");
throw redirect(path.to.issueDetails(id));
}
+
+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 6c04b7b684..605d9cd8f8 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
@@ -5,9 +5,12 @@
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;
if (!id) throw new Error("Could not find id");
throw redirect(path.to.changeNoticeDetails(id));
}
+
+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 06fd857b2c..97be47ef62 100644
--- a/apps/erp/app/routes/x+/job+/$jobId._index.tsx
+++ b/apps/erp/app/routes/x+/job+/$jobId._index.tsx
@@ -5,9 +5,12 @@
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;
if (!jobId) throw new Error("Could not find jobId");
throw redirect(path.to.jobDetails(jobId));
}
+
+export const middleware = [redirectBeforeLoaders(loader)];
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 e447fba633..ad736949c9 100644
--- a/apps/erp/app/routes/x+/journal-entry+/$journalEntryId._index.tsx
+++ b/apps/erp/app/routes/x+/journal-entry+/$journalEntryId._index.tsx
@@ -5,9 +5,12 @@
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;
if (!journalEntryId) throw new Error("Could not find journalEntryId");
throw redirect(path.to.journalEntryDetails(journalEntryId));
}
+
+export const middleware = [redirectBeforeLoaders(loader)];
diff --git a/apps/erp/app/routes/x+/material+/$itemId._index.tsx b/apps/erp/app/routes/x+/material+/$itemId._index.tsx
index 30c82d149e..58ef397fac 100644
--- a/apps/erp/app/routes/x+/material+/$itemId._index.tsx
+++ b/apps/erp/app/routes/x+/material+/$itemId._index.tsx
@@ -5,9 +5,12 @@
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;
if (!itemId) throw new Error("Could not find itemId");
throw redirect(path.to.materialDetails(itemId));
}
+
+export const middleware = [redirectBeforeLoaders(loader)];
diff --git a/apps/erp/app/routes/x+/part+/$itemId._index.tsx b/apps/erp/app/routes/x+/part+/$itemId._index.tsx
index 1df5a1fcf9..03abef67d9 100644
--- a/apps/erp/app/routes/x+/part+/$itemId._index.tsx
+++ b/apps/erp/app/routes/x+/part+/$itemId._index.tsx
@@ -5,9 +5,12 @@
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;
if (!itemId) throw new Error("Could not find itemId");
throw redirect(path.to.partDetails(itemId));
}
+
+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 cb3dc201e8..85a793ae6c 100644
--- a/apps/erp/app/routes/x+/person+/$personId._index.tsx
+++ b/apps/erp/app/routes/x+/person+/$personId._index.tsx
@@ -5,9 +5,12 @@
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;
if (!personId) throw new Error("Could not find personId");
throw redirect(path.to.personDetails(personId));
}
+
+export const middleware = [redirectBeforeLoaders(loader)];
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 19b188416f..b3a9d936ad 100644
--- a/apps/erp/app/routes/x+/picking-list+/$pickingListId._index.tsx
+++ b/apps/erp/app/routes/x+/picking-list+/$pickingListId._index.tsx
@@ -5,9 +5,12 @@
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;
if (!pickingListId) throw new Error("Could not find pickingListId");
throw redirect(path.to.pickingListDetails(pickingListId));
}
+
+export const middleware = [redirectBeforeLoaders(loader)];
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 26279a71cb..ae17b58c10 100644
--- a/apps/erp/app/routes/x+/purchase-invoice+/$invoiceId._index.tsx
+++ b/apps/erp/app/routes/x+/purchase-invoice+/$invoiceId._index.tsx
@@ -6,9 +6,12 @@ import { notFound } from "@carbon/auth";
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;
if (!invoiceId) throw notFound("Could not find invoiceId");
throw redirect(path.to.purchaseInvoiceDetails(invoiceId));
}
+
+export const middleware = [redirectBeforeLoaders(loader)];
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 ac1065a16b..74365fb16d 100644
--- a/apps/erp/app/routes/x+/purchase-order+/$orderId._index.tsx
+++ b/apps/erp/app/routes/x+/purchase-order+/$orderId._index.tsx
@@ -5,9 +5,12 @@
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;
if (!orderId) throw new Error("Could not find orderId");
throw redirect(path.to.purchaseOrderDetails(orderId));
}
+
+export const middleware = [redirectBeforeLoaders(loader)];
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 6fa751c166..5a78177637 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
@@ -5,9 +5,12 @@
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;
if (!id) throw new Error("Could not find id");
throw redirect(path.to.purchaseReturnOrderDetails(id));
}
+
+export const middleware = [redirectBeforeLoaders(loader)];
diff --git a/apps/erp/app/routes/x+/quote+/$quoteId._index.tsx b/apps/erp/app/routes/x+/quote+/$quoteId._index.tsx
index b8431c9915..ab007d53b5 100644
--- a/apps/erp/app/routes/x+/quote+/$quoteId._index.tsx
+++ b/apps/erp/app/routes/x+/quote+/$quoteId._index.tsx
@@ -5,9 +5,12 @@
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;
if (!quoteId) throw new Error("Could not find quoteId");
throw redirect(path.to.quoteDetails(quoteId));
}
+
+export const middleware = [redirectBeforeLoaders(loader)];
diff --git a/apps/erp/app/routes/x+/receipt+/$receiptId._index.tsx b/apps/erp/app/routes/x+/receipt+/$receiptId._index.tsx
index 15e33cc504..3af735c594 100644
--- a/apps/erp/app/routes/x+/receipt+/$receiptId._index.tsx
+++ b/apps/erp/app/routes/x+/receipt+/$receiptId._index.tsx
@@ -5,9 +5,12 @@
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;
if (!receiptId) throw new Error("Could not find receiptId");
throw redirect(path.to.receiptDetails(receiptId));
}
+
+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 b0b64afa63..719dbe1a98 100644
--- a/apps/erp/app/routes/x+/sales-invoice+/$invoiceId._index.tsx
+++ b/apps/erp/app/routes/x+/sales-invoice+/$invoiceId._index.tsx
@@ -6,9 +6,12 @@ import { notFound } from "@carbon/auth";
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;
if (!invoiceId) throw notFound("Could not find invoiceId");
throw redirect(path.to.salesInvoiceDetails(invoiceId));
}
+
+export const middleware = [redirectBeforeLoaders(loader)];
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 8358878990..8f89ff98d2 100644
--- a/apps/erp/app/routes/x+/sales-order+/$orderId._index.tsx
+++ b/apps/erp/app/routes/x+/sales-order+/$orderId._index.tsx
@@ -5,9 +5,12 @@
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;
if (!orderId) throw new Error("Could not find orderId");
throw redirect(path.to.salesOrderDetails(orderId));
}
+
+export const middleware = [redirectBeforeLoaders(loader)];
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 689e333f96..df4a969461 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
@@ -5,9 +5,12 @@
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;
if (!id) throw new Error("Could not find id");
throw redirect(path.to.salesReturnOrderDetails(id));
}
+
+export const middleware = [redirectBeforeLoaders(loader)];
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 ccf8e2d626..0c81ea0acc 100644
--- a/apps/erp/app/routes/x+/sales-rfq+/$rfqId._index.tsx
+++ b/apps/erp/app/routes/x+/sales-rfq+/$rfqId._index.tsx
@@ -5,9 +5,12 @@
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;
if (!rfqId) throw new Error("Could not find rfqId");
throw redirect(path.to.salesRfqDetails(rfqId));
}
+
+export const middleware = [redirectBeforeLoaders(loader)];
diff --git a/apps/erp/app/routes/x+/service+/$itemId._index.tsx b/apps/erp/app/routes/x+/service+/$itemId._index.tsx
index cf433824e0..70ceaa81cd 100644
--- a/apps/erp/app/routes/x+/service+/$itemId._index.tsx
+++ b/apps/erp/app/routes/x+/service+/$itemId._index.tsx
@@ -5,9 +5,12 @@
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;
if (!itemId) throw new Error("Could not find itemId");
throw redirect(path.to.serviceDetails(itemId));
}
+
+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 1746931841..1a24eb0159 100644
--- a/apps/erp/app/routes/x+/shipment+/$shipmentId._index.tsx
+++ b/apps/erp/app/routes/x+/shipment+/$shipmentId._index.tsx
@@ -5,9 +5,12 @@
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;
if (!shipmentId) throw new Error("Could not find shipmentId");
throw redirect(path.to.shipmentDetails(shipmentId));
}
+
+export const middleware = [redirectBeforeLoaders(loader)];
diff --git a/apps/erp/app/routes/x+/supplier+/$supplierId._index.tsx b/apps/erp/app/routes/x+/supplier+/$supplierId._index.tsx
index 94b2c3eb6b..7ea7b1feb7 100644
--- a/apps/erp/app/routes/x+/supplier+/$supplierId._index.tsx
+++ b/apps/erp/app/routes/x+/supplier+/$supplierId._index.tsx
@@ -5,9 +5,12 @@
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;
if (!supplierId) throw new Error("Could not find supplierId");
throw redirect(path.to.supplierDetails(supplierId));
}
+
+export const middleware = [redirectBeforeLoaders(loader)];
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 91bcaa5fbb..10f423d59d 100644
--- a/apps/erp/app/routes/x+/supplier-quote+/$id._index.tsx
+++ b/apps/erp/app/routes/x+/supplier-quote+/$id._index.tsx
@@ -5,9 +5,12 @@
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;
if (!id) throw new Error("Could not find id");
throw redirect(path.to.supplierQuoteDetails(id));
}
+
+export const middleware = [redirectBeforeLoaders(loader)];
diff --git a/apps/erp/app/routes/x+/tool+/$itemId._index.tsx b/apps/erp/app/routes/x+/tool+/$itemId._index.tsx
index caff008104..5d9adea4f7 100644
--- a/apps/erp/app/routes/x+/tool+/$itemId._index.tsx
+++ b/apps/erp/app/routes/x+/tool+/$itemId._index.tsx
@@ -5,9 +5,12 @@
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;
if (!itemId) throw new Error("Could not find itemId");
throw redirect(path.to.toolDetails(itemId));
}
+
+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 53040a9672..8587c2c4ee 100644
--- a/apps/erp/app/routes/x+/warehouse-transfer+/$transferId._index.tsx
+++ b/apps/erp/app/routes/x+/warehouse-transfer+/$transferId._index.tsx
@@ -5,9 +5,12 @@
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;
if (!transferId) throw new Error("Could not find transferId");
throw redirect(path.to.warehouseTransferDetails(transferId));
}
+
+export const middleware = [redirectBeforeLoaders(loader)];
diff --git a/apps/erp/app/utils/redirect.server.test.ts b/apps/erp/app/utils/redirect.server.test.ts
new file mode 100644
index 0000000000..63a52a5b0e
--- /dev/null
+++ b/apps/erp/app/utils/redirect.server.test.ts
@@ -0,0 +1,75 @@
+// 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 {
+ createStaticHandler,
+ type LoaderFunctionArgs,
+ RouterContextProvider,
+ 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.
+function entityRoutes() {
+ const layoutLoader = vi.fn(async () => ({ entity: true }));
+ const layoutAction = vi.fn(async () => ({ saved: true }));
+ const indexLoader = ({ params }: LoaderFunctionArgs) => {
+ throw redirect(`/x/issue/${params.id}/details`);
+ };
+ const handler = createStaticHandler([
+ {
+ id: "layout",
+ path: "/x/issue/:id",
+ loader: layoutLoader,
+ action: layoutAction,
+ children: [
+ {
+ id: "index",
+ index: true,
+ loader: indexLoader,
+ middleware: [redirectBeforeLoaders(indexLoader)]
+ },
+ { id: "details", path: "details", loader: async () => ({}) }
+ ]
+ }
+ ]);
+ const run = async (request: Request) => {
+ const result = await handler.query(request, {
+ requestContext: new RouterContextProvider(),
+ generateMiddlewareResponse: async (query) => {
+ const inner = await query(request);
+ return inner instanceof Response ? inner : new Response("rendered");
+ }
+ });
+ return result as Response;
+ };
+ return { layoutAction, layoutLoader, run };
+}
+
+describe("redirectBeforeLoaders", () => {
+ it("redirects a GET of the bare URL without running the layout loader", async () => {
+ const { layoutLoader, run } = entityRoutes();
+ const response = await run(new Request("http://erp/x/issue/nc_1"));
+ expect(response.status).toBe(302);
+ expect(response.headers.get("Location")).toBe("/x/issue/nc_1/details");
+ expect(layoutLoader).not.toHaveBeenCalled();
+ });
+
+ it("lets a POST to the bare URL reach the layout's action", async () => {
+ const { layoutAction, run } = entityRoutes();
+ await run(
+ new Request("http://erp/x/issue/nc_1", { body: "a=1", method: "POST" })
+ );
+ expect(layoutAction).toHaveBeenCalledTimes(1);
+ });
+
+ it("leaves the details URL alone", async () => {
+ const { layoutLoader, run } = entityRoutes();
+ const response = await run(new Request("http://erp/x/issue/nc_1/details"));
+ expect(response.status).toBe(200);
+ expect(layoutLoader).toHaveBeenCalledTimes(1);
+ });
+});
diff --git a/apps/erp/app/utils/redirect.server.ts b/apps/erp/app/utils/redirect.server.ts
new file mode 100644
index 0000000000..fe76426ff1
--- /dev/null
+++ b/apps/erp/app/utils/redirect.server.ts
@@ -0,0 +1,32 @@
+// 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();
+ };
+}