|
|
@@ -82,7 +82,7 @@ if (!isProduction) {
|
|
|
}
|
|
|
|
|
|
// 将请求处理逻辑适配为 Hono 中间件
|
|
|
-app.use(async (c) => {
|
|
|
+app.use(async (c, next) => {
|
|
|
try {
|
|
|
// 使用 c.env 获取原生请求响应对象(关键修复)
|
|
|
const req = c.env.incoming;
|
|
|
@@ -131,6 +131,108 @@ app.use(async (c) => {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ // // 处理所有其他请求的 SSR 逻辑
|
|
|
+ // /** @type {string} */
|
|
|
+ // let template;
|
|
|
+ // /** @type {import('./src/server/index.tsx').render} */
|
|
|
+ // let render;
|
|
|
+
|
|
|
+ // if (!isProduction && vite) {
|
|
|
+ // // 开发环境:读取最新模板并转换
|
|
|
+ // template = await fs.readFile('./index.html', 'utf-8');
|
|
|
+ // template = await vite.transformIndexHtml(normalizedUrl, template);
|
|
|
+ // render = (await vite.ssrLoadModule('/src/server/index.tsx')).render;
|
|
|
+ // } else {
|
|
|
+ // // 生产环境:使用缓存的模板
|
|
|
+ // template = templateHtml;
|
|
|
+ // render = (await import('./dist/server/index.js')).render;
|
|
|
+ // }
|
|
|
+
|
|
|
+ // let didError = false;
|
|
|
+ // let abortController;
|
|
|
+
|
|
|
+ // // 创建一个可读流用于 SSR 渲染内容
|
|
|
+ // const [htmlStart, htmlEnd] = template.split(`<!--app-html-->`);
|
|
|
+ // const ssrStream = new Readable({ read: () => {} });
|
|
|
+
|
|
|
+ // // 写入 HTML 头部
|
|
|
+ // ssrStream.push(htmlStart);
|
|
|
+
|
|
|
+ // // 设置响应头和状态码
|
|
|
+ // c.header('Content-Type', 'text/html');
|
|
|
+
|
|
|
+ // // 处理渲染
|
|
|
+ // const { pipe, abort } = render(normalizedUrl, {
|
|
|
+ // onShellError() {
|
|
|
+ // didError = true;
|
|
|
+ // c.status(500);
|
|
|
+ // ssrStream.push('<h1>Something went wrong</h1>');
|
|
|
+ // ssrStream.push(null); // 结束流
|
|
|
+ // },
|
|
|
+ // onShellReady() {
|
|
|
+ // // 将渲染结果通过管道传入 ssrStream
|
|
|
+ // const transformStream = new Transform({
|
|
|
+ // transform(chunk, encoding, callback) {
|
|
|
+ // ssrStream.push(chunk, encoding);
|
|
|
+ // callback();
|
|
|
+ // }
|
|
|
+ // });
|
|
|
+
|
|
|
+ // pipe(transformStream);
|
|
|
+
|
|
|
+ // // 当 transformStream 完成时,添加 HTML 尾部
|
|
|
+ // transformStream.on('finish', () => {
|
|
|
+ // ssrStream.push(htmlEnd);
|
|
|
+ // ssrStream.push(null); // 结束流
|
|
|
+ // });
|
|
|
+ // },
|
|
|
+ // onError(error) {
|
|
|
+ // didError = true;
|
|
|
+ // console.error(error);
|
|
|
+ // },
|
|
|
+ // });
|
|
|
+
|
|
|
+ // // 设置超时中止
|
|
|
+ // abortController = new AbortController();
|
|
|
+ // const abortTimeout = setTimeout(() => {
|
|
|
+ // abort();
|
|
|
+ // abortController.abort();
|
|
|
+ // }, ABORT_DELAY);
|
|
|
+
|
|
|
+ // // 将流通过 Hono 响应返回
|
|
|
+ // return c.body(ssrStream, {
|
|
|
+ // onEnd: () => {
|
|
|
+ // clearTimeout(abortTimeout);
|
|
|
+ // }
|
|
|
+ // });
|
|
|
+ await next()
|
|
|
+ } catch (e) {
|
|
|
+ if (!isProduction && vite) {
|
|
|
+ vite.ssrFixStacktrace(e);
|
|
|
+ }
|
|
|
+ console.error(e.stack);
|
|
|
+ return c.text(e.stack, 500);
|
|
|
+ }
|
|
|
+});
|
|
|
+
|
|
|
+// 将请求处理逻辑适配为 Hono 中间件
|
|
|
+app.use(async (c) => {
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 使用 c.env 获取原生请求响应对象(关键修复)
|
|
|
+ const req = c.env.incoming;
|
|
|
+ const res = c.env.outgoing;
|
|
|
+ const url = new URL(req.url, `http://${req.headers.host}`);
|
|
|
+ const path = url.pathname;
|
|
|
+
|
|
|
+ // 检查是否匹配基础路径
|
|
|
+ if (!path.startsWith(baseUrl.pathname)) {
|
|
|
+ return c.text('Not found', 404);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 处理基础路径
|
|
|
+ const normalizedUrl = path.replace(baseUrl.pathname, '/') || '/';
|
|
|
+
|
|
|
// 处理所有其他请求的 SSR 逻辑
|
|
|
/** @type {string} */
|
|
|
let template;
|
|
|
@@ -139,13 +241,16 @@ app.use(async (c) => {
|
|
|
|
|
|
if (!isProduction && vite) {
|
|
|
// 开发环境:读取最新模板并转换
|
|
|
- template = await fs.readFile('./index.html', 'utf-8');
|
|
|
+ // template = await fs.readFile('./index.html', 'utf-8');
|
|
|
+ const module = (await vite.ssrLoadModule('/src/server/index.tsx'));
|
|
|
+ template = module.template;
|
|
|
template = await vite.transformIndexHtml(normalizedUrl, template);
|
|
|
- render = (await vite.ssrLoadModule('/src/server/index.tsx')).render;
|
|
|
+ render = module.render;
|
|
|
} else {
|
|
|
// 生产环境:使用缓存的模板
|
|
|
- template = templateHtml;
|
|
|
- render = (await import('./dist/server/index.js')).render;
|
|
|
+ const module = (await import('./dist/server/index.js'))
|
|
|
+ template = module.template;
|
|
|
+ render = module.render;
|
|
|
}
|
|
|
|
|
|
let didError = false;
|
|
|
@@ -205,7 +310,6 @@ app.use(async (c) => {
|
|
|
clearTimeout(abortTimeout);
|
|
|
}
|
|
|
});
|
|
|
-
|
|
|
} catch (e) {
|
|
|
if (!isProduction && vite) {
|
|
|
vite.ssrFixStacktrace(e);
|