From 44446b9b4c43dca0c07bd8b66a1952a5401265a2 Mon Sep 17 00:00:00 2001 From: Michiel Doesburg <3763566+mdoesburg@users.noreply.github.com> Date: Thu, 9 Jun 2022 11:42:28 -0400 Subject: [PATCH 1/5] Call fetch on all components when preview is enabled --- packages/vue-app/template/App.js | 9 ++++----- packages/vue-app/template/utils.js | 1 - 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/packages/vue-app/template/App.js b/packages/vue-app/template/App.js index 1cd3be1d55c9..60c2b94df070 100644 --- a/packages/vue-app/template/App.js +++ b/packages/vue-app/template/App.js @@ -184,11 +184,10 @@ export default { } if (page.$fetch) { p.push(page.$fetch()) - } else { - // Get all component instance to call $fetch - for (const component of getChildrenComponentInstancesUsingFetch(page.$vnode.componentInstance)) { - p.push(component.$fetch()) - } + } + // Get all component instance to call $fetch + for (const component of getChildrenComponentInstancesUsingFetch(page.$vnode.componentInstance)) { + p.push(component.$fetch()) } <% } %> <% if (features.asyncData) { %> diff --git a/packages/vue-app/template/utils.js b/packages/vue-app/template/utils.js index 91bd66ee4ee4..99a4ad6b113b 100644 --- a/packages/vue-app/template/utils.js +++ b/packages/vue-app/template/utils.js @@ -58,7 +58,6 @@ export function getChildrenComponentInstancesUsingFetch(vm, instances = []) { for (const child of children) { if (child.$fetch) { instances.push(child) - continue; // Don't get the children since it will reload the template } if (child.$children) { getChildrenComponentInstancesUsingFetch(child, instances) From e9dd6ca9b045aa7c8e2df670ce3b51053c53bcc9 Mon Sep 17 00:00:00 2001 From: Michiel Doesburg <3763566+mdoesburg@users.noreply.github.com> Date: Thu, 9 Jun 2022 11:43:26 -0400 Subject: [PATCH 2/5] Add tests --- test/dev/full-static-with-preview.test.js | 88 +++++++++++++++++++ .../components/ComponentWithFetch.vue | 29 ++++++ .../full-static-with-preview.test.js | 3 + .../full-static-with-preview/nuxt.config.js | 7 ++ .../full-static-with-preview/pages/index.vue | 22 +++++ .../pages/with-component.vue | 23 +++++ .../pages/with-nested-components.vue | 25 ++++++ .../plugins/preview.client.js | 5 ++ 8 files changed, 202 insertions(+) create mode 100644 test/dev/full-static-with-preview.test.js create mode 100644 test/fixtures/full-static-with-preview/components/ComponentWithFetch.vue create mode 100644 test/fixtures/full-static-with-preview/full-static-with-preview.test.js create mode 100644 test/fixtures/full-static-with-preview/nuxt.config.js create mode 100644 test/fixtures/full-static-with-preview/pages/index.vue create mode 100644 test/fixtures/full-static-with-preview/pages/with-component.vue create mode 100644 test/fixtures/full-static-with-preview/pages/with-nested-components.vue create mode 100644 test/fixtures/full-static-with-preview/plugins/preview.client.js diff --git a/test/dev/full-static-with-preview.test.js b/test/dev/full-static-with-preview.test.js new file mode 100644 index 000000000000..cbc8f8309e16 --- /dev/null +++ b/test/dev/full-static-with-preview.test.js @@ -0,0 +1,88 @@ +import http from 'http' +import { resolve } from 'path' +import serveStatic from 'serve-static' +import finalhandler from 'finalhandler' +import { Builder, Generator, getPort, loadFixture, Nuxt } from '../utils' + +let port +const url = route => 'http://localhost:' + port + route +const rootDir = resolve(__dirname, '..', 'fixtures/full-static-with-preview') +const distDir = resolve(rootDir, '.nuxt-generate') + +let builder +let server = null +let generator = null + +describe('full-static-with-preview', () => { + beforeAll(async () => { + const config = await loadFixture('full-static-with-preview', { + generate: { + static: false, + dir: '.nuxt-generate' + } + }) + const nuxt = new Nuxt(config) + await nuxt.ready() + + builder = new Builder(nuxt) + builder.build = jest.fn() + generator = new Generator(nuxt, builder) + + await generator.generate() + + const serve = serveStatic(distDir) + server = http.createServer((req, res) => { + serve(req, res, finalhandler(req, res)) + }) + + port = await getPort() + server.listen(port) + }) + + test('/', async () => { + const window = await generator.nuxt.server.renderAndGetWindow(url('/')) + const html = window.document.body.innerHTML + expect(html).toContain('
page-fetch-called
') + }) + + test('preview: /', async () => { + const window = await generator.nuxt.server.renderAndGetWindow(url('/?preview=true')) + const html = window.document.body.innerHTML + expect(html).toContain('page-fetch-called-in-preview
') + }) + + test('/with-component', async () => { + const window = await generator.nuxt.server.renderAndGetWindow(url('/with-component')) + const html = window.document.body.innerHTML + expect(html).toContain('page-fetch-called
') + expect(html).toContain('component-fetch-called
') + }) + + test('preview: /with-component', async () => { + const window = await generator.nuxt.server.renderAndGetWindow(url('/with-component?preview=true')) + const html = window.document.body.innerHTML + expect(html).toContain('page-fetch-called-in-preview
') + expect(html).toContain('component-fetch-called-in-preview
') + }) + + test('/with-nested-components', async () => { + const window = await generator.nuxt.server.renderAndGetWindow(url('/with-nested-components')) + const html = window.document.body.innerHTML + expect(html).toContain('page-fetch-called
') + expect(html).toContain('component-fetch-called
') + expect(html).toContain('sub-component-fetch-called
') + }) + + test('preview: /with-nested-components', async () => { + const window = await generator.nuxt.server.renderAndGetWindow(url('/with-nested-components?preview=true')) + const html = window.document.body.innerHTML + expect(html).toContain('page-fetch-called-in-preview
') + expect(html).toContain('component-fetch-called-in-preview
') + expect(html).toContain('sub-component-fetch-called-in-preview
') + }) + + // Close server and ask nuxt to stop listening to file changes + afterAll(async () => { + await server.close() + }) +}) diff --git a/test/fixtures/full-static-with-preview/components/ComponentWithFetch.vue b/test/fixtures/full-static-with-preview/components/ComponentWithFetch.vue new file mode 100644 index 000000000000..456d91237d69 --- /dev/null +++ b/test/fixtures/full-static-with-preview/components/ComponentWithFetch.vue @@ -0,0 +1,29 @@ + +{{ text }}
+{{ text }}
+{{ text }}
+{{ text }}
+sub-component-fetch-called-in-preview
') }) + test('/async-data-dependent-fetch', async () => { + const window = await generator.nuxt.server.renderAndGetWindow(url('/async-data-dependent-fetch')) + const html = window.document.body.innerHTML + expect(html).toContain('page-fetch-called
') + expect(html).toContain('component-1-fetch-called
') + expect(html).toContain('component-2-fetch-called
') + expect(html).toContain('component-3-fetch-called
') + }) + + test('preview: /async-data-dependent-fetch', async () => { + const window = await generator.nuxt.server.renderAndGetWindow(url('/async-data-dependent-fetch?preview=true')) + await waitFor(100) + const html = window.document.body.innerHTML + expect(html).toContain('page-fetch-called-in-preview
') + expect(html).toContain('component-1-fetch-called-in-preview
') + expect(html).toContain('component-2-fetch-called-in-preview
') + expect(html).toContain('component-3-fetch-called-in-preview
') + }) + // Close server and ask nuxt to stop listening to file changes afterAll(async () => { await server.close() diff --git a/test/fixtures/full-static-with-preview/components/ComponentRenderer.vue b/test/fixtures/full-static-with-preview/components/ComponentRenderer.vue new file mode 100644 index 000000000000..e0c71eb1bf6b --- /dev/null +++ b/test/fixtures/full-static-with-preview/components/ComponentRenderer.vue @@ -0,0 +1,24 @@ + +{{ text }}
+