Skip to content

Commit

Permalink
fix: never fetch undefined chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
brc-dd committed Jul 1, 2023
1 parent 05eca66 commit a1d7458
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 19 deletions.
8 changes: 3 additions & 5 deletions src/client/app/composables/preFetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export function usePrefetch() {
if (!hasFetched.has(pathname)) {
hasFetched.add(pathname)
const pageChunkPath = pathToFile(pathname)
doFetch(pageChunkPath)
if (pageChunkPath) doFetch(pageChunkPath)
}
}
})
Expand All @@ -90,11 +90,9 @@ export function usePrefetch() {
if (
// only prefetch same tab navigation, since a new tab will load
// the lean js chunk instead.
!link.target &&
link.target !== '_blank' &&
// only prefetch inbound links
hostname === location.hostname &&
// bypass for links inside .vp-raw
!link.closest('.vp-raw')
hostname === location.hostname
) {
if (pathname !== location.pathname) {
observer!.observe(link)
Expand Down
31 changes: 19 additions & 12 deletions src/client/app/index.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import RawTheme from '@theme/index'
import {
type App,
createApp as createClientApp,
createSSRApp,
defineComponent,
h,
onMounted,
watchEffect
watchEffect,
type App
} from 'vue'
import RawTheme from '@theme/index'
import { inBrowser, pathToFile } from './utils'
import { type Router, RouterSymbol, createRouter, scrollTo } from './router'
import { siteDataRef, useData } from './data'
import { useUpdateHead } from './composables/head'
import { usePrefetch } from './composables/preFetch'
import { dataSymbol, initData } from './data'
import { Content } from './components/Content'
import { notFoundPageData } from '../shared'
import { ClientOnly } from './components/ClientOnly'
import { useCopyCode } from './composables/copyCode'
import { Content } from './components/Content'
import { useCodeGroups } from './composables/codeGroups'
import { useCopyCode } from './composables/copyCode'
import { useUpdateHead } from './composables/head'
import { usePrefetch } from './composables/preFetch'
import { dataSymbol, initData, siteDataRef, useData } from './data'
import { RouterSymbol, createRouter, scrollTo, type Router } from './router'
import { inBrowser, pathToFile } from './utils'

function resolveThemeExtends(theme: typeof RawTheme): typeof RawTheme {
if (theme.extends) {
Expand Down Expand Up @@ -120,9 +120,16 @@ function newRouter(): Router {
let isInitialPageLoad = inBrowser
let initialPath: string

return createRouter((path) => {
return createRouter(async (path) => {
let pageFilePath = pathToFile(path)

if (!pageFilePath) {
return {
__pageData: notFoundPageData,
default: Theme.NotFound
}
}

if (isInitialPageLoad) {
initialPath = pageFilePath
}
Expand Down
5 changes: 3 additions & 2 deletions src/client/app/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export { inBrowser } from '../shared'
/**
* Join two paths by resolving the slash collision.
*/
export function joinPath(base: string, path: string): string {
export function joinPath(base: string, path: string) {
return `${base}${path}`.replace(/\/+/g, '/')
}

Expand All @@ -31,7 +31,7 @@ export function withBase(path: string) {
/**
* Converts a url path to the corresponding js chunk filename.
*/
export function pathToFile(path: string): string {
export function pathToFile(path: string) {
let pagePath = path.replace(/\.html$/, '')
pagePath = decodeURIComponent(pagePath)
pagePath = pagePath.replace(/\/$/, '/index') // /foo/ -> /foo/index
Expand All @@ -57,6 +57,7 @@ export function pathToFile(path: string): string {
: pagePath.slice(0, -3) + '_index.md'
pageHash = __VP_HASH_MAP__[pagePath.toLowerCase()]
}
if (!pageHash) return null
pagePath = `${base}assets/${pagePath}.${pageHash}.js`
} else {
// ssr build uses much simpler name mapping
Expand Down
1 change: 1 addition & 0 deletions src/client/theme-default/components/VPLocalSearchBox.vue
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ debouncedWatch(
async function fetchExcerpt(id: string) {
const file = pathToFile(id.slice(0, id.indexOf('#')))
try {
if (!file) throw new Error(`Cannot find file for id: ${id}`)
return { id, mod: await import(/*@vite-ignore*/ file) }
} catch (e) {
console.error(e)
Expand Down

0 comments on commit a1d7458

Please sign in to comment.