Windows 10上Odoo 11安装新模块后菜单加载失败问题
最近刚刚体验Odoo11,结果却遇到了小小的麻烦。
问题
当安装新的模块后,chrome浏览器下页面却加载不全,主菜单没有显示出来:
打开chrome的调试发现如下报错:
Refused to execute script from 'http://localhost:8069/web/content/347-89e7965/web.assets_common.js' because its MIME type ('text/plain') is not executable, and strict MIME type checking is enabled.
修复1
看起来问题在于http服务端返回的MIME类型不对,js文件正常应该返回Content-Type:application/javascript
, 这里却是Content-Type:text/plain
,所以chrome拒绝执行脚本。
解决方法也就出来,直接打开PGAdmin, 找到ir_attachment表,更新相关记录的mimetype字段为application/javascript
, 刷新chrome,页面成功加载。
修复2
解决问题后,随即希望反馈到Odoo社区,也发现不少用户困于此问题,并且也使用了直接更新数据库的修正方法。
但这种方法毕竟不能从根本上解决问题,而且官方跟进的人员无法重现,所以跟贴说明了问题产生的原因。
随后多次安装新模块,总是要手动更新数据库,不胜其烦,忍不住跟踪了一下相关的代码,发现根源在于python mime相关的一个库,在win10上兼容性有些问题。
所以快捷的修复方法是跳过这个库,手动在save_attachment时设定'mimetype': 'application/javascript'
。代码如下:
def save_attachment(self, type, content, inc=None):
ira = self.env['ir.attachment']
fname = '%s%s.%s' % (self.name, ('' if inc is None else '.%s' % inc), type)
values = {
'name': "/web/content/%s" % type,
'datas_fname': fname,
'res_model': 'ir.ui.view',
'res_id': False,
'type': 'binary',
'public': True,
'datas': content.encode('utf8').encode('base64'),
}
# fix js buddle mimetype
if type == 'js':
mime_vaule = {
'mimetype': 'application/javascript'
}
values.update(mime_vaule)
attachment = ira.sudo().create(values)`
参考
相关的github issue: