Skip to content

Commit 68a6197

Browse files
committed
fix: reset sandbox when changing files for safari compat
ref vuejs/docs#1973
1 parent d35080e commit 68a6197

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

src/output/Preview.vue

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ watch(
4949
)
5050
5151
// reset sandbox when version changes
52-
watch(() => store.state.vueRuntimeURL, createSandbox)
52+
watch(() => store.state.resetFlip, createSandbox)
5353
5454
onUnmounted(() => {
5555
proxy.destroy()
@@ -202,11 +202,15 @@ async function updatePreview() {
202202
203203
// compile code to simulated module system
204204
const modules = compileModulesForPreview(store)
205-
console.log(`[@vue/repl] successfully compiled ${modules.length} modules.`)
205+
console.log(
206+
`[@vue/repl] successfully compiled ${modules.length} module${
207+
modules.length > 1 ? `s` : ``
208+
}.`
209+
)
206210
207211
const codeToEval = [
208-
`window.__modules__ = {};window.__css__ = '';` +
209-
`if (window.__app__) window.__app__.unmount();` +
212+
`window.__modules__ = {}\nwindow.__css__ = ''\n` +
213+
`if (window.__app__) window.__app__.unmount()\n` +
210214
(isSSR ? `` : `document.body.innerHTML = '<div id="app"></div>'`),
211215
...modules,
212216
`document.getElementById('__sfc-styles').innerHTML = window.__css__`

src/store.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ export interface StoreState {
4848
errors: (string | Error)[]
4949
vueRuntimeURL: string
5050
vueServerRendererURL: string
51+
// used to force reset the sandbox
52+
resetFlip: boolean
5153
}
5254

5355
export interface SFCOptions {
@@ -126,7 +128,8 @@ export class ReplStore implements Store {
126128
activeFile: files[mainFile],
127129
errors: [],
128130
vueRuntimeURL: this.defaultVueRuntimeURL,
129-
vueServerRendererURL: this.defaultVueServerRendererURL
131+
vueServerRendererURL: this.defaultVueServerRendererURL,
132+
resetFlip: true
130133
})
131134

132135
this.initImportMap()
@@ -191,6 +194,11 @@ export class ReplStore implements Store {
191194
this.state.files = files
192195
this.initImportMap()
193196
this.setActive(mainFile)
197+
this.forceSandboxReset()
198+
}
199+
200+
private forceSandboxReset() {
201+
this.state.resetFlip = !this.state.resetFlip
194202
}
195203

196204
private initImportMap() {
@@ -256,6 +264,7 @@ export class ReplStore implements Store {
256264
imports.vue = runtimeUrl
257265
imports['vue/server-renderer'] = ssrUrl
258266
this.setImportMap(importMap)
267+
this.forceSandboxReset()
259268
console.info(`[@vue/repl] Now using Vue version: ${version}`)
260269
}
261270

@@ -269,6 +278,7 @@ export class ReplStore implements Store {
269278
imports.vue = this.defaultVueRuntimeURL
270279
imports['vue/server-renderer'] = this.defaultVueServerRendererURL
271280
this.setImportMap(importMap)
281+
this.forceSandboxReset()
272282
console.info(`[@vue/repl] Now using default Vue version`)
273283
}
274284
}

0 commit comments

Comments
 (0)