Skip to content

Commit ef91b38

Browse files
committed
fix: improve hmr on invaild state
1 parent 55e8c09 commit ef91b38

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

demo/test/slides.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,9 @@ Current Page: {{$slidev.nav.currentPage}}
5252
3. A
5353

5454
</v-clicks>
55+
56+
---
57+
58+
<div>
59+
Hi
60+
</div>

packages/slidev/node/plugins/loaders.ts

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ export function createSlidesLoader(
7575

7676
const entryId = slash(entry)
7777

78+
let _layouts_cache_time = 0
79+
let _layouts_cache: Record<string, string> = {}
80+
7881
return [
7982
{
8083
name: 'slidev:loader',
@@ -140,7 +143,7 @@ export function createSlidesLoader(
140143
const a = data.slides[i]
141144
const b = newData.slides[i]
142145

143-
if (a?.content.trim() === b?.content.trim() && JSON.stringify(a.frontmatter) === JSON.stringify(b.frontmatter))
146+
if (a?.content.trim() === b?.content.trim() && equal(a.frontmatter, b.frontmatter))
144147
continue
145148

146149
ctx.server.ws.send({
@@ -160,14 +163,16 @@ export function createSlidesLoader(
160163
const vueModules = (
161164
await Promise.all(Array.from(hmrPages).map(async(i) => {
162165
const file = `${slidePrefix}${i + 1}.md`
163-
return await VuePlugin.handleHotUpdate!({
164-
...ctx,
165-
modules: Array.from(ctx.server.moduleGraph.getModulesByFile(file) || []),
166-
file,
167-
async read() {
168-
return await transformMarkdown((<any>MarkdownPlugin.transform)(newData.slides[i]?.content, file), i, newData)
169-
},
170-
})
166+
try {
167+
const md = await transformMarkdown((<any>MarkdownPlugin.transform)(newData.slides[i]?.content, file), i, newData)
168+
return await VuePlugin.handleHotUpdate!({
169+
...ctx,
170+
modules: Array.from(ctx.server.moduleGraph.getModulesByFile(file) || []),
171+
file,
172+
read() { return md },
173+
})
174+
}
175+
catch {}
171176
}),
172177
)
173178
).flatMap(i => i || [])
@@ -202,7 +207,7 @@ export function createSlidesLoader(
202207
if (id === '/@slidev/styles')
203208
return generateUserStyles()
204209

205-
// styles
210+
// monaco-types
206211
if (id === '/@slidev/monaco-types')
207212
return generateMonacoTypes()
208213

@@ -265,6 +270,10 @@ export function createSlidesLoader(
265270
}
266271

267272
async function getLayouts() {
273+
const now = Date.now()
274+
if (now - _layouts_cache_time < 2000)
275+
return _layouts_cache
276+
268277
const layouts: Record<string, string> = {}
269278

270279
const roots = [
@@ -287,6 +296,9 @@ export function createSlidesLoader(
287296
}
288297
}
289298

299+
_layouts_cache_time = now
300+
_layouts_cache = layouts
301+
290302
return layouts
291303
}
292304

0 commit comments

Comments
 (0)