Repository navigation
🚀 Fastify Route Auto Grouping Issue (CmdProtocol & CloudTask) #1081
Description
#1️⃣ Problem Description
When registering routes in Fastify using register(), routes with the same first letter are automatically grouped under a common node.
📌 Code for Route Registration (routes/index.js)
import { LoginRouter } from "./Login.js";
import { NetProtocolRouter } from "./NetProtocol.js";
import { CmdProtocolRouter } from "./CmdProtocol.js";
import { CloudTaskRouter } from "./CloudTask.js";
import { BotTelegramRouter } from "./BotTelegram.js";
import { EventTrrigerRouter } from "./EventTrriger.js";
export default async function router(fastify, _options) {
fastify.register(async (instance) => {
await instance.register(LoginRouter, { prefix: "/login" });
await instance.register(NetProtocolRouter, { prefix: "/netProtocol" });
await instance.register(CmdProtocolRouter, { prefix: "/cmdProtocol" });
await instance.register(EventTrrigerRouter, { prefix: "/eventTrriger" });
await instance.register(BotTelegramRouter, { prefix: "/botTelegram" });
await instance.register(CloudTaskRouter, { prefix: "/cloudTask" });
}, { prefix: '/api' });
}
2️⃣ printRoutes() Output
└── (empty root node)
├── /
│ └── api/
│ ├── login (POST)
│ │ └── / (POST)
│ ├── netProtocol (POST, GET, HEAD)
│ │ └── / (POST, GET, HEAD)
│ ├── c
│ │ ├── mdProtocol (POST)
│ │ │ └── / (POST)
│ │ └── loudTask/TallyingLotto/ (POST)
│ ├── eventTrriger/UpdateLotto/ (POST)
│ └── botTelegram/telegram/webhook/ (POST)
└── * (OPTIONS)
🚨 Issue:
cmdProtocol and cloudTask are grouped under a single c node instead of being separate routes.
Expected Output:
├── /api/
│ ├── login (POST)
│ ├── netProtocol (POST, GET, HEAD)
│ ├── cmdProtocol (POST)
│ ├── cloudTask/TallyingLotto (POST)
│ ├── eventTrriger/UpdateLotto (POST)
│ ├── botTelegram/telegram/webhook (POST)
3️⃣ Solutions Tried
✅ 1) Enabling caseSensitive: true → Did not work
const fastify = Fastify({
caseSensitive: true, // Prevents auto-grouping by case sensitivity
});
🚨 Still grouped under c
✅ 2) Registering routes individually → Did not work
await fastify.register(CmdProtocolRouter, { prefix: "/api/cmdProtocol" });
await fastify.register(CloudTaskRouter, { prefix: "/api/cloudTask" });
🚨 Still grouped under c
✅ 3) Using relative paths inside route files → Did not work
export default async function CmdProtocolRouter(fastify, _options) {
fastify.post('/', async (request, reply) => {
reply.send({ success: true });
});
}
🚨 Still grouped under c
4️⃣ Question
Why is Fastify automatically grouping routes with the same first letter?
Is there an official way to prevent this automatic grouping behavior?
Is this a bug, or is there an intended optimization in Fastify that causes this?
Activity
mcollina commentedon Feb 16, 2025
That is the radix prefix trie that Fastify uses to route your requests. It's a good thing that they are prefixed together, as it avoid quite a lot of checks at runtime.
What problems is this causing? Or it's just an output thing?