luoluoluo22 commited on
Commit
78963e3
·
1 Parent(s): d425192

修复: 解决Feature-Policy警告和页面显示问题

Browse files
Files changed (3) hide show
  1. Dockerfile +7 -0
  2. index.html +80 -22
  3. server.js +46 -37
Dockerfile CHANGED
@@ -16,6 +16,13 @@ COPY . .
16
  # 构建应用
17
  RUN npm run build
18
 
 
 
 
 
 
 
 
19
  # 暴露端口
20
  EXPOSE 7860
21
 
 
16
  # 构建应用
17
  RUN npm run build
18
 
19
+ # 确保 dist 目录中有 index.html(如果构建失败)
20
+ RUN mkdir -p dist && \
21
+ if [ ! -f "dist/index.html" ]; then \
22
+ echo "使用备用 index.html"; \
23
+ cp index.html dist/ || echo "备用 index.html 不存在"; \
24
+ fi
25
+
26
  # 暴露端口
27
  EXPOSE 7860
28
 
index.html CHANGED
@@ -1,29 +1,87 @@
1
  <!DOCTYPE html>
2
- <html lang="en">
3
  <head>
4
- <meta charset="utf-8" />
5
- <link rel="icon" type="image/svg+xml" href="/logo.svg" />
6
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
7
- <title>DeepSite | Build with AI</title>
8
- <meta
9
- name="description"
10
- content="DeepSite is a web development tool that
11
- helps you build websites with AI, no code required. Let's deploy your
12
- website with DeepSite and enjoy the magic of AI."
13
- />
14
- <link rel="preconnect" href="https://fonts.googleapis.com" />
15
- <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
16
- <link
17
- href="https://fonts.googleapis.com/css2?family=Noto+Sans:ital,wght@0,100..900;1,100..900&display=swap"
18
- rel="stylesheet"
19
- />
20
- <link
21
- href="https://fonts.googleapis.com/css2?family=Noto+Sans:ital,wght@0,100..900;1,100..900&family=Source+Code+Pro:ital,wght@0,200..900;1,200..900&display=swap"
22
- rel="stylesheet"
23
- />
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  </head>
25
  <body>
26
- <div id="root"></div>
27
- <script type="module" src="/src/main.tsx"></script>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  </body>
29
  </html>
 
1
  <!DOCTYPE html>
2
+ <html lang="zh-CN">
3
  <head>
4
+ <meta charset="UTF-8" />
 
5
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
6
+ <title>DeepSite - AI驱动的前端助手</title>
7
+ <style>
8
+ html, body {
9
+ height: 100%;
10
+ margin: 0;
11
+ padding: 0;
12
+ font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
13
+ }
14
+ body {
15
+ display: flex;
16
+ flex-direction: column;
17
+ align-items: center;
18
+ justify-content: center;
19
+ background: #f8f9fa;
20
+ color: #333;
21
+ text-align: center;
22
+ padding: 20px;
23
+ }
24
+ .container {
25
+ background: white;
26
+ border-radius: 10px;
27
+ box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
28
+ padding: 40px;
29
+ max-width: 600px;
30
+ width: 100%;
31
+ }
32
+ h1 {
33
+ margin-top: 0;
34
+ color: #0066cc;
35
+ }
36
+ p {
37
+ line-height: 1.6;
38
+ margin-bottom: 20px;
39
+ }
40
+ </style>
41
  </head>
42
  <body>
43
+ <div class="container">
44
+ <h1>DeepSite</h1>
45
+ <p>一个强大的 AI 驱动的前端开发助手,帮助你快速创建和优化网页。</p>
46
+ <p>应用正在加载中,请稍候...</p>
47
+ <p id="status">正在检查服务状态...</p>
48
+
49
+ <script>
50
+ // 简单的健康检查
51
+ const checkHealth = async () => {
52
+ try {
53
+ const response = await fetch('/debug');
54
+
55
+ if (response.ok) {
56
+ const data = await response.json();
57
+ document.getElementById('status').innerHTML =
58
+ '服务器运行正常,将在3秒后刷新页面...<br>' +
59
+ '时间: ' + data.time + '<br>' +
60
+ '端口: ' + data.port;
61
+
62
+ // 3秒后刷新
63
+ setTimeout(() => {
64
+ window.location.reload();
65
+ }, 3000);
66
+ } else {
67
+ document.getElementById('status').innerHTML =
68
+ '服务器响应异常,将在5秒后重试...';
69
+
70
+ // 5秒后重试
71
+ setTimeout(checkHealth, 5000);
72
+ }
73
+ } catch (err) {
74
+ document.getElementById('status').innerHTML =
75
+ '无法连接到服务器,将在5秒后重试...';
76
+
77
+ // 5秒后重试
78
+ setTimeout(checkHealth, 5000);
79
+ }
80
+ };
81
+
82
+ // 页面加载后检查健康状态
83
+ window.addEventListener('load', checkHealth);
84
+ </script>
85
+ </div>
86
  </body>
87
  </html>
server.js CHANGED
@@ -55,9 +55,16 @@ const MAX_REQUESTS_PER_IP = 4;
55
 
56
  app.use(cookieParser());
57
  app.use(express.static(path.join(__dirname, "dist"), {
58
- index: false, // 不自动提供 index.html,我们会手动处理
59
  setHeaders: (res, filePath) => {
60
  console.log(`提供静态文件: ${filePath}`);
 
 
 
 
 
 
 
61
  }
62
  }));
