Skip to content

Commit

Permalink
fix: currentNav updated after page change
Browse files Browse the repository at this point in the history
Also disabled smart prefetch for lazy components in the meantime
  • Loading branch information
atinux committed Jul 8, 2021
1 parent 58adb6c commit e08b17b
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 34 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@
"clean:dist": "rm -rf dist docs/dist playground/dist nuxtjs.org/dist",
"clean": "yarn clean:nuxt & yarn clean:node_modules & yarn clean:dist"
},
"resolutions": {
"@nuxt/components": "npm:@pi0/components"
},
"dependencies": {
"@docsearch/css": "^1.0.0-alpha.28",
"@docsearch/js": "^1.0.0-alpha.28",
Expand Down Expand Up @@ -144,8 +147,5 @@
},
"peerDependencies": {
"vuex": "^3.6.2"
},
"resolutions": {
"@nuxt/components": "npm:@pi0/components"
}
}
2 changes: 2 additions & 0 deletions src/app/pages/_.vue
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ export default defineComponent({
// Set Docus runtime current page
this.$docus.currentPage.value = this.page
// Update navigation path to update currentNav
this.$docus.currentPath.value = `/${this.$route.params.pathMatch}`
},
mounted() {
if (this.page?.version) localStorage.setItem(`page-${this.page.slug}-version`, this.page.version)
Expand Down
7 changes: 4 additions & 3 deletions src/core/runtime/components/DocusContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,10 @@ function processNode(node, h, doc) {
children.push(...processQueue.map(node => processNode(node, h, doc)))
}

if (process.server && typeof Vue.component(pascalCase(node.tag)) === 'function') {
lazyComponents.add(pascalCase(node.tag))
}
// Disable in the meantime
// if (process.server && typeof Vue.component(pascalCase(node.tag)) === 'function') {
// lazyComponents.add(pascalCase(node.tag))
// }
return h(node.tag, data, children)
}

Expand Down
5 changes: 3 additions & 2 deletions src/core/runtime/composables/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export const clientAsyncData = (_app, $nuxt: any) => {
const loadComponents = function (components?: Set<string>) {
if (!components) return
return Array.from(components).map(async function (name) {
if (!loadedComponents.has(name) && typeof Vue.component(name) === 'function') {
const component: any = Vue.component(name)
if (!loadedComponents.has(name) && typeof component === 'function' && !component.options) {
loadedComponents.add(name)
try {
// @ts-ignore
Expand All @@ -32,7 +33,7 @@ export const clientAsyncData = (_app, $nuxt: any) => {
$nuxt.fetchPayload = async function (...args) {
const payload = await originalFetchPayload(...args)

await loadComponents(payload.fetch?._lazyComponents)
// await loadComponents(payload.fetch?._lazyComponents)
await loadComponents(new Set(payload.data[0]?.page?.template))

return payload
Expand Down
12 changes: 4 additions & 8 deletions src/core/runtime/composables/navigation.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import { withTrailingSlash } from 'ufo'
import { ref, computed } from '@nuxtjs/composition-api'
import { DocusAddonContext, DocusNavigationGetParameters, NavItem } from '../../../types'
import { useReactivePath } from './reactivePath'
import { useDocusTemplates } from './templates'

/**
* Handling all the navigation querying logic.
*/
export const useDocusNavigation = ({ context, state, api }: DocusAddonContext) => {
// Nuxt context
const { app, route } = context

// Reactive router path
const { path } = useReactivePath(app, route)
const { app } = context

// Init navigation object if not preset
if (!state.navigation) state.navigation = {}
Expand Down Expand Up @@ -130,7 +126,7 @@ export const useDocusNavigation = ({ context, state, api }: DocusAddonContext) =
* Check if a "to" path is the currently active path.
*/
function isLinkActive(to: string) {
return withTrailingSlash(path.value) === withTrailingSlash(context.$contentLocalePath(to))
return withTrailingSlash(state.currentPath) === withTrailingSlash(context.$contentLocalePath(to))
}

/**
Expand All @@ -140,9 +136,9 @@ export const useDocusNavigation = ({ context, state, api }: DocusAddonContext) =
// eslint-disable-next-line no-unused-expressions
fetchCounter.value

// Calcualte navigatin based on current path
// Calculate the navigation based on current path
return get({
from: path.value
from: state.currentPath
})
})

Expand Down
17 changes: 0 additions & 17 deletions src/core/runtime/composables/reactivePath.ts

This file was deleted.

3 changes: 2 additions & 1 deletion src/core/runtime/docus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ export const createDocus = async (
// Nuxt instance proxy
let $nuxt: any

const { ssrContext, nuxtState = {} } = context
const { ssrContext, nuxtState = {}, route } = context

// Prevent hydration mismatch: inject templateOptions from ssr payload before page load
const templateOptions = nuxtState.data?.[0].templateOptions || {}

// State
const state = reactive({
currentPath: `/${route.params.pathMatch}`,
currentPage: null,
settings: null,
theme: null,
Expand Down
1 change: 1 addition & 0 deletions src/types/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ export interface DocusSettings<T = DefaultThemeSettings> {

export type DocusState<T = DefaultThemeSettings> = {
// Core
currentPath: string
currentPage: DocusDocument | null
settings: DocusSettings<T> | null
navigation: DocusNavigation
Expand Down

0 comments on commit e08b17b

Please sign in to comment.