Skip to content
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

fix: unstable_middlwares doesn't exec when render.middlewares not empty #6624

Merged
merged 2 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading