RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.
Go agent-harnessagent-native-databaseagentic-aiagentic-retrievalagentic-searchaiai-agentscontext-enginecontext-engineeringcontext-managementharness-engineeringknowledge-compilationragretrieval-augmented-generationretrieval-harnesssearch-harness
fix(web): prevent DOCX preview infinite update loop (#19475)
Sourav Rajvif99dd22dfd3c
diff --git a/web/src/components/document-preview/doc-preview.test.tsx b/web/src/components/document-preview/doc-preview.test.tsxnew file mode 100644index 0000000000..8786fd2ee7--- /dev/null+++ b/web/src/components/document-preview/doc-preview.test.tsx@@ -0,0 +1,133 @@+/*+ * Copyright 2026 The InfiniFlow Authors. All Rights Reserved.+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++import request from '@/utils/request';+import { render, screen, waitFor } from '@testing-library/react';+import { DocPreviewer } from './doc-preview';++const mockImportDocxFile = jest.fn().mockResolvedValue(undefined);+let mockEditorImportDocxFile = mockImportDocxFile;++jest.mock('@/utils/request', () => ({+ __esModule: true,+ default: jest.fn(),+}));++jest.mock('@/components/ui/message', () => ({+ __esModule: true,+ default: { error: jest.fn() },+}));++jest.mock('@/components/ui/spin', () => ({+ Spin: () => <div data-testid="spin" />,+}));++jest.mock('./hooks', () => ({+ isZipLikeBlob: jest.fn().mockResolvedValue(true),+ useDocumentResizeObserver: () => ({+ containerWidth: 800,+ setContainerRef: jest.fn(),+ }),+ useDocxPreviewZoom: () => ({+ zoomScale: 100,+ minZoom: 50,+ maxZoom: 200,+ handleZoomIn: jest.fn(),+ handleZoomOut: jest.fn(),+ }),+}));++jest.mock('@extend-ai/react-docx', () => ({+ DocxEditorViewer: ({+ pageVirtualization,+ }: {+ pageVirtualization?: { enabled?: boolean };+ }) => (+ <div+ data-testid="docx-viewer"+ data-page-virtualization={JSON.stringify(pageVirtualization)}+ />+ ),+ useDocxEditor: () => ({+ importDocxFile: mockEditorImportDocxFile,+ status: 'ready',+ totalPages: 2,+ }),+ useDocxPageLayout: () => ({ layout: { pageWidthPx: 800 } }),+ parseDocx: jest.fn(),+ packageToArrayBuffer: jest.fn(),+}));++const MockRequest = jest.mocked(request);+const OriginalResizeObserver = globalThis.ResizeObserver;++beforeAll(() => {+ globalThis.ResizeObserver = jest.fn().mockImplementation(() => ({+ observe: jest.fn(),+ unobserve: jest.fn(),+ disconnect: jest.fn(),+ }));+});++afterAll(() => {+ globalThis.ResizeObserver = OriginalResizeObserver;+});++describe('DocPreviewer', () => {+ beforeEach(() => {+ mockEditorImportDocxFile = mockImportDocxFile;+ mockImportDocxFile.mockClear();+ mockImportDocxFile.mockImplementation(async () => {+ // Mimic @extend-ai/react-docx returning a new callback after import.+ mockEditorImportDocxFile = jest.fn().mockResolvedValue(undefined);+ });+ MockRequest.mockResolvedValue({+ data: new Blob([new Uint8Array([0x50, 0x4b, 0x03, 0x04])], {+ type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',+ }),+ } as never);+ });++ it('loads the document once even when importDocxFile identity changes', async () => {+ const { rerender } = render(+ <DocPreviewer url="http://example.com/document.docx" />,+ );++ await waitFor(() => {+ expect(mockImportDocxFile).toHaveBeenCalledTimes(1);+ });++ rerender(<DocPreviewer url="http://example.com/document.docx" />);++ await waitFor(() => {+ expect(screen.getByTestId('docx-viewer')).toBeInTheDocument();+ });+ expect(mockImportDocxFile).toHaveBeenCalledTimes(1);+ });++ it('disables internal DOCX page virtualization to avoid update loops', async () => {+ render(<DocPreviewer url="http://example.com/document.docx" />);++ await waitFor(() => {+ expect(screen.getByTestId('docx-viewer')).toBeInTheDocument();+ });++ expect(screen.getByTestId('docx-viewer')).toHaveAttribute(+ 'data-page-virtualization',+ JSON.stringify({ enabled: false }),+ );+ });+});diff --git a/web/src/components/document-preview/doc-preview.tsx b/web/src/components/document-preview/doc-preview.tsxindex 8176d26263..457e3f712e 100644--- a/web/src/components/document-preview/doc-preview.tsx+++ b/web/src/components/document-preview/doc-preview.tsx@@ -162,6 +162,10 @@ export const DocPreviewer: React.FC<DocPreviewerProps> = ({}) => {const editor = useDocxEditor({ initialFileName: 'document.docx' });const { importDocxFile, status, totalPages } = editor;+ // importDocxFile is recreated whenever the library's internal state changes+ // (after each import), which would re-trigger the fetch effect endlessly.+ const importDocxFileRef = useRef(importDocxFile);+ importDocxFileRef.current = importDocxFile;const { layout } = useDocxPageLayout(editor);const { containerWidth, setContainerRef } = useDocumentResizeObserver();const [loading, setLoading] = useState(false);@@ -227,7 +231,7 @@ export const DocPreviewer: React.FC<DocPreviewerProps> = ({type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',});- await importDocxFile(file);+ await importDocxFileRef.current(file);if (!cancelledRef.current) {setLoading(false);@@ -239,7 +243,7 @@ export const DocPreviewer: React.FC<DocPreviewerProps> = ({setLoading(false);}}- }, [url, importDocxFile]);+ }, [url]);useEffect(() => {fetchDocument();@@ -328,6 +332,7 @@ export const DocPreviewer: React.FC<DocPreviewerProps> = ({<DocxEditorViewereditor={editor}mode="read-only"+ pageVirtualization={{ enabled: false }}loadingState={<div className="flex items-center justify-center p-8"><Spin />