fix: stabilize chat history prepend scroll preservation on mobile and desktop
- Mobile: defeat iOS momentum scroll when compensating history prepend (overflow toggle + short rAF watchdog); disable history virtualization and post-paint background prepends; preload Markdown renderer and use plain-text Suspense fallback to avoid first-frame geometry shifts - Desktop: stop double-compensating prepends on the virtualized list - virtua shift owns the adjustment; remove sticky-anchor heuristics that misfired as failed restores - Sync: skip no-op store writes when messages/parts are unchanged
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
import React from 'react';
|
||||
import { lazyWithChunkRecovery } from '@/lib/chunkLoadRecovery';
|
||||
import { isMobileSurfaceRuntime } from '@/lib/runtimeSurface';
|
||||
import { cn } from '@/lib/utils';
|
||||
import { loadMarkdownRendererModule } from './markdownRendererLoader';
|
||||
|
||||
// Thin lazy wrapper around the MarkdownRenderer implementation.
|
||||
// The full implementation (marked + Shiki highlighting + KaTeX + morphdom
|
||||
@@ -7,23 +10,41 @@ import { lazyWithChunkRecovery } from '@/lib/chunkLoadRecovery';
|
||||
// initial bundle lean.
|
||||
|
||||
const MarkdownRendererLazy = lazyWithChunkRecovery(() =>
|
||||
import('./MarkdownRendererImpl').then((m) => ({ default: m.MarkdownRenderer }))
|
||||
loadMarkdownRendererModule().then((m) => ({ default: m.MarkdownRenderer }))
|
||||
);
|
||||
|
||||
const SimpleMarkdownRendererLazy = lazyWithChunkRecovery(() =>
|
||||
import('./MarkdownRendererImpl').then((m) => ({ default: m.SimpleMarkdownRenderer }))
|
||||
loadMarkdownRendererModule().then((m) => ({ default: m.SimpleMarkdownRenderer }))
|
||||
);
|
||||
|
||||
const fallback = <div className="break-words w-full min-w-0" />;
|
||||
|
||||
const fallbackContentClassName = (variant: unknown): string => {
|
||||
if (variant === 'tool') return 'markdown-content markdown-tool';
|
||||
if (variant === 'reasoning') return 'markdown-content markdown-reasoning';
|
||||
return 'markdown-content leading-relaxed';
|
||||
};
|
||||
|
||||
const MobileMarkdownFallback = (props: { content?: unknown; className?: unknown; variant?: unknown }) => {
|
||||
if (!isMobileSurfaceRuntime() || typeof props.content !== 'string' || props.content.length === 0) {
|
||||
return fallback;
|
||||
}
|
||||
|
||||
return (
|
||||
<div className={cn('break-words w-full min-w-0 whitespace-pre-wrap', fallbackContentClassName(props.variant), typeof props.className === 'string' ? props.className : undefined)}>
|
||||
{props.content}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export const MarkdownRenderer: React.FC<React.ComponentPropsWithoutRef<typeof MarkdownRendererLazy>> = (props) => (
|
||||
<React.Suspense fallback={fallback}>
|
||||
<React.Suspense fallback={<MobileMarkdownFallback {...props} />}>
|
||||
<MarkdownRendererLazy {...props} />
|
||||
</React.Suspense>
|
||||
);
|
||||
|
||||
export const SimpleMarkdownRenderer: React.FC<React.ComponentPropsWithoutRef<typeof SimpleMarkdownRendererLazy>> = (props) => (
|
||||
<React.Suspense fallback={fallback}>
|
||||
<React.Suspense fallback={<MobileMarkdownFallback {...props} />}>
|
||||
<SimpleMarkdownRendererLazy {...props} />
|
||||
</React.Suspense>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user