feat: dynamically load heic2any to accelerate initial page loading speed

This commit is contained in:
Shirasawa 2025-09-05 20:07:12 +08:00
parent c0a47169fa
commit a74ec200b3
3 changed files with 16 additions and 6 deletions

View file

@ -5,7 +5,6 @@
import DOMPurify from 'dompurify'; import DOMPurify from 'dompurify';
import { marked } from 'marked'; import { marked } from 'marked';
import heic2any from 'heic2any';
import { toast } from 'svelte-sonner'; import { toast } from 'svelte-sonner';
@ -32,7 +31,7 @@
} from '$lib/stores'; } from '$lib/stores';
import { import {
blobToFile, convertHeicToJpeg,
compressImage, compressImage,
createMessagesList, createMessagesList,
extractContentFromFile, extractContentFromFile,
@ -765,7 +764,7 @@
}; };
reader.readAsDataURL( reader.readAsDataURL(
file['type'] === 'image/heic' file['type'] === 'image/heic'
? await heic2any({ blob: file, toType: 'image/jpeg' }) ? await convertHeicToJpeg(file)
: file : file
); );
} else { } else {

View file

@ -1,7 +1,6 @@
<script lang="ts"> <script lang="ts">
import { getContext, onDestroy, onMount, tick } from 'svelte'; import { getContext, onDestroy, onMount, tick } from 'svelte';
import { v4 as uuidv4 } from 'uuid'; import { v4 as uuidv4 } from 'uuid';
import heic2any from 'heic2any';
import fileSaver from 'file-saver'; import fileSaver from 'file-saver';
const { saveAs } = fileSaver; const { saveAs } = fileSaver;
@ -26,7 +25,7 @@
import { PaneGroup, Pane, PaneResizer } from 'paneforge'; import { PaneGroup, Pane, PaneResizer } from 'paneforge';
import { compressImage, copyToClipboard, splitStream } from '$lib/utils'; import { compressImage, copyToClipboard, splitStream, convertHeicToJpeg } from '$lib/utils';
import { WEBUI_API_BASE_URL, WEBUI_BASE_URL } from '$lib/constants'; import { WEBUI_API_BASE_URL, WEBUI_BASE_URL } from '$lib/constants';
import { uploadFile } from '$lib/apis/files'; import { uploadFile } from '$lib/apis/files';
import { chatCompletion, generateOpenAIChatCompletion } from '$lib/apis/openai'; import { chatCompletion, generateOpenAIChatCompletion } from '$lib/apis/openai';
@ -547,7 +546,7 @@ ${content}
reader.readAsDataURL( reader.readAsDataURL(
file['type'] === 'image/heic' file['type'] === 'image/heic'
? await heic2any({ blob: file, toType: 'image/jpeg' }) ? await convertHeicToJpeg(file)
: file : file
); );
}); });

View file

@ -1528,3 +1528,15 @@ export const getAge = (birthDate) => {
} }
return age.toString(); return age.toString();
}; };
export const convertHeicToJpeg = async (file: File) => {
const { default: heic2any } = await import('heic2any');
try {
return await heic2any({ blob: file, toType: 'image/jpeg' });
} catch (err: any) {
if (err?.message?.includes('already browser readable')) {
return file;
}
throw err;
}
};