mirror of
https://github.com/sharevb/it-tools.git
synced 2026-11-22 22:02:38 +03:00
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:
6 files changed
+81
-72
No files matched your search
+8
-3
@@ -3,6 +3,7 @@ import { RouterView, useRoute } from 'vue-router';
|
|||||||
import { NGlobalStyle, NMessageProvider, NNotificationProvider, darkTheme } from 'naive-ui';
|
import { NGlobalStyle, NMessageProvider, NNotificationProvider, darkTheme } from 'naive-ui';
|
||||||
import { get } from '@vueuse/core';
|
import { get } from '@vueuse/core';
|
||||||
import { darkThemeOverrides, lightThemeOverrides } from './themes';
|
import { darkThemeOverrides, lightThemeOverrides } from './themes';
|
||||||
|
import BaseLayout from './layouts/base.layout.vue';
|
||||||
import { layouts } from './layouts';
|
import { layouts } from './layouts';
|
||||||
import { useStyleStore } from './stores/style.store';
|
import { useStyleStore } from './stores/style.store';
|
||||||
import { useAppTheme } from './ui/theme/themes';
|
import { useAppTheme } from './ui/theme/themes';
|
||||||
@@ -52,9 +53,13 @@ syncRef(locale, useStorage('locale', locale));
|
|||||||
<NMessageProvider placement="bottom">
|
<NMessageProvider placement="bottom">
|
||||||
<NNotificationProvider placement="bottom-right">
|
<NNotificationProvider placement="bottom-right">
|
||||||
<div class="app-root">
|
<div class="app-root">
|
||||||
<component :is="layout">
|
<!-- Wraps the switch: inside it, this is to prevent moving between a plain and a tool route
|
||||||
<RouterView />
|
tearing down down the sider and remounted its ~460 menu items. -->
|
||||||
</component>
|
<BaseLayout>
|
||||||
|
<component :is="layout">
|
||||||
|
<RouterView />
|
||||||
|
</component>
|
||||||
|
</BaseLayout>
|
||||||
</div>
|
</div>
|
||||||
</NNotificationProvider>
|
</NNotificationProvider>
|
||||||
</NMessageProvider>
|
</NMessageProvider>
|
||||||
|
|||||||
@@ -47,6 +47,17 @@ watchEffect(() => {
|
|||||||
const isToggling = ref(false);
|
const isToggling = ref(false);
|
||||||
const menuContainerRefs = ref<Record<string, HTMLElement>>({});
|
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 }) {
|
function toggleCategoryCollapse({ name }: { name: string }) {
|
||||||
collapsedCategories.value[name] = !collapsedCategories.value[name];
|
collapsedCategories.value[name] = !collapsedCategories.value[name];
|
||||||
}
|
}
|
||||||
@@ -88,11 +99,12 @@ function isCategoryActive(components: Tool[]): boolean {
|
|||||||
return components.some((tool) => tool.path === route.path);
|
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(() =>
|
const menuOptions = computed(() =>
|
||||||
toolsByCategory.value.map(({ name, components }) => ({
|
toolsByCategory.value.map(({ name, components }) => ({
|
||||||
name,
|
name,
|
||||||
isCollapsed: collapsedCategories.value[name],
|
components,
|
||||||
isActive: isCategoryActive(components),
|
|
||||||
animationDuration: getAnimationDuration(components.length),
|
animationDuration: getAnimationDuration(components.length),
|
||||||
tools: components.map((tool) => ({
|
tools: components.map((tool) => ({
|
||||||
label: makeLabel(tool),
|
label: makeLabel(tool),
|
||||||
@@ -155,14 +167,10 @@ const themeVars = useThemeVars();
|
|||||||
</c-button>
|
</c-button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div
|
<div v-for="{ name, components, tools, animationDuration } of menuOptions" :key="name" class="category-container">
|
||||||
v-for="{ name, tools, isCollapsed, isActive, animationDuration } of menuOptions"
|
|
||||||
:key="name"
|
|
||||||
class="category-container"
|
|
||||||
>
|
|
||||||
<button
|
<button
|
||||||
class="category-button"
|
class="category-button"
|
||||||
:class="{ 'category-active': isActive }"
|
:class="{ 'category-active': isCategoryActive(components) }"
|
||||||
flex
|
flex
|
||||||
cursor-pointer
|
cursor-pointer
|
||||||
items-center
|
items-center
|
||||||
@@ -170,7 +178,7 @@ const themeVars = useThemeVars();
|
|||||||
@click="toggleCategoryCollapse({ name })"
|
@click="toggleCategoryCollapse({ name })"
|
||||||
>
|
>
|
||||||
<span
|
<span
|
||||||
:class="{ 'rotate-0': isCollapsed, 'rotate-90': !isCollapsed }"
|
:class="{ 'rotate-0': collapsedCategories[name], 'rotate-90': !collapsedCategories[name] }"
|
||||||
text-16px
|
text-16px
|
||||||
lh-1
|
lh-1
|
||||||
op-50
|
op-50
|
||||||
@@ -192,13 +200,14 @@ const themeVars = useThemeVars();
|
|||||||
}
|
}
|
||||||
"
|
"
|
||||||
class="menu-container"
|
class="menu-container"
|
||||||
:class="{ collapsed: isCollapsed }"
|
:class="{ collapsed: collapsedCategories[name] }"
|
||||||
:style="{ '--animation-duration': `${animationDuration}ms` }"
|
:style="{ '--animation-duration': `${animationDuration}ms` }"
|
||||||
>
|
>
|
||||||
<div class="menu-wrapper">
|
<div class="menu-wrapper">
|
||||||
<div class="toggle-bar" @click="toggleCategoryCollapse({ name })" />
|
<div class="toggle-bar" @click="toggleCategoryCollapse({ name })" />
|
||||||
|
|
||||||
<n-menu
|
<n-menu
|
||||||
|
v-if="openedCategories[name]"
|
||||||
class="menu"
|
class="menu"
|
||||||
:value="route.path"
|
:value="route.path"
|
||||||
:collapsed-width="64"
|
:collapsed-width="64"
|
||||||
|
|||||||
@@ -11,26 +11,17 @@ const textRef = ref<HTMLElement>();
|
|||||||
const showTooltip = ref(false);
|
const showTooltip = ref(false);
|
||||||
let resizeTimeout: ReturnType<typeof setTimeout> | null = null;
|
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() {
|
async function checkTruncation() {
|
||||||
await nextTick();
|
await nextTick();
|
||||||
if (textRef.value) {
|
|
||||||
const text = textRef.value.textContent || '';
|
|
||||||
const computedStyle = window.getComputedStyle(textRef.value);
|
|
||||||
|
|
||||||
// Create canvas context for precise text measurement
|
const el = textRef.value;
|
||||||
const canvas = document.createElement('canvas');
|
|
||||||
const context = canvas.getContext('2d')!;
|
|
||||||
context.font = `${computedStyle.fontWeight} ${computedStyle.fontSize} ${computedStyle.fontFamily}`;
|
|
||||||
|
|
||||||
const textWidth = context.measureText(text).width;
|
if (el) {
|
||||||
|
// .menu-text is one nowrap ellipsised line, so overflow is exactly the condition.
|
||||||
// Calculate precise container width by accounting for padding
|
// Replaces a canvas measureText() that cost a <canvas> + getComputedStyle per item.
|
||||||
const containerWidth =
|
showTooltip.value = el.scrollWidth > el.clientWidth;
|
||||||
textRef.value.clientWidth -
|
|
||||||
Number.parseFloat(computedStyle.paddingLeft) -
|
|
||||||
Number.parseFloat(computedStyle.paddingRight);
|
|
||||||
|
|
||||||
showTooltip.value = textWidth > containerWidth;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
import BaseLayout from './base.layout.vue';
|
import PassthroughLayout from './passthrough.layout.vue';
|
||||||
import ToolLayout from './tool.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 = {
|
export const layouts = {
|
||||||
base: BaseLayout,
|
base: PassthroughLayout,
|
||||||
toolLayout: ToolLayout,
|
toolLayout: ToolLayout,
|
||||||
};
|
};
|
||||||
@@ -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
@@ -12,7 +12,6 @@ const VueMarkdown = defineAsyncComponent(() => import('vue-markdown-render'));
|
|||||||
|
|
||||||
import { useThemeVars } from 'naive-ui';
|
import { useThemeVars } from 'naive-ui';
|
||||||
import { useTheme } from '../ui/c-link/c-link.theme';
|
import { useTheme } from '../ui/c-link/c-link.theme';
|
||||||
import BaseLayout from './base.layout.vue';
|
|
||||||
import FavoriteButton from '@/components/FavoriteButton.vue';
|
import FavoriteButton from '@/components/FavoriteButton.vue';
|
||||||
import type { Tool } from '@/tools/tools.types';
|
import type { Tool } from '@/tools/tools.types';
|
||||||
|
|
||||||
@@ -83,51 +82,49 @@ const linkTheme = useTheme();
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<BaseLayout>
|
<div class="tool-layout">
|
||||||
<div class="tool-layout">
|
<div class="tool-header">
|
||||||
<div class="tool-header">
|
<div flex flex-nowrap items-center justify-between>
|
||||||
<div flex flex-nowrap items-center justify-between>
|
<n-h1>
|
||||||
<n-h1>
|
{{ toolTitle }}
|
||||||
{{ toolTitle }}
|
<n-tooltip placement="right" trigger="click" content-class="tool-privacy-info">
|
||||||
<n-tooltip placement="right" trigger="click" content-class="tool-privacy-info">
|
<template #trigger>
|
||||||
<template #trigger>
|
<World v-if="route.meta.externAccessDescription" class="tool-privacy-icon" />
|
||||||
<World v-if="route.meta.externAccessDescription" class="tool-privacy-icon" />
|
<DeviceDesktop v-else class="tool-privacy-icon" />
|
||||||
<DeviceDesktop v-else class="tool-privacy-icon" />
|
</template>
|
||||||
</template>
|
<VueMarkdown
|
||||||
<VueMarkdown
|
v-if="route.meta.externAccessDescription"
|
||||||
v-if="route.meta.externAccessDescription"
|
:source="route.meta.externAccessDescription as string"
|
||||||
:source="route.meta.externAccessDescription as string"
|
:options="{ linkify: true }"
|
||||||
:options="{ linkify: true }"
|
/>
|
||||||
/>
|
<template v-else>
|
||||||
<template v-else>
|
{{ $t('tools.tool.layout.text.runs-entirely-in-your-browser-no-external-requests') }}
|
||||||
{{ $t('tools.tool.layout.text.runs-entirely-in-your-browser-no-external-requests') }}
|
</template>
|
||||||
</template>
|
</n-tooltip>
|
||||||
</n-tooltip>
|
</n-h1>
|
||||||
</n-h1>
|
|
||||||
|
|
||||||
<div>
|
<div>
|
||||||
<FavoriteButton :tool="{ name: route.meta.name, path: route.path } as Tool" />
|
<FavoriteButton :tool="{ name: route.meta.name, path: route.path } as Tool" />
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="separator" />
|
|
||||||
|
|
||||||
<div class="description">
|
|
||||||
{{ toolDescription }}
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="tool-content">
|
<div class="separator" />
|
||||||
<Suspense>
|
|
||||||
<slot />
|
|
||||||
</Suspense>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="tool-footer">
|
<div class="description">
|
||||||
<VueMarkdown :source="toolFooter" />
|
{{ toolDescription }}
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</BaseLayout>
|
</div>
|
||||||
|
|
||||||
|
<div class="tool-content">
|
||||||
|
<Suspense>
|
||||||
|
<slot />
|
||||||
|
</Suspense>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="tool-footer">
|
||||||
|
<VueMarkdown :source="toolFooter" />
|
||||||
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<style lang="less">
|
<style lang="less">
|
||||||
@@ -156,7 +153,7 @@ const linkTheme = useTheme();
|
|||||||
gap: 16px;
|
gap: 16px;
|
||||||
overflow-x: auto;
|
overflow-x: auto;
|
||||||
|
|
||||||
::v-deep(& > *) {
|
> :deep(*) {
|
||||||
flex: 0 1 1200px;
|
flex: 0 1 1200px;
|
||||||
min-width: 0;
|
min-width: 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user