dropping console on production

This commit is contained in:
2025-10-28 19:20:19 +01:00
parent 5f8a6bef31
commit b3b3250f23
2 changed files with 76 additions and 39 deletions

View File

@@ -93,7 +93,7 @@ if (frontendBuildPath) {
} }
// Root route // Root route
app.get('/', (req, res) => { app.get('/', apiLimiter, (req, res) => {
if (!frontendBuildPath) { if (!frontendBuildPath) {
return res.status(500).send('Frontend build not found'); return res.status(500).send('Frontend build not found');
} }
@@ -110,7 +110,7 @@ app.get('/', (req, res) => {
}); });
// Client-side routing fallback // Client-side routing fallback
app.get('*', (req, res) => { app.get('*', apiLimiter, (req, res) => {
// Ignoriere API Routes // Ignoriere API Routes
if (req.path.startsWith('/api/')) { if (req.path.startsWith('/api/')) {
return res.status(404).json({ error: 'API endpoint not found' }); return res.status(404).json({ error: 'API endpoint not found' });
@@ -137,12 +137,34 @@ app.get('*', (req, res) => {
} }
}); });
// Production error handling - don't leak stack traces
app.use((err: any, req: express.Request, res: express.Response, next: express.NextFunction) => {
console.error('Error:', err);
if (process.env.NODE_ENV === 'production') {
res.status(500).json({
error: 'Internal server error',
message: 'Something went wrong'
});
} else {
res.status(500).json({
error: 'Internal server error',
message: err.message,
stack: err.stack
});
}
});
// Error handling middleware // Error handling middleware
app.use((err: any, req: express.Request, res: express.Response, next: express.NextFunction) => { app.use((err: any, req: express.Request, res: express.Response, next: express.NextFunction) => {
console.error('Unhandled error:', err); console.error('Unhandled error:', err);
res.status(500).json({ error: 'Internal server error' }); res.status(500).json({ error: 'Internal server error' });
}); });
app.use('*', (req, res) => {
res.status(404).json({ error: 'Endpoint not found' });
});
// Initialize the application // Initialize the application
const initializeApp = async () => { const initializeApp = async () => {
try { try {

View File

@@ -1,14 +1,26 @@
import { defineConfig } from 'vite' import { defineConfig, loadEnv } from 'vite'
import react from '@vitejs/plugin-react' import react from '@vitejs/plugin-react'
import { resolve } from 'path' import { resolve } from 'path'
// https://vitejs.dev/config/ // https://vitejs.dev/config/
export default defineConfig({ export default defineConfig(({ mode }) => {
// Load env file based on `mode` in the current directory
const env = loadEnv(mode, process.cwd(), '')
// Only expose specific environment variables to the client
const clientEnv = {
NODE_ENV: mode,
ENABLE_PRO: env.ENABLE_PRO || 'false',
VITE_APP_TITLE: env.VITE_APP_TITLE || 'Shift Planning App',
// Add other client-safe variables here
}
return {
plugins: [react()], plugins: [react()],
server: { server: {
port: 3003, port: 3003,
host: true, host: true,
open: true, open: mode === 'development',
proxy: { proxy: {
'/api': { '/api': {
target: 'http://localhost:3002', target: 'http://localhost:3002',
@@ -19,12 +31,13 @@ export default defineConfig({
}, },
build: { build: {
outDir: 'dist', outDir: 'dist',
sourcemap: true, sourcemap: mode === 'development',
rollupOptions: { minify: mode === 'production' ? 'terser' : false,
input: { terserOptions: mode === 'production' ? {
main: resolve(__dirname, 'index.html') compress: {
} drop_console: true,
} },
} : undefined,
}, },
resolve: { resolve: {
alias: { alias: {
@@ -38,8 +51,10 @@ export default defineConfig({
'@/design': resolve(__dirname, './src/design') '@/design': resolve(__dirname, './src/design')
} }
}, },
// Define environment variables // ✅ SICHER: Nur explizit definierte Variablen
define: { define: Object.keys(clientEnv).reduce((acc, key) => {
'process.env': process.env acc[`process.env.${key}`] = JSON.stringify(clientEnv[key])
return acc
}, {} as Record<string, string>)
} }
}) })