perf(bundle): improve initial load + loading of tools (#575)

* perf(bundle): improve initial load + loading of tool

Render the sider once and only build the parts of it that are visible.

- App.vue renders BaseLayout around the layout switch instead of inside each
  layout, so moving between a plain route and a tool route no longer unmounts
  and rebuilds the whole sider (~460 menu items) on every navigation.
- CollapsibleToolMenu mounts a category's items the first time it opens. Every
  category starts collapsed, so the menu was built entirely out of view.
- Its menuOptions computed no longer folds in the collapsed/active flags, which
  rebuilt every category's tools array -- and re-rendered every item -- per route
  change. They are read from the template instead.
- MenuItemTooltip measures truncation with scrollWidth/clientWidth rather than a
  canvas measureText(), dropping a throwaway <canvas> and a getComputedStyle per
  menu item.

Production build, 3 runs each, fresh profile per run:

  first contentful paint   2476-2735ms -> 814-974ms
  main-thread blocking     1806-2024ms -> 250-294ms
  requests at boot         384         -> 208
  click a tool from home   991-1014ms  -> 178-197ms
  switch between tools     281-322ms   -> 74-125ms

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* Fix small width in mermaid exporter

---------

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-authored-by: sharevb <sharevb@gmail.com>
This commit is contained in:
authored and GitHub committed 2026-09-27 12:04:23 +02:00
1 parent 0f4c7b1d68
commit 641fa857b4
6 files changed
+81 -72

No files matched your search

+8 -3
View File
@@ -3,6 +3,7 @@ import { RouterView, useRoute } from 'vue-router';
import { NGlobalStyle, NMessageProvider, NNotificationProvider, darkTheme } from 'naive-ui';
import { get } from '@vueuse/core';
import { darkThemeOverrides, lightThemeOverrides } from './themes';
import BaseLayout from './layouts/base.layout.vue';
import { layouts } from './layouts';
import { useStyleStore } from './stores/style.store';
import { useAppTheme } from './ui/theme/themes';
@@ -52,9 +53,13 @@ syncRef(locale, useStorage('locale', locale));
<NMessageProvider placement="bottom">
<NNotificationProvider placement="bottom-right">
<div class="app-root">
<component :is="layout">
<RouterView />
</component>
<!-- Wraps the switch: inside it, this is to prevent moving between a plain and a tool route
tearing down down the sider and remounted its ~460 menu items. -->
<BaseLayout>
<component :is="layout">
<RouterView />
</component>
</BaseLayout>
</div>
</NNotificationProvider>
</NMessageProvider>
+19 -10
View File
@@ -47,6 +47,17 @@ watchEffect(() => {
const isToggling = ref(false);
const menuContainerRefs = ref<Record<string, HTMLElement>>({});
// Every category starts coMount a category's items when it first opens, then keep themllapsed, so mounting all ~460 items up front built a
// menu nobody can see. Mount a category's items when it first opens, then keep them.
const openedCategories = ref<Record<string, boolean>>({});
watchEffect(() => {
toolsByCategory.value.forEach(({ name }) => {
if (collapsedCategories.value[name] === false) {
openedCategories.value[name] = true;
}
});
});
function toggleCategoryCollapse({ name }: { name: string }) {
collapsedCategories.value[name] = !collapsedCategories.value[name];
}
@@ -88,11 +99,12 @@ function isCategoryActive(components: Tool[]): boolean {
return components.some((tool) => tool.path === route.path);
}
// Depends on the tool list only: holding the collapsed/active flags in here rebuilt
// every category's `tools` array, re-rendering all ~460 items on each navigation.
const menuOptions = computed(() =>
toolsByCategory.value.map(({ name, components }) => ({
name,
isCollapsed: collapsedCategories.value[name],
isActive: isCategoryActive(components),
components,
animationDuration: getAnimationDuration(components.length),
tools: components.map((tool) => ({
label: makeLabel(tool),
@@ -155,14 +167,10 @@ const themeVars = useThemeVars();
</c-button>
</div>
<div
v-for="{ name, tools, isCollapsed, isActive, animationDuration } of menuOptions"
:key="name"
class="category-container"
>
<div v-for="{ name, components, tools, animationDuration } of menuOptions" :key="name" class="category-container">
<button
class="category-button"
:class="{ 'category-active': isActive }"
:class="{ 'category-active': isCategoryActive(components) }"
flex
cursor-pointer
items-center
@@ -170,7 +178,7 @@ const themeVars = useThemeVars();
@click="toggleCategoryCollapse({ name })"
>
<span
:class="{ 'rotate-0': isCollapsed, 'rotate-90': !isCollapsed }"
:class="{ 'rotate-0': collapsedCategories[name], 'rotate-90': !collapsedCategories[name] }"
text-16px
lh-1
op-50
@@ -192,13 +200,14 @@ const themeVars = useThemeVars();
}
"
class="menu-container"
:class="{ collapsed: isCollapsed }"
:class="{ collapsed: collapsedCategories[name] }"
:style="{ '--animation-duration': `${animationDuration}ms` }"
>
<div class="menu-wrapper">
<div class="toggle-bar" @click="toggleCategoryCollapse({ name })" />
<n-menu
v-if="openedCategories[name]"
class="menu"
:value="route.path"
:collapsed-width="64"
+7 -16
View File
@@ -11,26 +11,17 @@ const textRef = ref<HTMLElement>();
const showTooltip = ref(false);
let resizeTimeout: ReturnType<typeof setTimeout> | null = null;
// Must settle before the pointer arrives: n-tooltip reads `disabled` on enter,
// so deciding on hover would leave the first hover with a stale value.
async function checkTruncation() {
await nextTick();
if (textRef.value) {
const text = textRef.value.textContent || '';
const computedStyle = window.getComputedStyle(textRef.value);
// Create canvas context for precise text measurement
const canvas = document.createElement('canvas');
const context = canvas.getContext('2d')!;
context.font = `${computedStyle.fontWeight} ${computedStyle.fontSize} ${computedStyle.fontFamily}`;
const el = textRef.value;
const textWidth = context.measureText(text).width;
// Calculate precise container width by accounting for padding
const containerWidth =
textRef.value.clientWidth -
Number.parseFloat(computedStyle.paddingLeft) -
Number.parseFloat(computedStyle.paddingRight);
showTooltip.value = textWidth > containerWidth;
if (el) {
// .menu-text is one nowrap ellipsised line, so overflow is exactly the condition.
// Replaces a canvas measureText() that cost a <canvas> + getComputedStyle per item.
showTooltip.value = el.scrollWidth > el.clientWidth;
}
}
+4 -2
View File
@@ -1,7 +1,9 @@
import BaseLayout from './base.layout.vue';
import PassthroughLayout from './passthrough.layout.vue';
import ToolLayout from './tool.layout.vue';
// BaseLayout is not here: App.vue renders it once around the switch, so it
// survives navigation instead of being remounted with every route.
export const layouts = {
base: BaseLayout,
base: PassthroughLayout,
toolLayout: ToolLayout,
};
+5
View File
@@ -0,0 +1,5 @@
<!-- Layout for routes wanting no chrome beyond the app shell; BaseLayout is
rendered once in App.vue so the sider is not rebuilt on every navigation. -->
<template>
<slot />
</template>
+38 -41
View File
@@ -12,7 +12,6 @@ const VueMarkdown = defineAsyncComponent(() => import('vue-markdown-render'));
import { useThemeVars } from 'naive-ui';
import { useTheme } from '../ui/c-link/c-link.theme';
import BaseLayout from './base.layout.vue';
import FavoriteButton from '@/components/FavoriteButton.vue';
import type { Tool } from '@/tools/tools.types';
@@ -83,51 +82,49 @@ const linkTheme = useTheme();
</script>
<template>
<BaseLayout>
<div class="tool-layout">
<div class="tool-header">
<div flex flex-nowrap items-center justify-between>
<n-h1>
{{ toolTitle }}
<n-tooltip placement="right" trigger="click" content-class="tool-privacy-info">
<template #trigger>
<World v-if="route.meta.externAccessDescription" class="tool-privacy-icon" />
<DeviceDesktop v-else class="tool-privacy-icon" />
</template>
<VueMarkdown
v-if="route.meta.externAccessDescription"
:source="route.meta.externAccessDescription as string"
:options="{ linkify: true }"
/>
<template v-else>
{{ $t('tools.tool.layout.text.runs-entirely-in-your-browser-no-external-requests') }}
</template>
</n-tooltip>
</n-h1>
<div class="tool-layout">
<div class="tool-header">
<div flex flex-nowrap items-center justify-between>
<n-h1>
{{ toolTitle }}
<n-tooltip placement="right" trigger="click" content-class="tool-privacy-info">
<template #trigger>
<World v-if="route.meta.externAccessDescription" class="tool-privacy-icon" />
<DeviceDesktop v-else class="tool-privacy-icon" />
</template>
<VueMarkdown
v-if="route.meta.externAccessDescription"
:source="route.meta.externAccessDescription as string"
:options="{ linkify: true }"
/>
<template v-else>
{{ $t('tools.tool.layout.text.runs-entirely-in-your-browser-no-external-requests') }}
</template>
</n-tooltip>
</n-h1>
<div>
<FavoriteButton :tool="{ name: route.meta.name, path: route.path } as Tool" />
</div>
</div>
<div class="separator" />
<div class="description">
{{ toolDescription }}
<div>
<FavoriteButton :tool="{ name: route.meta.name, path: route.path } as Tool" />
</div>
</div>
</div>
<div class="tool-content">
<Suspense>
<slot />
</Suspense>
</div>
<div class="separator" />
<div class="tool-footer">
<VueMarkdown :source="toolFooter" />
<div class="description">
{{ toolDescription }}
</div>
</div>
</BaseLayout>
</div>
<div class="tool-content">
<Suspense>
<slot />
</Suspense>
</div>
<div class="tool-footer">
<VueMarkdown :source="toolFooter" />
</div>
</template>
<style lang="less">
@@ -156,7 +153,7 @@ const linkTheme = useTheme();
gap: 16px;
overflow-x: auto;
::v-deep(& > *) {
> :deep(*) {
flex: 0 1 1200px;
min-width: 0;
}