-
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
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
[V4] TS Global Middleware not picked up #20322
Comments
This issue has been mentioned on Strapi Community Forum. There might be relevant details there: https://forum.strapi.io/t/plugin-middleware-setup-in-typescript/38366/6 |
Hey, I'm posting here the fix I found : Firstly, there was no issue with the config/middleware.ts. The problem was in the plugin. This does not work: const redirect = async (ctx: any, next: any) => {
console.log('MyMiddleware');
await next();
};
export default redirect; server/middleware/index.ts import redirect from './redirect';
export default {
redirect,
}; server/register.ts import { Strapi } from '@strapi/strapi';
import middleware from './middlewares';
console.log(middleware.redirect) //This is undefined
export default ({ strapi }: { strapi: Strapi }) => {
strapi.server.use(middleware.redirect); //registration fails
}; middleware.redirect is However, this is functional: import { Strapi } from '@strapi/strapi';
export default ({ strapi }: { strapi: Strapi }) => {
strapi.server.use(async (ctx: any, next: any) => {
console.log('MyMiddleware');
await next();
});
}; The way I found, which I don't know why (or more exactly, I don't understand why the other way does not work): server/middlewares/redirect.ts const redirect = () => async (ctx: any, next: any) => { //Redirect is now a function that returns the middleware.
console.log('MyMiddleware');
await next();
};
export default redirect; server/middleware/index.ts import redirect from './redirect';
export default {
redirect,
}; server/register.ts import { Strapi } from '@strapi/strapi';
import middleware from './middlewares';
console.log(middleware.redirect()) //This is not undefined anymore
export default ({ strapi }: { strapi: Strapi }) => {
strapi.server.use(middleware.redirect()); //I call the function that returns my middleware and that works.
}; Can someone with dark magic experience give an explanation ? :) |
Here is something weird again: I tried to pass them to my middleware like this : server/register.ts import { Strapi } from '@strapi/strapi';
import middleware from './middlewares';
console.log(middleware.redirect()) //This is not undefined anymore
export default ({ strapi }: { strapi: Strapi }) => {
strapi.server.use(middleware.redirect(strapi)); but that does not work. server/middlewares/redirect.ts const redirect = (config, { strapi }: { strapi: Strapi }) => async (ctx: any, next: any) => { //Redirect is now a function that returns the middleware.
console.log('MyMiddleware');
await next();
};
export default redirect; So I did this, but since I'm calling my middleware function when registering it, I also added them there: server/register.ts import { Strapi } from '@strapi/strapi';
import middleware from './middlewares';
console.log(middleware.redirect()) //This is not undefined anymore
export default ({ strapi }: { strapi: Strapi }) => {
strapi.server.use(middleware.redirect(config, { strapi })); And that seem to work. But still strange. |
Bug report
Following from https://discord.com/channels/811989166782021633/1240319511857463429
Seems there is error in doc's or in the way the global middleware imported.
I try to follow steps from docs https://docs.strapi.io/dev-docs/backend-customization/middlewares to add global middleware and ran in to issue.
Required System information
Describe the bug
A clear and concise description of what the bug is.
Steps to reproduce the behavior
src/middlewares/test.ts
config/middlewares.ts
Expected behavior
It should work
Additional context
The text was updated successfully, but these errors were encountered: