避免 Remix revalidation:使用原生 fetch 而非 fetcher.submit,避免触发 Remix 的数据重新验证机制,这是导致流式请求被中止的主要原因。
增加延迟时间:将项目保存延迟增加到2秒,确保流式请求完全结束后再执行保存操作,减少时间窗口内的冲突。 改进错误处理:添加了更详细的错误处理和日志记录,便于后续调试。
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
import { useFetcher } from '@remix-run/react';
|
||||
import { useEditorStorage } from '~/.client/persistence/editor';
|
||||
import { webBuilderStore } from '~/.client/stores/web-builder';
|
||||
import type { ApiResponse } from '~/types/global';
|
||||
@@ -7,7 +6,6 @@ import { createScopedLogger } from '~/utils/logger';
|
||||
const logger = createScopedLogger('useGrapesProject');
|
||||
|
||||
export function useProject() {
|
||||
const fetcher = useFetcher();
|
||||
const { saveEditorProject } = useEditorStorage();
|
||||
|
||||
/**
|
||||
@@ -62,18 +60,26 @@ export function useProject() {
|
||||
// 先保存在本地数据中
|
||||
saveEditorProject(messageId, projectPages, projectSections);
|
||||
// 再调用远程接口保存到后端数据库
|
||||
// 使用fetcher调用API保存项目数据
|
||||
fetcher.submit(
|
||||
{
|
||||
// 使用原生 fetch 而非 Remix fetcher,避免触发 revalidation 导致流式请求中断
|
||||
const response = await fetch('/api/project', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
messageId,
|
||||
pages: JSON.stringify(projectPages),
|
||||
sections: JSON.stringify(projectSections),
|
||||
},
|
||||
{
|
||||
method: 'POST',
|
||||
action: '/api/project',
|
||||
},
|
||||
);
|
||||
pages: projectPages,
|
||||
sections: projectSections,
|
||||
}),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const errorData = await response.json().catch(() => ({}));
|
||||
logger.error('保存项目失败:', errorData.message || '服务器错误');
|
||||
return false;
|
||||
}
|
||||
|
||||
logger.info('项目保存成功');
|
||||
return true;
|
||||
} catch (error) {
|
||||
logger.error('保存GrapesJS项目失败:', error);
|
||||
|
||||
Reference in New Issue
Block a user