fix: widget SDK无法加载的三个问题
1. Vite proxy缺少/widget转发 — SDK调用/widget/init等API时被SPA fallback 拦截返回HTML。添加/widget proxy并bypass HTML页面请求到/widget.html。 2. ParseWebWidgetConfig类型不匹配 — seed数据中channel_config JSON的 "continuity_via_email":""是空字符串,但Go结构体字段是bool, json.Unmarshal报UnmarshalTypeError导致整个config解析失败, findInboxByWebsiteToken跳过该inbox。容忍类型不匹配错误, 只对JSON语法错误返回error。 3. dev模式SDK bundle不可用 — /sdk/js/sdk.js在Vite dev模式下无构建产物, 被SPA fallback返回HTML。构建SDK后放入public/供dev server静态提供。
This commit is contained in:
@@ -1759,7 +1759,14 @@ func ParseWebWidgetConfig(channelConfig string) (*WebWidgetConfig, error) {
|
||||
}
|
||||
var config WebWidgetConfig
|
||||
if err := json.Unmarshal([]byte(channelConfig), &config); err != nil {
|
||||
return nil, fmt.Errorf("invalid JSON: %w", err)
|
||||
// Tolerate type mismatch errors (e.g., "" for a bool field).
|
||||
// Rails/Chatwoot seed data sometimes serializes boolean fields as
|
||||
// empty strings, which Go's strict json.Unmarshal rejects. Go still
|
||||
// populates fields that parse correctly (e.g. website_token), so we
|
||||
// only abort on genuine JSON syntax errors.
|
||||
if _, isTypeErr := err.(*json.UnmarshalTypeError); !isTypeErr {
|
||||
return nil, fmt.Errorf("invalid JSON: %w", err)
|
||||
}
|
||||
}
|
||||
return &config, nil
|
||||
}
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -122,6 +122,17 @@ export default defineConfig({
|
||||
target: process.env.VITE_API_HOST || 'http://127.0.0.1:3000',
|
||||
changeOrigin: true,
|
||||
},
|
||||
'/widget': {
|
||||
target: process.env.VITE_API_HOST || 'http://127.0.0.1:3000',
|
||||
changeOrigin: true,
|
||||
// Let Vite handle the /widget standalone HTML page (rewrite to /widget.html),
|
||||
// proxy everything else (API calls: /widget/init, /widget/conversations, etc.)
|
||||
bypass(req) {
|
||||
if (req.headers.accept?.includes('text/html')) {
|
||||
return '/widget.html';
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
build: {
|
||||
|
||||
Reference in New Issue
Block a user