From ecf41537ca41c343235adfa85ae07d537cd5e9b4 Mon Sep 17 00:00:00 2001 From: Julien Huang Date: Thu, 27 Apr 2023 12:51:33 +0200 Subject: [PATCH] fix(nuxt): throw errors when running legacy `asyncData` (#20535) --- packages/nuxt/src/app/composables/component.ts | 6 +++++- test/basic.test.ts | 7 +++++++ .../fixtures/basic/pages/legacy-async-data-fail.vue | 13 +++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/basic/pages/legacy-async-data-fail.vue diff --git a/packages/nuxt/src/app/composables/component.ts b/packages/nuxt/src/app/composables/component.ts index 9213550011..faf4a0abcf 100644 --- a/packages/nuxt/src/app/composables/component.ts +++ b/packages/nuxt/src/app/composables/component.ts @@ -5,6 +5,7 @@ import type { NuxtApp } from '../nuxt' import { callWithNuxt, useNuxtApp } from '../nuxt' import { useAsyncData } from './asyncData' import { useRoute } from './router' +import { createError } from './error' export const NuxtComponentIndicator = '__nuxt_component' @@ -14,7 +15,10 @@ async function runLegacyAsyncData (res: Record | Promise '') : fetchKey || route.fullPath - const { data } = await useAsyncData(`options:asyncdata:${key}`, () => callWithNuxt(nuxt, fn, [nuxt])) + const { data, error } = await useAsyncData(`options:asyncdata:${key}`, () => callWithNuxt(nuxt, fn, [nuxt])) + if (error.value) { + throw createError(error.value) + } if (data.value && typeof data.value === 'object') { Object.assign(await res, toRefs(reactive(data.value))) } else if (process.dev) { diff --git a/test/basic.test.ts b/test/basic.test.ts index 14e5a57d8a..0755a888cf 100644 --- a/test/basic.test.ts +++ b/test/basic.test.ts @@ -353,6 +353,13 @@ describe('pages', () => { await page.waitForLoadState('networkidle') expect(await page.locator('#async-server-component-count').innerHTML()).toContain(('1')) expect(await page.locator('#long-async-component-count').innerHTML()).toContain('1') + await page.close() + }) + + it('/legacy-async-data-fail', async () => { + const response = await fetch('/legacy-async-data-fail').then(r => r.text()) + expect(response).not.toContain('don\'t look at this') + expect(response).toContain('OH NNNNNNOOOOOOOOOOO') }) }) diff --git a/test/fixtures/basic/pages/legacy-async-data-fail.vue b/test/fixtures/basic/pages/legacy-async-data-fail.vue new file mode 100644 index 0000000000..072c4ae000 --- /dev/null +++ b/test/fixtures/basic/pages/legacy-async-data-fail.vue @@ -0,0 +1,13 @@ + + +