node.js - 在 Heroku 上使用的 Create-React-App 优化
问题描述
我正在为 Heroku 优化 React 应用程序寻求帮助。
- 我买了一个翻译 XD Design 来 React on Fiverr。
- Translator 使用 SCSS 和 Bootstrap 创建所有前端。
- 然后我在上面添加了一点 web3js,并根据自己的喜好重新设计了一些组件。
- 将我的应用部署到 Heroku。
一切都很好,但问题是应用程序的加载时间很长(性能低下)。Google Page Speed Insight 显示超过 16 秒的加载时间,这是巨大的。主要原因是缺乏文本压缩。现在,我已经安装了一些压缩方法,在构建应用程序时,我拥有所有的“br”和“gz”文件,但我认为它没有被加载到 Heroku,因为加载时间没有改变。我还在 server.js 中声明使用这些文件(遵循一些教程),但它没有影响任何东西。谁能指出一些有关如何优化它的资源?
//server.js
const express = require('express');
const compression = require('compression');
const favicon = require('express-favicon');
const path = require('path');
const expressStaticGzip = require('express-static-gzip');
const port = process.env.PORT || 8080;
const app = express();
app.use(favicon(__dirname + '/build/favicon.ico'));
// the __dirname is the current directory from where the script is running
app.use(express.static(__dirname));
app.use(express.static(path.join(__dirname, 'build')));
app.use(compression());
app.get('/ping', function (req, res) {
return res.send('pong');
});
const buildPath = path.join(__dirname, '..', 'build');
app.use(
'/',
expressStaticGzip(buildPath, {
enableBrotli: true,
orderPreference: ['br', 'gz']
})
);
// Fallback to index.html when something that doesn't exist is requested
app.get('/*', (req, res) => {
res.sendFile(path.join(__dirname, '..', 'build', 'index.html'), err => {
if (err) {
res.status(500).send(err);
}
});
});
app.listen(port);
解决方案
推荐阅读
- vba - 如何根据 2 列条件复制行并将特定数据粘贴到 VBA 中?
- javascript - Firebase 管理员 deleteUser 功能不起作用
- karate - 空手道:一个班轮 json 路径表达式不起作用
- sql - EF Core Group By 正在开发 sqlite 但不是 SQL Server
- javascript - 在 mapbox gl js 中使用 geolocate.trigger() 时控制缩放级别
- java - Selenium 和 Java 无法访问覆盖页面上的信息/按钮
- javascript - Next js - 如何在 url 中为 api 传递多个参数?
- spring-boot - 将 spring boot 从 2.0.5.RELEASE 升级到 2.3.1.RELEASE 后,Liquibase 无法正常工作
- laravel - 找不到 PHP Artisan 修补程序类“XdgBaseDir\Xdg”
- amazon-web-services - AWS MQTT 收到非预定义主题