fastify / fastify/help

πŸš€ Fastify Route Auto Grouping Issue (CmdProtocol & CloudTask)

Open
#1,081 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
No language data
Stars
68
Forks
8
Avg merge
11h 2m
Merged PRs (30d)
2

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?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.