Skip to content

Commit 2324948

Browse files
committedFeb 14, 2022
feat: automatically update hash map + retry on failed page fetch
1 parent 2b9f186 commit 2324948

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed
 

Diff for: ‎src/client/app/router.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export function createRouter(
5858

5959
let latestPendingPath: string | null = null
6060

61-
async function loadPage(href: string, scrollPosition = 0) {
61+
async function loadPage(href: string, scrollPosition = 0, isRetry = false) {
6262
const targetLoc = new URL(href, fakeHost)
6363
const pendingPath = (latestPendingPath = targetLoc.pathname)
6464
try {
@@ -106,6 +106,19 @@ export function createRouter(
106106
if (!err.message.match(/fetch/)) {
107107
console.error(err)
108108
}
109+
110+
// retry on fetch fail: the page to hash map may have been invalidated
111+
// because a new deploy happened while the page is open. Try to fetch
112+
// the updated pageToHash map and fetch again.
113+
if (!isRetry) {
114+
try {
115+
const res = await fetch(siteDataRef.value.base + 'hashmap.json')
116+
;(window as any).__VP_HASH_MAP__ = await res.json()
117+
await loadPage(href, scrollPosition, true)
118+
return
119+
} catch (e) {}
120+
}
121+
109122
if (latestPendingPath === pendingPath) {
110123
latestPendingPath = null
111124
route.path = pendingPath

Diff for: ‎src/node/build/build.ts

+8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { resolveConfig } from '../config'
55
import { renderPage } from './render'
66
import { OutputChunk, OutputAsset } from 'rollup'
77
import ora from 'ora'
8+
import path from 'path'
89

910
export async function build(
1011
root: string,
@@ -68,6 +69,13 @@ export async function build(
6869
spinner.stopAndPersist({
6970
symbol: okMark
7071
})
72+
73+
// emit page hash map for the case where a user session is open
74+
// when the site got redeployed (which invalidates current hash map)
75+
fs.writeJSONSync(
76+
path.join(siteConfig.outDir, 'hashmap.json'),
77+
pageToHashMap
78+
)
7179
} finally {
7280
await fs.remove(siteConfig.tempDir)
7381
}

0 commit comments

Comments
 (0)