Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 3 additions & 10 deletions app/app.vue
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
<script setup lang="ts">
import type { NavigationItem } from 'comark-content'
import type { ContentShas } from '#shared/types'
import { navigationPath, searchInstanceKeys } from '#shared/utils/content'
import { pullNavigationPath } from '#shared/utils/pull'
import { l10nNavigationPath } from '#shared/utils/l10n'
import { searchInstanceKeys } from '#shared/utils/content'

const colorMode = useColorMode()
const route = useRoute()
Expand All @@ -30,13 +27,10 @@ watch(() => colorMode.preference, (newMode, oldMode) => {
}
})

const pull = usePullPreview()
const locale = useDocsLocale()

const { data: navigation } = await useFetch<NavigationItem[]>(() => {
if (pull.value) return pullNavigationPath(docsVersion.value, pull.value)
return locale.value ? l10nNavigationPath(locale.value, docsVersion.value) : navigationPath(docsVersion.value)
}, { key: () => `navigation:${docsVersion.value}` })
// Docs pages await their own call, which server-renders it for them.
const { data: navigation } = useDocsNavigation({ deferred: true })

const searchKeys = computed(() => searchInstanceKeys(docsVersion.value))
// Client-only: an SSR value gets baked into the page's ISR entry and would pin search to a stale commit.
Expand Down Expand Up @@ -92,7 +86,6 @@ if (import.meta.server) {

const navigationByVersion = computed(() => navigation.value ?? [])

provide('navigation', navigationByVersion)
provide('searchShas', searchShas)
</script>

Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
Expand Down
7 changes: 4 additions & 3 deletions app/composables/useBlog.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import type { BlogArticle, BlogCategory } from '#shared/types'

export const useBlog = () => {
const { data: articles, refresh } = useAsyncData<BlogArticle[]>('blog', async () => {
const { data: articles, execute } = useAsyncData<BlogArticle[]>('blog', async () => {
const items = await listByDir<BlogArticle>('/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()
}
}

Expand Down
29 changes: 29 additions & 0 deletions app/composables/useDocsNavigation.ts
Original file line number Diff line number Diff line change
@@ -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:<version>` key.
*/
export function useDocsNavigation({ deferred = false } = {}) {
const { version } = useDocsVersion()
const pull = usePullPreview()
const locale = useDocsLocale()

const navigation = useFetch<NavigationItem[]>(() => {
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
}
11 changes: 3 additions & 8 deletions app/error.vue
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<script setup lang="ts">
import type { NuxtError } from '#app'
import type { NavigationItem } from 'comark-content'
import type { ContentShas } from '#shared/types'
import { navigationPath, searchInstanceKeys } from '#shared/utils/content'
import { pullNavigationPath } from '#shared/utils/pull'
import { searchInstanceKeys } from '#shared/utils/content'

useSeoMeta({
title: 'Page not found',
Expand All @@ -15,10 +13,8 @@ defineProps<{ error: NuxtError }>()
const route = useRoute()
const { version: docsVersion } = useDocsVersion()

const pull = usePullPreview()

const { data: navigation } = await useFetch<NavigationItem[]>(computed(() =>
pull.value ? pullNavigationPath(docsVersion.value, pull.value) : navigationPath(docsVersion.value)))
// Only the client-only search palette uses it.
const { data: navigation } = useDocsNavigation({ deferred: true })

const searchKeys = computed(() => searchInstanceKeys(docsVersion.value))
// Client-only: an SSR value gets baked into the page's ISR entry and would pin search to a stale commit.
Expand All @@ -35,7 +31,6 @@ const { data: searchShas } = useAsyncData(

const navigationByVersion = computed(() => navigation.value ?? [])

provide('navigation', navigationByVersion)
provide('searchShas', searchShas)
</script>

Expand Down
17 changes: 11 additions & 6 deletions app/pages/docs/[...slug].vue
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ const markdownComponents = {
VideoAccordion
}

const navigation = inject<Ref<NavigationItem[]>>('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)

Expand Down Expand Up @@ -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()

Expand Down
69 changes: 46 additions & 23 deletions app/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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()
Expand Down Expand Up @@ -234,7 +245,10 @@ useNuxtBirthday()
<HomeLogoCarousel :companies="pageData.logos.companies" />
</div>
</UPageSection>
<UPageSection
<!-- Sections below the hero hydrate when they scroll into view: hydrating the whole page at once
is one long main-thread task, most of it in components nobody sees on load. -->
<LazyUPageSection
hydrate-on-visible
:title="pageData?.features.title"
:description="pageData?.features.description"
:ui="{
Expand Down Expand Up @@ -275,9 +289,10 @@ useNuxtBirthday()
</div>
</Motion>
</template>
</UPageSection>
</LazyUPageSection>

<UPageCTA
<LazyUPageCTA
hydrate-on-visible
:description="pageData.testimonial.quote"
variant="subtle"
class="rounded-none"
Expand All @@ -291,9 +306,10 @@ useNuxtBirthday()
size="xl"
class="justify-center"
/>
</UPageCTA>
</LazyUPageCTA>

<UPageSection
<LazyUPageSection
hydrate-on-visible
:ui="{
root: 'bg-linear-to-b border-t border-default from-muted dark:from-muted/40 to-default'
}"
Expand Down Expand Up @@ -362,9 +378,10 @@ useNuxtBirthday()
</UPageCard>
</template>
</div>
</UPageSection>
</LazyUPageSection>

<UPageSection
<LazyUPageSection
hydrate-on-visible
:title="pageData.stats.title"
:description="pageData.stats.description"
class="relative"
Expand Down Expand Up @@ -451,9 +468,10 @@ useNuxtBirthday()
</UPageCard>
</div>
</div>
</UPageSection>
</LazyUPageSection>

<UPageSection
<LazyUPageSection
hydrate-on-visible
:description="pageData.modules.description"
:links="pickLinks(pageData.modules.links)"
:ui="{
Expand All @@ -462,6 +480,7 @@ useNuxtBirthday()
description: 'text-left',
links: 'justify-start'
}"
@hydrated="modulesHydrated = true"
>
<template #title>
<LazyMarkdown :value="pageData.modules.title" unwrap="p" hydrate-never />
Expand All @@ -482,9 +501,10 @@ useNuxtBirthday()
>
<ModuleItem :module="item" :show-badge="false" :is-added="false" :show-add-button="false" class="min-h-full" />
</UCarousel>
</UPageSection>
</LazyUPageSection>

<UPageSection
<LazyUPageSection
hydrate-on-visible
:title="pageData.deploy.title"
:description="pageData.deploy.description"
:links="pickLinks(pageData.deploy.links)"
Expand All @@ -501,9 +521,10 @@ useNuxtBirthday()
class="mx-auto max-w-lg sm:w-full w-full"
loading="lazy"
/>
</UPageSection>
</LazyUPageSection>

<UPageSection
<LazyUPageSection
hydrate-on-visible
:title="pageData.contributors.title"
:description="pageData.contributors.description"
:links="pickLinks(pageData.contributors.links)"
Expand All @@ -514,10 +535,11 @@ useNuxtBirthday()
}"
>
<HomeSectionContributors />
</UPageSection>
</LazyUPageSection>

<UPageSection
<LazyUPageSection
id="showcaseData"
hydrate-on-visible
headline="Showcase"
title="Real-world Web Applications built with Nuxt"
:links="[{
Expand All @@ -540,7 +562,7 @@ useNuxtBirthday()
wheel-gestures
arrows
:autoplay="{ delay: 3000 }"
:items="(showcaseData.websites as any[]).slice(0, 10)"
:items="showcase?.websites ?? []"
:active="isMounted"
class="min-w-0"
:ui="{
Expand Down Expand Up @@ -577,9 +599,10 @@ useNuxtBirthday()
/>
</UPageCard>
</UCarousel>
</UPageSection>
</LazyUPageSection>

<UPageSection
<LazyUPageSection
hydrate-on-visible
:title="pageData.sponsors.title"
:description="pageData.sponsors.description"
:links="pickLinks(pageData.sponsors.links)"
Expand All @@ -597,6 +620,6 @@ useNuxtBirthday()
<SponsorsGrid :sponsors="sponsors" :featured="tier === 'diamond'" :columns="tier === 'diamond' ? 2 : 3" />
</div>
</div>
</UPageSection>
</LazyUPageSection>
</div>
</template>
4 changes: 3 additions & 1 deletion nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
]
},
Expand Down
26 changes: 9 additions & 17 deletions test/nuxt/docs-navigation.spec.ts
Original file line number Diff line number Diff line change
@@ -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<Ref<NavigationItem[]>>('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')
Expand Down
Loading