Skip to content

Commit 09878cd

Browse files
authored
feat(vue-app): add $nuxt.refresh() (nuxt#6194)
* feat(vue-app): add $nuxt.refreshPageData() * hotifx: lint * chore: use getMatchedComponentsInstance util * fix: rename to refreshPage * fix: rename to refresh * feat: add $nuxt.$context and handle error * feat: set $nuxt.context * hotfix: test
1 parent 6ec7654 commit 09878cd

File tree

4 files changed

+63
-1
lines changed

4 files changed

+63
-1
lines changed

packages/vue-app/template/App.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Vue from 'vue'
2+
import { getMatchedComponentsInstances, promisify, globalHandleError } from './utils'
23
<% if (loading) { %>import NuxtLoading from '<%= (typeof loading === "string" ? loading : "./components/nuxt-loading.vue") %>'<% } %>
34
<%if (buildIndicator) { %>import NuxtBuildIndicator from './components/nuxt-build-indicator'<% } %>
45
<% css.forEach((c) => { %>
@@ -73,6 +74,8 @@ export default {
7374
}
7475
// Add $nuxt.error()
7576
this.error = this.nuxt.error
77+
// Add $nuxt.context
78+
this.context = this.$options.context
7679
},
7780
<% if (loading) { %>
7881
mounted() {
@@ -100,6 +103,40 @@ export default {
100103
}
101104
}
102105
},
106+
async refresh() {
107+
const pages = getMatchedComponentsInstances(this.$route)
108+
109+
if (!pages.length) {
110+
return
111+
}
112+
<% if (loading) { %>this.$loading.start()<% } %>
113+
const promises = pages.map(async (page) => {
114+
const p = []
115+
116+
if (page.$options.fetch) {
117+
p.push(promisify(page.$options.fetch, this.context))
118+
}
119+
if (page.$options.asyncData) {
120+
p.push(
121+
promisify(page.$options.asyncData, this.context)
122+
.then((newData) => {
123+
for (const key in newData) {
124+
Vue.set(page.$data, key, newData[key])
125+
}
126+
})
127+
)
128+
}
129+
return Promise.all(p)
130+
})
131+
try {
132+
await Promise.all(promises)
133+
} catch (error) {
134+
<% if (loading) { %>this.$loading.fail()<% } %>
135+
globalHandleError(error)
136+
this.error(error)
137+
}
138+
<% if (loading) { %>this.$loading.finish()<% } %>
139+
},
103140
<% if (loading) { %>
104141
errorChanged() {
105142
if (this.nuxt.err && this.$loading) {

test/e2e/basic.browser.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,16 @@ describe('basic browser', () => {
291291
expect(p).toBe('Nuxt.js')
292292
})
293293

294+
test('/refresh-page-data', async () => {
295+
const page = await browser.page(url('/refresh-page-data'))
296+
let h1 = await page.$text('h1')
297+
expect(h1).toContain('Hello from server')
298+
await page.evaluate($nuxt => $nuxt.refresh(), page.$nuxt)
299+
h1 = await page.$text('h1')
300+
expect(h1).toContain('Hello from client')
301+
page.close()
302+
})
303+
294304
// Close server and ask nuxt to stop listening to file changes
295305
afterAll(async () => {
296306
await nuxt.close()
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<template>
2+
<h1>
3+
Hello from {{ name }}
4+
</h1>
5+
</template>
6+
7+
<script>
8+
export default {
9+
asyncData () {
10+
return {
11+
name: process.static ? 'static' : (process.server ? 'server' : 'client')
12+
}
13+
}
14+
}
15+
</script>

test/unit/async-config.size-limit.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,6 @@ describe('size-limit test', () => {
3636
const responseSizeBytes = responseSizes.reduce((bytes, responseLength) => bytes + responseLength, 0)
3737
const responseSizeKilobytes = Math.ceil(responseSizeBytes / 1024)
3838
// Without gzip!
39-
expect(responseSizeKilobytes).toBeLessThanOrEqual(195)
39+
expect(responseSizeKilobytes).toBeLessThanOrEqual(196)
4040
})
4141
})

0 commit comments

Comments
 (0)