diff --git a/app/app.vue b/app/app.vue index 453e7424d..f842b7366 100644 --- a/app/app.vue +++ b/app/app.vue @@ -1,9 +1,6 @@ diff --git a/app/composables/useBlog.ts b/app/composables/useBlog.ts index b8e164c3e..948bdc2d7 100644 --- a/app/composables/useBlog.ts +++ b/app/composables/useBlog.ts @@ -1,17 +1,18 @@ import type { BlogArticle, BlogCategory } from '#shared/types' export const useBlog = () => { - const { data: articles, refresh } = useAsyncData('blog', async () => { + const { data: articles, execute } = useAsyncData('blog', async () => { const items = await listByDir('/blog') return items .filter(article => article.extension !== '.yml') .sort((a, b) => String(b.date).localeCompare(String(a.date))) as BlogArticle[] - }, { default: () => [] }) + // `fetchList()` loads it: the search palette calls this on every page, and the list is over 250 KB. + }, { default: () => [], immediate: false }) async function fetchList() { if (!articles.value?.length) { - return refresh() + return execute() } } diff --git a/app/composables/useDocsNavigation.ts b/app/composables/useDocsNavigation.ts new file mode 100644 index 000000000..ee3c04fb9 --- /dev/null +++ b/app/composables/useDocsNavigation.ts @@ -0,0 +1,29 @@ +import type { NavigationItem } from 'comark-content' +import { navigationPath } from '#shared/utils/content' +import { pullNavigationPath } from '#shared/utils/pull' +import { l10nNavigationPath } from '#shared/utils/l10n' + +/** + * The navigation of the active docs version. Docs pages await it, so it is server-rendered there. + * Other pages only need it for the search palette: `deferred` keeps it out of their HTML payload + * (about 80 KB) and fetches it once the app is idle. Both share the `navigation:` key. + */ +export function useDocsNavigation({ deferred = false } = {}) { + const { version } = useDocsVersion() + const pull = usePullPreview() + const locale = useDocsLocale() + + const navigation = useFetch(() => { + if (pull.value) return pullNavigationPath(version.value, pull.value) + return locale.value ? l10nNavigationPath(locale.value, version.value) : navigationPath(version.value) + }, { key: () => `navigation:${version.value}`, server: !deferred, immediate: !deferred }) + + // Not `lazy`, which still starts the request while the page mounts. + if (deferred && import.meta.client) { + onNuxtReady(() => { + if (!navigation.data.value) navigation.execute() + }) + } + + return navigation +} diff --git a/app/error.vue b/app/error.vue index 016278abb..136382ded 100644 --- a/app/error.vue +++ b/app/error.vue @@ -1,9 +1,7 @@ diff --git a/app/pages/docs/[...slug].vue b/app/pages/docs/[...slug].vue index 301e233a2..0b606d42b 100644 --- a/app/pages/docs/[...slug].vue +++ b/app/pages/docs/[...slug].vue @@ -23,7 +23,9 @@ const markdownComponents = { VideoAccordion } -const navigation = inject>('navigation', ref([])) +// Awaited with the page below, so docs pages server-render their aside, breadcrumb and surround links. +const navigationFetch = useDocsNavigation() +const navigation = computed(() => navigationFetch.data.value ?? []) const menuDrawerOpen = ref(false) const onThisPageDrawerOpen = ref(false) @@ -81,11 +83,14 @@ const instanceKey = computed(() => { const { data: indexable } = useL10nIndexable(path) -const { data: page, status, refresh: refreshPage } = await useAsyncData(pageKey, () => { - const pagePath = path.value - const client = useContent(instanceKey.value) - return paintResponse().then(() => client.get(pagePath)) -}) +const [{ data: page, status, refresh: refreshPage }] = await Promise.all([ + useAsyncData(pageKey, () => { + const pagePath = path.value + const client = useContent(instanceKey.value) + return paintResponse().then(() => client.get(pagePath)) + }), + navigationFetch +]) const locale = useDocsLocale() diff --git a/app/pages/index.vue b/app/pages/index.vue index a7e367ab3..e42bac8ff 100644 --- a/app/pages/index.vue +++ b/app/pages/index.vue @@ -11,11 +11,20 @@ const [{ data: page }, { data: officialModules }, { data: showcase }, { data: sp useAsyncData('index', () => useContent('site').get('/')), useFetch('/api/v1/modules', { key: 'official-modules', + // Only what `ModuleItem` reads: the payload carries every module object otherwise. transform: (res: { modules: Module[], stats: Stats }) => res.modules .filter(module => module.type === 'official') - .sort((a, b) => b.stats.stars - a.stats.stars) + .sort((a, b) => b.stats!.stars - a.stats!.stars) + .map(({ name, npm, description, icon, category, type, sponsor, repo, website, health, stats }) => ({ + name, npm, description, icon, category, type, sponsor, repo, website, health, + stats: { version: stats!.version, downloads: stats!.downloads, stars: stats!.stars, publishedAt: stats!.publishedAt, createdAt: stats!.createdAt } + }) as Module) + }), + useAsyncData('showcase-home', async () => { + const doc = await useContent('site').get('/showcase') + // The carousel shows the first 10 websites. + return { websites: (doc?.data.websites as any[]).slice(0, 10).map(({ name, url }) => ({ name, url })) } }), - useAsyncData('showcase', () => useContent('site').get('/showcase')), useFetch('/api/sponsors', { key: 'sponsors-home', transform(sponsors: SponsorsByTier) { @@ -36,13 +45,15 @@ const [{ data: page }, { data: officialModules }, { data: showcase }, { data: sp ]) const pageData = computed(() => page.value?.data) -const showcaseData = computed(() => showcase.value?.data) // Merge via computed, not mutation: useFetch `data` is a shallowRef (Nuxt 4 // default), so writing module.health in place would not trigger a re-render. const { health } = useModuleHealth() +// The health map loads on the client only: merge it once the modules section hydrated, or its server +// HTML (without the health badges) would not match. +const modulesHydrated = ref(false) const officialModulesWithHealth = computed(() => - (officialModules.value || []).map(m => ({ ...m, health: health.value[m.name] ?? m.health ?? null })) + (officialModules.value || []).map(m => ({ ...m, health: (modulesHydrated.value && health.value[m.name]) || m.health || null })) ) const stats = useStats() @@ -234,7 +245,10 @@ useNuxtBirthday() - + - + - - + - - + - diff --git a/nuxt.config.ts b/nuxt.config.ts index dd0579abd..f0cccdb11 100644 --- a/nuxt.config.ts +++ b/nuxt.config.ts @@ -732,7 +732,9 @@ export default defineNuxtConfig({ }, fonts: { families: [ - { name: 'Inter', weights: [400, 500, 600, 700], global: true }, + // Latin only, like Outfit: each other subset (Cyrillic, Greek, Vietnamese…) adds 8 inline `@font-face` + // rules to every page, and text in those scripts falls back to the system font. + { name: 'Inter', weights: [400, 500, 600, 700], subsets: ['latin', 'latin-ext'], global: true }, { name: 'Outfit', weights: [400, 500, 600, 700], global: true } ] }, diff --git a/test/nuxt/docs-navigation.spec.ts b/test/nuxt/docs-navigation.spec.ts index 2f3498d4f..6ec816df8 100644 --- a/test/nuxt/docs-navigation.spec.ts +++ b/test/nuxt/docs-navigation.spec.ts @@ -1,31 +1,23 @@ -import type { NavigationItem } from 'comark-content' -import { mockComponent, mountSuspended, registerEndpoint } from '@nuxt/test-utils/runtime' +import { mountSuspended, registerEndpoint } from '@nuxt/test-utils/runtime' import { describe, expect, it, vi } from 'vitest' -import { defineComponent, h, inject, type Ref } from 'vue' +import { defineComponent, h } from 'vue' import { navigationPath } from '#shared/utils/content' -import App from '~/app.vue' // One root per version, so the rendered paths tell which tree was fetched. for (const version of ['3.x', '4.x'] as const) { registerEndpoint(navigationPath(version), () => [{ title: 'Docs', path: `/docs/${version}` }]) } -registerEndpoint('/api/content/heads', () => ({})) -// `app.vue` lazy loads the agent, whose imports can still be in flight when the environment tears down. -mockComponent('AgentPanel', { render: () => null }) -mockComponent('AgentFloatingInput', { render: () => null }) - -// Stands in for the layout, and renders the roots of the tree `app.vue` provides. -const NavigationRoots = defineComponent(() => { - const navigation = inject>('navigation')! - return () => h('p', { id: 'navigation-roots' }, navigation.value.map(item => item.path).join(',')) +// Renders the roots of the tree, as the docs page reads them. +const NavigationRoots = defineComponent(async () => { + const { data: navigation } = await useDocsNavigation() + return () => h('p', { id: 'navigation-roots' }, (navigation.value ?? []).map(item => item.path).join(',')) }) -describe('app.vue', () => { +describe('useDocsNavigation', () => { it('fetches the new version\'s navigation when the route switches version', async () => { - const wrapper = await mountSuspended(App, { - route: '/docs/4.x/getting-started/introduction', - global: { stubs: { NuxtLayout: NavigationRoots } } + const wrapper = await mountSuspended(NavigationRoots, { + route: '/docs/4.x/getting-started/introduction' }) const roots = () => wrapper.find('#navigation-roots').text() expect(roots()).toBe('/docs/4.x')