brillout / vite-plugin-server-entry

Handles the server production entry.
8 stars 2 forks source link

Multiple projects affect each other --- 多个项目会相互影响。 #12

Closed cz132023cz closed 2 months ago

cz132023cz commented 2 months ago

比如我有2个vike项目,A 和 B, 然后通过设置文根区分,构建后部署到同一个服务器上, 服务器是用koa2或express 其中项目A的文根是/a 预期项目A的访问地址是 demo.xxx.com/a/

其中项目B的文根是/b 预期项目B的访问地址是 demo.xxx.com/b/

问题是要引入2个“xxx/server/entry.mjs” 而且引入之后,启动服务器,每次只能访问一个项目,另一个就访问不了。

cz132023cz commented 2 months ago

Translation: For example, I have two vike projects, A and B, and then set the text root distinction, and deploy it to the same server after construction. The server is koa2 or express.

Where the text root of project A is /a, so the expected access address of project A is “demo.xxx.com/a/”

where the text root of item B is /b , so The expected access address of item B is “demo.xxx.com/b/”

the problem is to introduce 2 "xxx/server/entry.mjs" and after the introduction, the server is started, and only one project can be accessed at a time, and the other one can't.

brillout commented 2 months ago

Have you tried https://github.com/brillout/vite-plugin-server-entry#manual-import?

cz132023cz commented 2 months ago

Have you tried https://github.com/brillout/vite-plugin-server-entry#manual-import?

yes My problem is that I am using “manual-import” Later

cz132023cz commented 2 months ago

If I had two "renderPage" here (that is, two projects built), there would be a problem. I don't know how to introduce two "entry. mjs"Screenshot_20240823_212152.png

brillout commented 2 months ago

I don't think what you want can (easily) be supported. I don't know any framework that supports what you want.

(You can now disable the auto importer in case it causes issue. Although I don't think it would help for what you're trying to achieve.)

cz132023cz commented 2 months ago

I came up with a good idea, and it works very well. Is to put "import xxx.entry.mjs"、"import {renderPage} from vike/server" write to another js in a "node:worker_thread" in this way, there is no variable pollution. You can have as many "renderPage" as you want.

brillout commented 2 months ago

Thanks for circling back and that's a great idea! Would you be up for publishing an example? I'll then mention it in the documentation.

cz132023cz commented 2 months ago

// index.js import { Worker } from 'node:worker_threads';

// Run blog.js with a worker thread const blog = new Worker('./server/blog.js'); ....... router.get('/blog/:path?', async (ctx) => { const pageContextInit = { urlOriginal: ctx.originalUrl, headersOriginal: ctx.header }; const pageContext = await new Promise((resolve) => { blog.on('message', result => { resolve(JSON.parse(result)); }); blog.postMessage(JSON.stringify(pageContextInit)); }); const { body, statusCode, headers } = pageContext.httpResponse; ....

// blog.js import { parentPort } from 'node:worker_threads'; import '../www/blog/dist/server/entry.mjs'; import { renderPage } from 'vike/server';

parentPort.on('message', async pageContextInit => { const pageContext = await renderPage(JSON.parse(pageContextInit));

parentPort.postMessage(JSON.stringify(pageContext)); });

----------------------example end--------------------------------- But I think this is not a good method, because the communication between threads can not be comprehensive, for example, a function in an object can not be transferred A good idea for me is something like this:

// In the same js file import a '../www/blog/dist/server/entry.mjs'; import b '../www/word/dist/server/entry.mjs'; .... const pageContextA = await renderPage(pageContextInit,a); ... const pageContextB = await renderPage(pageContextInit,b); ....

brillout commented 2 months ago

Neat! Can you publish it as a full example in a public GitHub repository? That would be the best for others to play with it.

brillout commented 2 months ago

And, yes, I wonder if there is a simpler way without using threads.