Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[core] Fix a few performance issues in Toolpad production apps #3756

Merged
merged 2 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/toolpad-studio/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
"chalk": "5.3.0",
"chokidar": "3.6.0",
"clsx": "2.1.1",
"compression": "^1.7.4",
"concurrently": "8.2.2",
"cors": "2.8.5",
"csstype": "3.1.3",
Expand Down Expand Up @@ -149,6 +150,7 @@
},
"devDependencies": {
"@types/babel__code-frame": "7.0.6",
"@types/compression": "^1.7.5",
"@types/express": "4.17.21",
"@types/express-serve-static-core": "4.19.5",
"@types/formidable": "3.4.5",
Expand Down
3 changes: 3 additions & 0 deletions packages/toolpad-studio/src/runtime/constants.ts
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
export const PREVIEW_HEADER_HEIGHT = 52;

export const FONTS_URL =
'https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500;700&display=swap';
3 changes: 3 additions & 0 deletions packages/toolpad-studio/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import chalk from 'chalk';
import { serveRpc } from '@toolpad/utils/workerRpc';
import * as url from 'node:url';
import cors from 'cors';
import compression from 'compression';
import { asyncHandler } from '../utils/express';
import { createProdHandler } from './toolpadAppServer';
import { initProject, resolveProjectDir, type ToolpadProject } from './localMode';
Expand Down Expand Up @@ -280,6 +281,8 @@ async function startToolpadServer({ port, ...config }: ToolpadServerConfig) {
const app = express();
const httpServer = createServer(app);

app.use(compression());

app.get('/health-check', (req, res) => {
const memoryUsage = process.memoryUsage();
res.json({
Expand Down
8 changes: 4 additions & 4 deletions packages/toolpad-studio/src/server/toolpadAppBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import * as appDom from '@toolpad/studio-runtime/appDom';
import type { ComponentEntry, PagesManifest } from './localMode';
import { INITIAL_STATE_WINDOW_PROPERTY } from '../constants';
import viteVirtualPlugin, { VirtualFileContent, replaceFiles } from './viteVirtualPlugin';
import { FONTS_URL } from '../runtime/constants';

const currentDirectory = url.fileURLToPath(new URL('.', import.meta.url));

Expand All @@ -26,11 +27,10 @@ function getHtmlContent(entry: string) {
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Toolpad</title>
<link
rel="stylesheet"
href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap"
/>
<link rel="preload" href="${FONTS_URL}" as="style" onload="this.onload=null;this.rel='stylesheet'">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for not reviewing earlier!
All looks good, not sure if these would also help with font loading performance:

<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />

Copy link
Member Author

@Janpot Janpot Jul 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it would make a difference, the browser will already be loading the font asap.

<noscript><link rel="stylesheet" href="${FONTS_URL}"></noscript>
</head>
<body>
<div id="root"></div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
} from '../../../utils/geometry';
import { PageViewState, NodeInfo, SlotsState } from '../../../types';
import { useAppStateApi } from '../../AppState';
import { FONTS_URL } from '../../../runtime/constants';

// Interface to communicate between editor and canvas
export interface ToolpadBridge {
Expand Down Expand Up @@ -319,9 +320,12 @@ export default function EditorCanvasHost({
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Toolpad</title>
<link rel="preload" href="${FONTS_URL}" as="style" onload="this.onload=null;this.rel='stylesheet'">
<noscript><link rel="stylesheet" href="${FONTS_URL}"></noscript>
<link
rel="stylesheet"
href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap"
href="https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500;700&display=swap"
/>
</head>
<body className="notranslate">
Expand Down
Loading
Loading