Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(vue-app): this.$nuxt.refresh doesn't refresh every fetch hooks #9530

Open
wants to merge 12 commits into
base: 2.x
Choose a base branch
from
Open
27 changes: 26 additions & 1 deletion packages/vue-app/template/App.js
Expand Up @@ -182,6 +182,13 @@ export default {
if (page.$options.fetch && page.$options.fetch.length) {
p.push(promisify(page.$options.fetch, this.context))
}
if (page.$fetch) {
p.push(page.$fetch())
}
// Get all component instance to call $fetch
for (const component of getChildrenComponentInstancesUsingFetch(page.$vnode.componentInstance, [], pages)) {
p.push(component.$fetch())
}
<% } %>

<% if (features.asyncData) { %>
Expand All @@ -201,7 +208,7 @@ export default {
// Wait for asyncData & old fetch to finish
await Promise.all(p)
// Cleanup refs
p = []
p = []

if (page.$fetch) {
p.push(page.$fetch())
Expand All @@ -214,6 +221,24 @@ export default {

return Promise.all(p)
})

<% if (features.fetch) { %>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const layout = this.$children.find((component) => component._name?.toLowerCase().includes("layouts"))
// if layout is enabled
if (layout) {
const p = [];
// get layout fetch if exist
if (layout.$fetch) {
p.push(layout.$fetch())
}
// Get all component instance from layout to call $fetch
for (const component of getChildrenComponentInstancesUsingFetch(layout.$vnode.componentInstance, [], pages)) {
p.push(component.$fetch())
}

promises.push(Promise.all(p))
}
<% } %>
try {
await Promise.all(promises)
} catch (error) {
Expand Down
5 changes: 4 additions & 1 deletion packages/vue-app/template/utils.js
Expand Up @@ -53,9 +53,12 @@ export function purifyData(data) {
return obj
}, {})
}
export function getChildrenComponentInstancesUsingFetch(vm, instances = []) {
export function getChildrenComponentInstancesUsingFetch(vm, instances = [], exclude = []) {
const children = vm.$children || []
for (const child of children) {
// ignore the component when it is in the exclude list
if (exclude.includes(child)) continue;

if (child.$fetch) {
instances.push(child)
}
Expand Down