63
  app.use(bodyParser.json());
@@ -622,9 +629,9 @@ if (!fs.existsSync(distDir)) {
622
  fs.mkdirSync(distDir, { recursive: true });
623
  }
624
 
625
- if (!fs.existsSync(indexPath)) {
626
- console.log("创建基本的 index.html 文件");
627
- const basicHtml = `
628
  <!DOCTYPE html>
629
  <html lang="zh-CN">
630
  <head>
@@ -633,7 +640,7 @@ if (!fs.existsSync(indexPath)) {
633
  <title>DeepSite</title>
634
  <style>
635
  body {
636
- font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
637
  display: flex;
638
  flex-direction: column;
639
  align-items: center;
@@ -651,49 +658,51 @@ if (!fs.existsSync(indexPath)) {
651
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
652
  border-radius: 8px;
653
  }
654
- h1 {
655
- color: #0066cc;
656
- margin-bottom: 20px;
657
- }
658
- p {
659
- font-size: 18px;
660
- line-height: 1.6;
661
- color: #333;
662
- }
663
- .logo {
664
- width: 100px;
665
- height: 100px;
666
- margin-bottom: 20px;
667
- }
668
- .button {
669
- display: inline-block;
670
- background-color: #0066cc;
671
- color: white;
672
- padding: 12px 24px;
673
- border-radius: 4px;
674
- text-decoration: none;
675
- font-weight: bold;
676
- margin-top: 20px;
677
- transition: background-color 0.3s;
678
- }
679
- .button:hover {
680
- background-color: #0052a3;
681
- }
682
  </style>
683
  </head>
684
  <body>
685
  <div class="container">
686
  <h1>欢迎使用 DeepSite</h1>
687
- <p>一个强大的AI驱动的前端开发助手,帮助你快速创建和优化网页。</p>
688
- <p>如果您看到此页面,说明应用已成功启动,但可能静态资源加载有问题。</p>
689
- <a href="/" class="button">刷新页面</a>
 
 
 
 
 
 
 
 
 
 
690
  </div>
691
  </body>
692
  </html>
693
  `;
694
- fs.writeFileSync(indexPath, basicHtml);
 
 
 
 
 
695
  }
696
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
697
  app.listen(PORT, () => {
698
  console.log(`===== 应用启动信息 =====`);
699
  console.log(`启动时间: ${new Date().toISOString()}`);
 
55
 
56
  app.use(cookieParser());
57
  app.use(express.static(path.join(__dirname, "dist"), {
58
+ index: false,
59
  setHeaders: (res, filePath) => {
60
  console.log(`提供静态文件: ${filePath}`);
61
+
62
+ // 移除可能导致问题的安全头部
63
+ res.removeHeader('Feature-Policy');
64
+ res.removeHeader('Permissions-Policy');
65
+
66
+ // 设置适当的内容安全策略
67
+ res.setHeader('Content-Security-Policy', "default-src 'self' https: data: 'unsafe-inline' 'unsafe-eval';");
68
  }
69
  }));
70
  app.use(bodyParser.json());
 
629
  fs.mkdirSync(distDir, { recursive: true });
630
  }
631
 
632
+ // 修改 createBasicHtml 函数,创建一个更基础的 HTML 页面
633
+ const createBasicHtml = () => {
634
+ return `
635
  <!DOCTYPE html>
636
  <html lang="zh-CN">
637
  <head>
 
640
  <title>DeepSite</title>
641
  <style>
642
  body {
643
+ font-family: system-ui, sans-serif;
644
  display: flex;
645
  flex-direction: column;
646
  align-items: center;
 
658
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
659
  border-radius: 8px;
660
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
661
  </style>
662
  </head>
663
  <body>
664
  <div class="container">
665
  <h1>欢迎使用 DeepSite</h1>
666
+ <p>一个强大的 AI 驱动的前端开发助手</p>
667
+ <p>正在加载中...</p>
668
+ <p><a href="/" id="refresh">刷新页面</a></p>
669
+ <div id="debug"></div>
670
+ <script>
671
+ // 简单的调试脚本
672
+ document.getElementById('debug').innerHTML = '浏览器信息: ' + navigator.userAgent;
673
+
674
+ // 5秒后自动刷新
675
+ setTimeout(() => {
676
+ window.location.reload();
677
+ }, 5000);
678
+ </script>
679
  </div>
680
  </body>
681
  </html>
682
  `;
683
+ };
684
+
685
+ // 在检查 index.html 不存在时使用新的函数
686
+ if (!fs.existsSync(indexPath)) {
687
+ console.log("创建基本的 index.html 文件");
688
+ fs.writeFileSync(indexPath, createBasicHtml());
689
  }
690
 
691
+ // 添加调试路由
692
+ app.get("/debug", (req, res) => {
693
+ const info = {
694
+ env: process.env.NODE_ENV,
695
+ port: PORT,
696
+ time: new Date().toISOString(),
697
+ static_dir: path.join(__dirname, "dist"),
698
+ files: fs.readdirSync(path.join(__dirname, "dist")),
699
+ headers: req.headers
700
+ };
701
+
702
+ res.setHeader('Content-Type', 'application/json');
703
+ res.send(JSON.stringify(info, null, 2));
704
+ });
705
+
706
  app.listen(PORT, () => {
707
  console.log(`===== 应用启动信息 =====`);
708
  console.log(`启动时间: ${new Date().toISOString()}`);