From 34947f62625a9a8196fd78c28eabdfd8ab5ff91a Mon Sep 17 00:00:00 2001 From: egg Date: Wed, 17 Dec 2025 12:30:04 +0800 Subject: [PATCH] fix: Handle UTF-8 BOM in config loading and build script MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - main.js: Strip UTF-8 BOM when reading config.json - build-client.bat: Write config.json without BOM using [System.Text.UTF8Encoding]::new($false) PowerShell's -Encoding UTF8 writes BOM by default, which can cause JSON parsing issues in Node.js. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- client/src/main.js | 7 ++++++- scripts/build-client.bat | 4 ++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/client/src/main.js b/client/src/main.js index 88c3c4e..1700bd5 100644 --- a/client/src/main.js +++ b/client/src/main.js @@ -43,7 +43,12 @@ function loadConfig() { console.log(`Checking: ${configPath} - exists: ${exists}`); try { if (exists) { - const configData = fs.readFileSync(configPath, "utf-8"); + // Use utf-8 and strip BOM if present (Windows Notepad adds BOM) + let configData = fs.readFileSync(configPath, "utf-8"); + // Remove UTF-8 BOM if present + if (configData.charCodeAt(0) === 0xFEFF) { + configData = configData.slice(1); + } appConfig = JSON.parse(configData); console.log("Config loaded from:", configPath); console.log("Config content:", JSON.stringify(appConfig, null, 2)); diff --git a/scripts/build-client.bat b/scripts/build-client.bat index 5c6d9e1..b0b1fbb 100644 --- a/scripts/build-client.bat +++ b/scripts/build-client.bat @@ -313,8 +313,8 @@ if not exist "%CONFIG_FILE%" ( goto :eof ) -REM 使用 PowerShell 更新 backend.embedded = true -powershell -Command "$config = Get-Content '%CONFIG_FILE%' -Raw | ConvertFrom-Json; if (-not $config.backend) { $config | Add-Member -NotePropertyName 'backend' -NotePropertyValue @{} }; $config.backend.embedded = $true; $config | ConvertTo-Json -Depth 10 | Set-Content '%CONFIG_FILE%' -Encoding UTF8" +REM 使用 PowerShell 更新 backend.embedded = true (使用 UTF8 without BOM) +powershell -Command "$config = Get-Content '%CONFIG_FILE%' -Raw | ConvertFrom-Json; if (-not $config.backend) { $config | Add-Member -NotePropertyName 'backend' -NotePropertyValue @{} }; $config.backend.embedded = $true; $json = $config | ConvertTo-Json -Depth 10; [System.IO.File]::WriteAllText('%CONFIG_FILE%', $json, [System.Text.UTF8Encoding]::new($false))" if errorlevel 1 ( echo %RED%[ERROR]%NC% 更新 config.json embedded 設定失敗