Skip to content

Commit

Permalink
fix: unstable_middlwares doesn't exec when render.middlewares not emp…
Browse files Browse the repository at this point in the history
…ty (#6624)
  • Loading branch information
zllkjc authored Dec 9, 2024
1 parent eb9a8b5 commit 30f89d5
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .changeset/strange-crabs-walk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@modern-js/server-core': patch
---

fix: unstable_middlwares doesn't exec when render.middlewares not empty
fix: 修复 unstable_middlwares 在 `render.middlewares` 不为空时不执行的问题
15 changes: 12 additions & 3 deletions packages/server/core/src/plugins/customServer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,19 +158,28 @@ export class CustomServer {
};
}

async getServerMiddleware(): Promise<
async getServerMiddleware(
renderMiddlewares?: Middleware<ServerNodeEnv & ServerEnv>[],
): Promise<
| Middleware<ServerNodeEnv & ServerEnv>
| Array<Middleware<ServerNodeEnv & ServerEnv>>
| undefined
> {
const serverMiddleware = await this.serverMiddlewarePromise;

// if no server middleware in server/index.ts, return render middleware
if (!serverMiddleware) {
return;
return renderMiddlewares;
}

// if server middleware is array, concat it with render middleware
if (Array.isArray(serverMiddleware)) {
return getServerMidFromUnstableMid(serverMiddleware);
const unstableMiddlewares = getServerMidFromUnstableMid(serverMiddleware);
return [...(renderMiddlewares || []), ...unstableMiddlewares];
} else if (renderMiddlewares) {
// if server middleware is not array, which means it is deprecated mode.
// if there has render middlewares, ignore the deprecated middlewares.
return renderMiddlewares;
}

return async (c, next) => {
Expand Down
5 changes: 4 additions & 1 deletion packages/server/core/src/plugins/render/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ export const renderPlugin = (): ServerPlugin => ({

const customServer = new CustomServer(runner, serverBase!, pwd);

// render.middleware can register by server config and prepare hook
// render.middleware is the same as unstable_middleware in server/index.ts, but execute before unstable_middleware
// TODO:check api and add docs for render.middleware
const serverMiddleware =
config.render?.middleware &&
getServerMidFromUnstableMid(config.render.middleware);
Expand Down Expand Up @@ -75,7 +78,7 @@ export const renderPlugin = (): ServerPlugin => ({
});

const customServerMiddleware =
serverMiddleware || (await customServer.getServerMiddleware());
await customServer.getServerMiddleware(serverMiddleware);

customServerMiddleware &&
middlewares.push({
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { defineConfig } from '@modern-js/app-tools/server';

export default defineConfig({
render: {
middleware: [
async (c, next) => {
c.response.headers.set('x-render-middleware-config', 'ok');

await next();
},
],
},
plugins: [
{
name: 'custom-plugin-in-config',
setup: () => {
return {
config: async config => {
if (!config.render) {
config.render = {};
}
config.render.middleware ||= [];
config.render.middleware.push(async (c, next) => {
c.response.headers.set('x-render-middleware-plugin', 'ok');
await next();
});

return config;
},
};
},
},
],
});
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ describe('test new middleware run correctly', () => {
expect(headers).toHaveProperty('x-custom-value', 'modern');

expect(headers).toHaveProperty('x-matched-route', 'main');

expect(headers).toHaveProperty('x-render-middleware-config', 'ok');

expect(headers).toHaveProperty('x-render-middleware-plugin', 'ok');
});

test('should redirect corretly', async () => {
Expand Down

0 comments on commit 30f89d5

Please sign in to comment.