From 4b9db5bebfa1bdb8254cb7f6aaac9ce69d7856d7 Mon Sep 17 00:00:00 2001 From: Mathieu Date: Tue, 9 Jun 2026 13:15:04 +0200 Subject: [PATCH] =?UTF-8?q?fix:=20revert=20@imgly=20npm=20import=20?= =?UTF-8?q?=E2=86=92=20CDN=20+=20ErrorBoundary=20+=20next.config=20cleanup?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 36 +++++++++++++++++++++ next.config.mjs | 5 --- src/components/common/ErrorBoundary.tsx | 42 +++++++++++++++++++++++++ src/lib/client/bgRemover.ts | 19 +++++++---- 4 files changed, 91 insertions(+), 11 deletions(-) create mode 100644 README.md create mode 100644 src/components/common/ErrorBoundary.tsx diff --git a/README.md b/README.md new file mode 100644 index 0000000..e215bc4 --- /dev/null +++ b/README.md @@ -0,0 +1,36 @@ +This is a [Next.js](https://nextjs.org) project bootstrapped with [`create-next-app`](https://nextjs.org/docs/app/api-reference/cli/create-next-app). + +## Getting Started + +First, run the development server: + +```bash +npm run dev +# or +yarn dev +# or +pnpm dev +# or +bun dev +``` + +Open [http://localhost:3000](http://localhost:3000) with your browser to see the result. + +You can start editing the page by modifying `app/page.tsx`. The page auto-updates as you edit the file. + +This project uses [`next/font`](https://nextjs.org/docs/app/building-your-application/optimizing/fonts) to automatically optimize and load [Geist](https://vercel.com/font), a new font family for Vercel. + +## Learn More + +To learn more about Next.js, take a look at the following resources: + +- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API. +- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial. + +You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js) - your feedback and contributions are welcome! + +## Deploy on Vercel + +The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js. + +Check out our [Next.js deployment documentation](https://nextjs.org/docs/app/building-your-application/deploying) for more details. diff --git a/next.config.mjs b/next.config.mjs index eadf3b7..e25a6a2 100644 --- a/next.config.mjs +++ b/next.config.mjs @@ -1,11 +1,6 @@ /** @type {import('next').NextConfig} */ const nextConfig = { output: 'standalone', - webpack: (config) => { - // Requis pour onnxruntime-web (WASM) utilisé par @imgly/background-removal - config.experiments = { ...config.experiments, asyncWebAssembly: true } - return config - }, }; export default nextConfig; diff --git a/src/components/common/ErrorBoundary.tsx b/src/components/common/ErrorBoundary.tsx new file mode 100644 index 0000000..53e9778 --- /dev/null +++ b/src/components/common/ErrorBoundary.tsx @@ -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 { + 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 ( +
+
+

Une erreur est survenue

+

{this.state.error.message}

+ +
+
+ ) + } + return this.props.children + } +} diff --git a/src/lib/client/bgRemover.ts b/src/lib/client/bgRemover.ts index 817d7b0..aee2427 100644 --- a/src/lib/client/bgRemover.ts +++ b/src/lib/client/bgRemover.ts @@ -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) => Promise) | 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 { - 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[1]) + }) } /**