Open source personal finance application
perf(app): defer swiper and echarts off the boot-critical path
Ilya Komichev49517c0c2511
diff --git a/app/app/components/stat/chart/Wrap.vue b/app/app/components/stat/chart/Wrap.vueindex be152935..a0e5dacb 100644--- a/app/app/components/stat/chart/Wrap.vue+++ b/app/app/components/stat/chart/Wrap.vue@@ -23,6 +23,8 @@ const statDate = inject(statDateKey)!const statConfig = inject(statConfigKey)!const trnsFormStore = useTrnsFormStore()+// Charts mount on the first idle frame so echarts doesn't compete with the LCP render.+const isChartMountReady = useIdleMount()const isChartShow = computed(() => statConfig.config.value.isChartShow)const chartView = computed(() => statConfig.config.value.chartView)const chartType = computed(() => resolveChartType(statConfig.config.value.chartType, statConfig.config.value.chart.mode))@@ -64,7 +66,7 @@ function onChangePeriod(period: Period) {</div><div- v-if="isPie"+ v-if="isPie && isChartMountReady"class="grid gap-4":class="{ '@sm/stat:grid-cols-2': props.pieGroups.length > 1 }">@@ -79,12 +81,15 @@ function onChangePeriod(period: Period) {</div><LazyStatChartView- v-else+ v-else-if="isChartMountReady":chartType:period="statDate.params.value.intervalsBy":series="props.series":xAxisLabels="props.xAxisLabels"@click="onClickChart"/>++ <!-- Space-reserving placeholder: same height as the chart, so the idle mount doesn't shift layout. -->+ <div v-else class="h-40 @3xl/stat:h-52" /></div></template>diff --git a/app/app/composables/useIdleMount.ts b/app/app/composables/useIdleMount.tsnew file mode 100644index 00000000..d134e0bc--- /dev/null+++ b/app/app/composables/useIdleMount.ts@@ -0,0 +1,17 @@+/**+ * Flips to `true` on the first idle frame after mount. Gating heavy below-the-fold+ * widgets (echarts) on it keeps their fetch + init off the boot-critical main thread.+ */+export function useIdleMount(): Readonly<Ref<boolean>> {+ const isIdleReady = ref(false)++ onMounted(() => {+ const schedule = window.requestIdleCallback?.bind(window)+ ?? ((cb: () => void) => window.setTimeout(cb, 200))+ schedule(() => {+ isIdleReady.value = true+ })+ })++ return readonly(isIdleReady)+}diff --git a/app/app/layouts/default.vue b/app/app/layouts/default.vueindex 84777d1c..36d36a05 100644--- a/app/app/layouts/default.vue+++ b/app/app/layouts/default.vue@@ -26,6 +26,23 @@ watch([isOnboarded, isHydrated], () => {isOnboardedHint.value = isOnboarded.value})+// Mount the trn form only after its first open so swiper stays out of the boot-critical+// chunks; an idle prefetch keeps that first open instant.+const hasTrnFormOpened = ref(false)+watch(() => trnsFormStore.ui.isShow, (show) => {+ if (show)+ hasTrnFormOpened.value = true+})++onMounted(() => {+ const idle = window.requestIdleCallback ?? ((cb: () => void) => setTimeout(cb, 2000))+ idle(() => {+ void (width.value < 767+ ? import('~/components/trnForm/Bottom.vue')+ : import('~/components/trnForm/Sidebar.vue'))+ })+})+const showShell = computed(() => bootState.value === 'ready')const layoutClasses = computed(() => cn('flex min-h-dvh flex-col transition-all duration-300 ease-in-out',@@ -111,11 +128,11 @@ defineShortcuts({</template><Teleport- v-if="categoriesStore.hasItems && walletsStore.hasItems"+ v-if="hasTrnFormOpened && categoriesStore.hasItems && walletsStore.hasItems"to="body">- <TrnFormBottom v-if="width < 767" />- <TrnFormSidebar v-if="width >= 767" />+ <LazyTrnFormBottom v-if="width < 767" />+ <LazyTrnFormSidebar v-if="width >= 767" /></Teleport></div></template>