-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ignoreTrailingSlash doesn't work with wildcard routes #3331
Comments
Would you like to work on this bug? How would you fix it? |
I can probably work on it. Haven't delved into the code to figure out where the issue lies. I guess I wanted to confirm what I expected to happen was reasonable. Sounds like you agree, any suggestions on where to start looking in the code? |
I agree that there needs to be a way to make work what you are trying to do. However I'm not sure about the actual fix
I don't understand this at all. I think the best next step is to write a small example with a few example http calls to explain what you would like to achieve. |
Here's an example server with tests: import Fastify from "fastify";
import fetch from "node-fetch";
const fastify = new Fastify({ ignoreTrailingSlash: true });
fastify.get("/test/:stuff", (req, reply) => {
console.log("Request from ", req.url);
reply.send(`ok! ${req.params.stuff}`);
});
fastify.listen(3000, async (err, address) => {
if (err) throw err;
console.log(`server listening on ${address}`);
for (let path of ["/test/", "/test/example", "/test"]) {
console.log(`Testing GET ${path}`);
const res = await fetch(`http://localhost:3000${path}`);
if (res.status >= 200 && res.status < 300) {
const body = await res.text();
console.log(`${res.url} passed: ${res.status}`);
console.log(`response body: ${body}`);
} else {
console.log(`${res.url} failed: ${res.status}`);
console.log(`This is expected to work when ignoreTrailingSlash:true`)
}
}
}); |
I do not see it will create a infinite loop from your example. Some snippets below and full repro and test case on replit const Fastify = require("fastify");
const fastify = Fastify();
fastify.get("/test", (req, reply) => {
reply.redirect("/test/");
});
fastify.get("/test/*", (req, reply) => {
reply.send(`ok! ${req.params.stuff}`);
});
|
Now enable |
I'm not sure if it is expected to work when
|
This is fine if But the I think it should also have - |
I think your assumption for
More clear image for how const Fastify = require("fastify");
const fastify = Fastify({ ignoreTrailingSlash: true });
fastify.get("/test", (req, reply) => { reply.redirect("/test/"); });
// `/test/` will throw because it is registered at the same time of `/test` when `ignoreTrailingSlash` is enabled
fastify.get("/test/", (req, reply) => { reply.send(`ok! ${req.params.stuff}`); });
fastify.get("/test/*", (req, reply) => { reply.send(`ok! ${req.params.stuff}`); }); |
How I want my API to work is my business. I need |
const Fastify = require("fastify");
const fastify = Fastify();
fastify.get("/test", (req, reply) => {
reply.redirect("/test/");
});
fastify.get("/test/*", (req, reply) => {
reply.send(`ok! ${req.params.stuff}`);
}); if you need const Fastify = require("fastify");
const fastify = Fastify();
fastify.get("/test", (req, reply) => {
reply.redirect("/test/");
});
fastify.get("/test/", (req, reply) => {
reply.send('I am special')
});
fastify.get("/test/*", (req, reply) => {
reply.send(`ok! ${req.params.stuff}`);
}); |
yes, this SHOULD work. But as the repl shows as soon as you add The only thing that might work now would be : const Fastify = require("fastify");
const fastify = Fastify();
const myHandler = (req, reply) => {
const stuff = req?.params?.stuff;
reply.send(`ok! ${stuff}`);
}
fastify.get("/test/", myHandler);
});
fastify.get("/test/*", myHandler); But this seemed overly verbose and frustrating. |
When you understand the nature of
If it really need a fix to the problem of mixing Maybe a more clear document of this two option is needed? |
Sure, docs are always helpful. I can appreciate as someone who's intimately familiar with how fastify is built that this makes sense. To everyone who uses this package this is all fastify configs that should be modifying the same thing and aren't. Either way. That was just one attempt I made at resolving the underlying issue. |
Prerequisites
Fastify version
3.21.1
Plugin version
4.2.3
Node.js version
14.17.6
Operating system
macOS
Operating system version (i.e. 20.04, 11.3, 10)
11.6
Description
I want to listen on all routes under
/app
including/app
and/app/anything
.I want to serve the same html file for any of these routes.
This works for
/app/
and/app/:anything
.I originally tried to setup a redirect then from
/app
=>/app/
but because I hadignoreTrailingSlash
enabled this created an infinite loop. Basically the/app
didn't match/app/
but for some reason/app/
did match/app
even though the redirect was configured withprefixTrailingSlash: "no-slash"
.My guess was that this was being caused by
ignoreTrailingSlash
being applied to the redirect but not the initial catch all.Disabling
ignoreTrailingSlash
did fix my redirect. But I'd rather not have to redirect, I'd rather just have/app
currectly resolve as a 200.I then tried:
This correctly resolves everything:
/app
,/app/
, and/app/:anything
.Steps to Reproduce
I think above covers this. Let me know if you need more info.
Expected Behavior
I'd expect
/app/*
to resolve/app
,/app/
, and/app/:anything
whenignoreTrailingSlash: true
and only/app/
, and/app/:anything
whenignoreTrailingSlash: false
.The text was updated successfully, but these errors were encountered: