fix(webui): show route-aligned skeletons from first paint (#6303)
This commit is contained in:
1 parent
af68da3280
commit
5d886fbb20
43 files changed
+1592
-213
No files matched your search
+26
-1
@@ -3,8 +3,33 @@ import react from "@vitejs/plugin-react";
|
||||
import { readFileSync, writeFileSync } from "node:fs";
|
||||
import path from "node:path";
|
||||
import { gzipSync } from "node:zlib";
|
||||
import { createElement } from "react";
|
||||
import { renderToStaticMarkup } from "react-dom/server";
|
||||
import { readStartupLayout, StartupSkeleton } from "./src/components/StartupSkeleton";
|
||||
import { componentGallery } from "./src/dev/gallery/inventory-plugin";
|
||||
|
||||
function startupSkeleton(): Plugin {
|
||||
return {
|
||||
name: "nanobot-startup-skeleton",
|
||||
transformIndexHtml: {
|
||||
order: "post",
|
||||
handler(html) {
|
||||
if (!html.includes("<!--startup-shell-->")) return html;
|
||||
const css = readFileSync(path.resolve(__dirname, "src/components/startup-skeleton.css"), "utf8");
|
||||
const initialize = `var shell = document.querySelector('.startup-shell'); var layout = (${readStartupLayout.toString()})();
|
||||
shell.dataset.startupSurface = layout.surface; shell.dataset.startupSection = layout.section;
|
||||
shell.dataset.sidebarExpanded = String(layout.sidebarExpanded); shell.dataset.existingChat = String(layout.existingChat);
|
||||
shell.dataset.calendarWeeks = String(layout.calendarWeeks);
|
||||
shell.style.setProperty('--startup-sidebar-width', (layout.sidebarExpanded ? layout.sidebarWidth : 56) + 'px');`;
|
||||
return html.replace("<!--startup-style-->", `<style>${css}</style>`)
|
||||
.replace("<!--startup-shell-->", renderToStaticMarkup(createElement(StartupSkeleton)))
|
||||
.replace("<!--startup-script-->", `<script>${initialize}</script>`)
|
||||
.replace(/<link rel="stylesheet"/g, '<link rel="preload" as="style" data-app-stylesheet');
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
const GZIP_MIN_BYTES = 4 * 1024;
|
||||
const GZIP_ASSET_PATTERN = /\.(?:css|html|js|json|mjs|svg)$/i;
|
||||
const LAZY_FEATURE_CHUNK_PREFIXES = ["markdown-vendor-", "syntax-highlight-", "katex-"];
|
||||
@@ -120,7 +145,7 @@ export default defineConfig(({ mode }) => {
|
||||
const hmrPath = "/__nanobot_vite_hmr";
|
||||
|
||||
return {
|
||||
plugins: [react(), componentGallery(path.resolve(__dirname, "..")), guardWebuiEntryChunk(), gzipWebuiAssets()],
|
||||
plugins: [react(), startupSkeleton(), componentGallery(path.resolve(__dirname, "..")), guardWebuiEntryChunk(), gzipWebuiAssets()],
|
||||
resolve: {
|
||||
alias: {
|
||||
"@": path.resolve(__dirname, "./src"),
|
||||
|
||||
Reference in new issue
Block a user