From b9f64fa6d659836cf8bee6cdd97f9bfdb70fd80b Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Mon, 3 Jul 2023 14:49:28 +0000 Subject: [PATCH 1/3] perf(nuxt): allow hmr for server components in dev mode --- packages/nuxt/src/app/components/nuxt-island.ts | 17 ++++++++++++----- packages/nuxt/src/components/module.ts | 15 +++++++++++++++ 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/packages/nuxt/src/app/components/nuxt-island.ts b/packages/nuxt/src/app/components/nuxt-island.ts index 6e97aea95437..56bacbafb66d 100644 --- a/packages/nuxt/src/app/components/nuxt-island.ts +++ b/packages/nuxt/src/app/components/nuxt-island.ts @@ -67,9 +67,9 @@ export default defineComponent({ const cHead = ref>>>({ link: [], style: [] }) useHead(cHead) - async function _fetchComponent () { + async function _fetchComponent (force = false) { const key = `${props.name}_${hashId.value}` - if (nuxtApp.payload.data[key]) { return nuxtApp.payload.data[key] } + if (nuxtApp.payload.data[key] && !force) { return nuxtApp.payload.data[key] } const url = `/__nuxt_island/${key}` if (process.server && process.env.prerender) { @@ -106,10 +106,10 @@ export default defineComponent({ return result } const key = ref(0) - async function fetchComponent () { + async function fetchComponent (force = false) { nuxtApp[pKey] = nuxtApp[pKey] || {} if (!nuxtApp[pKey][uid.value]) { - nuxtApp[pKey][uid.value] = _fetchComponent().finally(() => { + nuxtApp[pKey][uid.value] = _fetchComponent(force).finally(() => { delete nuxtApp[pKey]![uid.value] }) } @@ -127,10 +127,17 @@ export default defineComponent({ setUid() } + if (import.meta.hot) { + import.meta.hot.on(`nuxt-server-component:${props.name}`, () => { + fetchComponent(true) + }) + } + if (process.client) { - watch(props, debounce(fetchComponent, 100)) + watch(props, debounce(() => fetchComponent(), 100)) } + // TODO: allow lazy loading server islands if (process.server || !nuxtApp.isHydrating) { await fetchComponent() } diff --git a/packages/nuxt/src/components/module.ts b/packages/nuxt/src/components/module.ts index 571552f319d9..6884559b38df 100644 --- a/packages/nuxt/src/components/module.ts +++ b/packages/nuxt/src/components/module.ts @@ -227,6 +227,21 @@ export default defineNuxtModule({ getComponents })) } + if (!isServer && nuxt.options.experimental.componentIslands) { + config.plugins.push({ + name: 'nuxt-server-component-hmr', + handleHotUpdate (ctx) { + const components = getComponents() + const comp = components.find(c => c.filePath === ctx.file) + if (comp?.mode === 'server') { + ctx.server.ws.send({ + event: `nuxt-server-component:${comp.pascalName}`, + type: 'custom' + }) + } + }, + }) + } }) nuxt.hook('webpack:config', (configs) => { configs.forEach((config) => { From 36066a4c8168e53c8d7dec608db987fe26524863 Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Mon, 3 Jul 2023 14:58:14 +0000 Subject: [PATCH 2/3] [autofix.ci] apply automated fixes --- packages/nuxt/src/components/module.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/nuxt/src/components/module.ts b/packages/nuxt/src/components/module.ts index 6884559b38df..08118c16e29f 100644 --- a/packages/nuxt/src/components/module.ts +++ b/packages/nuxt/src/components/module.ts @@ -239,7 +239,7 @@ export default defineNuxtModule({ type: 'custom' }) } - }, + } }) } }) From ad7e634c1709d20c2feaec7bf833d1d9ed13aecb Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Mon, 3 Jul 2023 15:09:00 +0000 Subject: [PATCH 3/3] =?UTF-8?q?fix:=20normalize=20path=20for=20windows=20?= =?UTF-8?q?=F0=9F=A4=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/nuxt/src/components/module.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/nuxt/src/components/module.ts b/packages/nuxt/src/components/module.ts index 6884559b38df..5388d7d9e592 100644 --- a/packages/nuxt/src/components/module.ts +++ b/packages/nuxt/src/components/module.ts @@ -1,5 +1,5 @@ import { statSync } from 'node:fs' -import { relative, resolve } from 'pathe' +import { normalize, relative, resolve } from 'pathe' import { addPluginTemplate, addTemplate, addVitePlugin, addWebpackPlugin, defineNuxtModule, resolveAlias, updateTemplates } from '@nuxt/kit' import type { Component, ComponentsDir, ComponentsOptions } from 'nuxt/schema' @@ -232,7 +232,8 @@ export default defineNuxtModule({ name: 'nuxt-server-component-hmr', handleHotUpdate (ctx) { const components = getComponents() - const comp = components.find(c => c.filePath === ctx.file) + const filePath = normalize(ctx.file) + const comp = components.find(c => c.filePath === filePath) if (comp?.mode === 'server') { ctx.server.ws.send({ event: `nuxt-server-component:${comp.pascalName}`,