fix: revert @imgly npm import → CDN + ErrorBoundary + next.config cleanup
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
'use client'
|
||||
import { Component, type ReactNode } from 'react'
|
||||
|
||||
interface Props {
|
||||
children: ReactNode
|
||||
fallback?: (error: Error, reset: () => void) => ReactNode
|
||||
}
|
||||
|
||||
interface State {
|
||||
error: Error | null
|
||||
}
|
||||
|
||||
export class ErrorBoundary extends Component<Props, State> {
|
||||
state: State = { error: null }
|
||||
|
||||
static getDerivedStateFromError(error: Error): State {
|
||||
return { error }
|
||||
}
|
||||
|
||||
reset = () => this.setState({ error: null })
|
||||
|
||||
render() {
|
||||
if (this.state.error) {
|
||||
if (this.props.fallback) return this.props.fallback(this.state.error, this.reset)
|
||||
return (
|
||||
<div className="p-6">
|
||||
<div className="bg-red-900/30 border border-red-700 rounded-xl p-6 text-center space-y-3">
|
||||
<p className="text-red-300 font-semibold">Une erreur est survenue</p>
|
||||
<p className="text-red-400 text-sm font-mono">{this.state.error.message}</p>
|
||||
<button
|
||||
onClick={this.reset}
|
||||
className="px-4 py-2 bg-slate-700 hover:bg-slate-600 text-white rounded-lg text-sm"
|
||||
>
|
||||
Réessayer
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
return this.props.children
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,14 @@
|
||||
import { removeBackground } from '@imgly/background-removal'
|
||||
const CDN_URL = 'https://cdn.jsdelivr.net/npm/@imgly/background-removal@1.7.0/dist/index.mjs'
|
||||
|
||||
// URL CDN pour les ressources (WASM + modèles ONNX) — évite de les héberger localement
|
||||
const IMGLY_CDN = 'https://cdn.jsdelivr.net/npm/@imgly/background-removal@1.7.0/dist/'
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
let _removeBg: ((blob: Blob, opts: Record<string, unknown>) => Promise<Blob>) | null = null
|
||||
|
||||
async function loadLib() {
|
||||
if (_removeBg) return _removeBg
|
||||
const mod = await import(/* webpackIgnore: true */ CDN_URL)
|
||||
_removeBg = mod.removeBackground
|
||||
return _removeBg!
|
||||
}
|
||||
|
||||
export interface ProgressEvent {
|
||||
key: string
|
||||
@@ -16,15 +23,15 @@ export async function removeBackgroundRaw(
|
||||
blob: Blob,
|
||||
onProgress?: (e: ProgressEvent) => void,
|
||||
): Promise<Blob> {
|
||||
return removeBackground(blob, {
|
||||
publicPath: IMGLY_CDN,
|
||||
const removeBg = await loadLib()
|
||||
return removeBg(blob, {
|
||||
output: { format: 'image/png', type: 'foreground', quality: 1 },
|
||||
progress: (key: string, current: number, total: number) => {
|
||||
if (onProgress && total > 0) {
|
||||
onProgress({ key, percent: Math.round((current / total) * 100) })
|
||||
}
|
||||
},
|
||||
} as Parameters<typeof removeBackground>[1])
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user