我正在使用 express 提供一个反应应用程序构建。
const root = path.join(__dirname, 'build/');
app.use(express.static(root));
app.use((req, res, next) => {
if (req.method === 'GET' && req.accepts('html') && !req.is('json') && !req.path.includes('.')) {
res.sendFile('index.html', { root });
} else next();
});
一切都按预期工作。但是,一旦我将helmet
(不是 react-helmet)添加到 express 应用程序,我就会遇到问题(样式和脚本未加载)。在搜索了几个资源之后,我能够想出一个解决方案来使它工作。下面的代码显示了我为加载样式和脚本所做的修复。
app.use(helmet());
app.use(helmet.contentSecurityPolicy({
defaultSrc: [
'\'self\'',
'https://api.domain.tld/*',
'https://domain.tld',
],
styleSrc: [
'\'self\'',
'\'unsafe-inline\'',
'https://*.googleapis.com',
'https://api.domain.tld/*',
'https://domain.tld',
],
scriptSrc: [
'\'self\'',
'\'unsafe-inline\'',
'https://api.domain.tld/*',
'https://domain.tld',
],
contentSrc: [
'\'self\'',
'\'unsafe-inline\'',
'https://api.domain.tld/*',
'https://domain.tld',
],
}));
此外,我还包括INLINE_RUNTIME_CHUNK=false
在.env file
.
我目前遇到的问题是我正在进行的 API 调用api.domain.tld
不起作用。它被阻止,并且在 Firefox 上显示以下错误。
内容安全策略:页面设置阻止在https://api.domain.tld/endpoint(“default-src”)加载资源。
Chrome 显示以下错误。
拒绝连接到“https://api.domain.tld/endpoint”,因为它违反了以下内容安全策略指令:“default-src 'self'”。请注意,'connect-src' 没有明确设置,因此 'default-src' 用作后备。
- 请注意,react 应用程序已打开
domain.tld
且 API 已打开api.domain.tld
如果解决此问题,我该如何进行 API 调用?