From eccd10645fa5c32dc8abd3ca6a8ede9eb46c95a9 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Mon, 24 Jul 2023 13:13:17 +0000 Subject: [PATCH] feat(nuxt): await parallel/blocking promises on route change --- packages/nuxt/src/app/components/route-provider.ts | 13 +++++++++++-- packages/nuxt/src/app/composables/asyncData.ts | 2 +- packages/nuxt/src/app/nuxt.ts | 2 +- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/packages/nuxt/src/app/components/route-provider.ts b/packages/nuxt/src/app/components/route-provider.ts index d24b807aad..624c47ee03 100644 --- a/packages/nuxt/src/app/components/route-provider.ts +++ b/packages/nuxt/src/app/components/route-provider.ts @@ -2,6 +2,7 @@ import { defineComponent, h, nextTick, onMounted, provide, shallowReactive } fro import type { Ref, VNode } from 'vue' import type { RouteLocation, RouteLocationNormalizedLoaded } from '#vue-router' import { PageRouteSymbol } from '#app/components/injections' +import { useNuxtApp } from '#app/nuxt' export const RouteProvider = defineComponent({ name: 'RouteProvider', @@ -50,10 +51,18 @@ export const RouteProvider = defineComponent({ return () => { if (process.dev && process.client) { vnode = h(props.vnode, { ref: props.vnodeRef }) - return vnode + return [vnode, h(ResolvePendingPromises)] } - return h(props.vnode, { ref: props.vnodeRef }) + return [h(props.vnode, { ref: props.vnodeRef }), h(ResolvePendingPromises)] } } }) + +const ResolvePendingPromises = defineComponent({ + async setup () { + const nuxtApp = useNuxtApp() + await Promise.all(Object.values(nuxtApp._asyncDataPromises).filter(p => p?.strategy !== 'lazy')) + }, + render: () => null +}) diff --git a/packages/nuxt/src/app/composables/asyncData.ts b/packages/nuxt/src/app/composables/asyncData.ts index aab91109c8..adf7f49820 100644 --- a/packages/nuxt/src/app/composables/asyncData.ts +++ b/packages/nuxt/src/app/composables/asyncData.ts @@ -215,7 +215,7 @@ export function useAsyncData< } delete nuxt._asyncDataPromises[key] }) - nuxt._asyncDataPromises[key] = promise + nuxt._asyncDataPromises[key] = Object.assign(promise, { strategy: options.strategy }) return nuxt._asyncDataPromises[key] } diff --git a/packages/nuxt/src/app/nuxt.ts b/packages/nuxt/src/app/nuxt.ts index fe8aed8f05..7fdc68078f 100644 --- a/packages/nuxt/src/app/nuxt.ts +++ b/packages/nuxt/src/app/nuxt.ts @@ -103,7 +103,7 @@ interface _NuxtApp { [key: string]: unknown /** @internal */ - _asyncDataPromises: Record | undefined> + _asyncDataPromises: Record & { strategy?: 'lazy' | 'blocking' | 'parallel' } | undefined> /** @internal */ _asyncData: Record