A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.
fix: Optimize external resource image blocking
shuai597f617c790a
diff --git a/ui/src/components/Avatar/index.tsx b/ui/src/components/Avatar/index.tsxindex 357beb0c..9762776e 100644--- a/ui/src/components/Avatar/index.tsx+++ b/ui/src/components/Avatar/index.tsx@@ -57,16 +57,13 @@ const Index: FC<IProps> = ({className && className.indexOf('rounded') !== -1 ? '' : 'rounded-circle';return (- <>- {/* eslint-disable jsx-a11y/no-noninteractive-element-to-interactive-role,jsx-a11y/control-has-associated-label */}- <img- src={url || DefaultAvatar}- width={size}- height={size}- className={classNames(roundedCls, className)}- alt={alt}- />- </>+ <img+ src={url || DefaultAvatar}+ width={size}+ height={size}+ className={classNames(roundedCls, className)}+ alt={alt}+ />);};diff --git a/ui/src/pages/Layout/index.tsx b/ui/src/pages/Layout/index.tsxindex 4ed908c2..e61d5afd 100644--- a/ui/src/pages/Layout/index.tsx+++ b/ui/src/pages/Layout/index.tsx@@ -79,43 +79,56 @@ const Layout: FC = () => {};}, []);- const replaceImgSrc = (img) => {+ const replaceImgSrc = () => {const storageUserExternalMode = Storage.get(EXTERNAL_CONTENT_DISPLAY_MODE);+ const images = document.querySelectorAll(+ 'img:not([data-processed])',+ ) as NodeListOf<HTMLImageElement>;- if (- storageUserExternalMode !== 'always' &&- img.src &&- !img.src.startsWith(window.location.origin)- ) {- externalToast.onShow();- img.dataset.src = img.src;- img.removeAttribute('src');- }+ images.forEach((img) => {+ // 标记为已处理,避免重复处理+ img.setAttribute('data-processed', 'true');++ if (+ img.src &&+ storageUserExternalMode !== 'always' &&+ !img.src.startsWith('/') &&+ !img.src.startsWith('data:') &&+ !img.src.startsWith('blob:') &&+ !img.src.startsWith(window.location.origin)+ ) {+ externalToast.onShow();+ img.dataset.src = img.src;+ img.removeAttribute('src');+ }+ });};useEffect(() => {// Controlling the loading of external image resourcesconst observer = new MutationObserver((mutationsList) => {+ let hasNewImages = false;mutationsList.forEach((mutation) => {if (mutation.type === 'childList') {mutation.addedNodes.forEach((node) => {- if (node.nodeName === 'IMG') {- replaceImgSrc(node);- }- if ((node as Element).querySelectorAll) {- const images = (node as Element).querySelectorAll('img');- images.forEach(replaceImgSrc);+ if (+ node.nodeName === 'IMG' ||+ (node.nodeType === 1 &&+ (node as Element).querySelectorAll('img:not([data-processed])')+ .length > 0)+ ) {+ hasNewImages = true;}});}});+ // 如果发现新图片,处理它们+ if (hasNewImages) {+ replaceImgSrc();+ }});if (externalContentDisplay !== 'always_display') {- // Process all existing images- const images = document.querySelectorAll('img');- images.forEach(replaceImgSrc);- // Process all images added laterobserver.observe(document.body, { childList: true, subtree: true });}