fix: Make trianglify/canvas optional and handle gracefully

Canvas requires Python and native build tools which fail in Docker builds
with --ignore-scripts. Since trianglify is only used for cosmetic backgrounds
on the login and layout pages, we can make it optional.

Changes:
- Added try-catch around trianglify usage in Login and Layout
- Using --ignore-scripts in frontend Dockerfile to skip canvas native build
- Gracefully fail when trianglify is not available (background won't render)

This allows the frontend to build and run even without canvas/trianglify.
This commit is contained in:
Muhammad Ibrahim
2025-10-28 17:35:15 +00:00
parent cc46940b0c
commit 9050595b7c
3 changed files with 43 additions and 32 deletions

View File

@@ -23,8 +23,9 @@ COPY frontend/package*.json ./
RUN npm cache clean --force &&\ RUN npm cache clean --force &&\
rm -rf node_modules ~/.npm /root/.npm &&\ rm -rf node_modules ~/.npm /root/.npm &&\
npm install --legacy-peer-deps --no-audit --prefer-online --fetch-retries=3 --fetch-retry-mintimeout=20000 --fetch-retry-maxtimeout=120000 &&\ npm install --legacy-peer-deps --no-audit --prefer-online --fetch-retries=3 --fetch-retry-mintimeout=20000 --fetch-retry-maxtimeout=120000 --ignore-scripts &&\
(npm rebuild canvas || echo "Canvas rebuild failed, continuing without native bindings") echo "package.json" > node_modules/.gitignore &&\
echo "Express cors and trianglify are not needed in production build" || true
COPY frontend/ ./ COPY frontend/ ./

View File

@@ -245,23 +245,28 @@ const Layout = ({ children }) => {
themeConfig?.login && themeConfig?.login &&
document.documentElement.classList.contains("dark") document.documentElement.classList.contains("dark")
) { ) {
// Get current date as seed for daily variation try {
const today = new Date(); // Get current date as seed for daily variation
const dateSeed = `${today.getFullYear()}-${today.getMonth()}-${today.getDate()}`; const today = new Date();
const dateSeed = `${today.getFullYear()}-${today.getMonth()}-${today.getDate()}`;
// Generate pattern with selected theme configuration // Generate pattern with selected theme configuration
const pattern = trianglify({ const pattern = trianglify({
width: window.innerWidth, width: window.innerWidth,
height: window.innerHeight, height: window.innerHeight,
cellSize: themeConfig.login.cellSize, cellSize: themeConfig.login.cellSize,
variance: themeConfig.login.variance, variance: themeConfig.login.variance,
seed: dateSeed, seed: dateSeed,
xColors: themeConfig.login.xColors, xColors: themeConfig.login.xColors,
yColors: themeConfig.login.yColors, yColors: themeConfig.login.yColors,
}); });
// Render to canvas // Render to canvas
pattern.toCanvas(bgCanvasRef.current); pattern.toCanvas(bgCanvasRef.current);
} catch (error) {
// Silently fail if trianglify/canvas is not available
console.debug("Trianglify background not available:", error.message);
}
} }
}; };

View File

@@ -61,23 +61,28 @@ const Login = () => {
useEffect(() => { useEffect(() => {
const generateBackground = () => { const generateBackground = () => {
if (canvasRef.current && themeConfig?.login) { if (canvasRef.current && themeConfig?.login) {
// Get current date as seed for daily variation try {
const today = new Date(); // Get current date as seed for daily variation
const dateSeed = `${today.getFullYear()}-${today.getMonth()}-${today.getDate()}`; const today = new Date();
const dateSeed = `${today.getFullYear()}-${today.getMonth()}-${today.getDate()}`;
// Generate pattern with selected theme configuration // Generate pattern with selected theme configuration
const pattern = trianglify({ const pattern = trianglify({
width: canvasRef.current.offsetWidth, width: canvasRef.current.offsetWidth,
height: canvasRef.current.offsetHeight, height: canvasRef.current.offsetHeight,
cellSize: themeConfig.login.cellSize, cellSize: themeConfig.login.cellSize,
variance: themeConfig.login.variance, variance: themeConfig.login.variance,
seed: dateSeed, seed: dateSeed,
xColors: themeConfig.login.xColors, xColors: themeConfig.login.xColors,
yColors: themeConfig.login.yColors, yColors: themeConfig.login.yColors,
}); });
// Render to canvas // Render to canvas
pattern.toCanvas(canvasRef.current); pattern.toCanvas(canvasRef.current);
} catch (error) {
// Silently fail if trianglify/canvas is not available
console.debug("Trianglify background not available:", error.message);
}
} }
}; };