diff --git a/web-interface/server.js b/web-interface/server.js index f5b27f4..21ffa26 100644 --- a/web-interface/server.js +++ b/web-interface/server.js @@ -3,6 +3,7 @@ const path = require('path'); const cors = require('cors'); const axios = require('axios'); const { exec, spawn } = require('child_process'); +const fs = require('fs'); const app = express(); const PORT = process.env.PORT || 3000; @@ -33,6 +34,62 @@ app.use(express.static(path.join(__dirname, 'public'), { } })); +// Middleware para injetar fix de roteador em sites clonados (SPAs como React, Vue, Angular, Vite) +app.use((req, res, next) => { + if (!req.path.startsWith('/api') && req.path !== '/' && req.path !== '/old') { + let urlPath = req.path; + if (!urlPath.match(/\.[a-zA-Z0-9]+$/)) { + if (!urlPath.endsWith('/')) urlPath += '/'; + urlPath += 'index.html'; + } + + if (urlPath.endsWith('.html')) { + const filePath = path.join(__dirname, '..', 'cloned-sites', urlPath); + if (fs.existsSync(filePath)) { + try { + let html = fs.readFileSync(filePath, 'utf8'); + if (!html.includes('__CLONEWEB_ROUTER_FIX__')) { + const injectScript = ` + + `; + if (html.includes('')) { + html = html.replace('', '' + injectScript); + } else { + html = injectScript + html; + } + } + res.setHeader('Content-Type', 'text/html'); + return res.send(html); + } catch (err) { + console.error('Erro ao injetar fix no HTML do clone:', err); + } + } + } + } + next(); +}); + // Serve cloned sites directory app.use(express.static(path.join(__dirname, '..', 'cloned-sites'), { setHeaders: (res, filePath) => {