mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-22 05:35:13 +00:00
perf(nuxt): allow hmr for server components in dev mode (#21916)
This commit is contained in:
parent
d15df420a3
commit
435ac87961
@ -67,9 +67,9 @@ export default defineComponent({
|
|||||||
const cHead = ref<Record<'link' | 'style', Array<Record<string, string>>>>({ link: [], style: [] })
|
const cHead = ref<Record<'link' | 'style', Array<Record<string, string>>>>({ link: [], style: [] })
|
||||||
useHead(cHead)
|
useHead(cHead)
|
||||||
|
|
||||||
async function _fetchComponent () {
|
async function _fetchComponent (force = false) {
|
||||||
const key = `${props.name}_${hashId.value}`
|
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}`
|
const url = `/__nuxt_island/${key}`
|
||||||
if (process.server && process.env.prerender) {
|
if (process.server && process.env.prerender) {
|
||||||
@ -106,10 +106,10 @@ export default defineComponent({
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
const key = ref(0)
|
const key = ref(0)
|
||||||
async function fetchComponent () {
|
async function fetchComponent (force = false) {
|
||||||
nuxtApp[pKey] = nuxtApp[pKey] || {}
|
nuxtApp[pKey] = nuxtApp[pKey] || {}
|
||||||
if (!nuxtApp[pKey][uid.value]) {
|
if (!nuxtApp[pKey][uid.value]) {
|
||||||
nuxtApp[pKey][uid.value] = _fetchComponent().finally(() => {
|
nuxtApp[pKey][uid.value] = _fetchComponent(force).finally(() => {
|
||||||
delete nuxtApp[pKey]![uid.value]
|
delete nuxtApp[pKey]![uid.value]
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -127,10 +127,17 @@ export default defineComponent({
|
|||||||
setUid()
|
setUid()
|
||||||
}
|
}
|
||||||
|
|
||||||
if (process.client) {
|
if (import.meta.hot) {
|
||||||
watch(props, debounce(fetchComponent, 100))
|
import.meta.hot.on(`nuxt-server-component:${props.name}`, () => {
|
||||||
|
fetchComponent(true)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (process.client) {
|
||||||
|
watch(props, debounce(() => fetchComponent(), 100))
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: allow lazy loading server islands
|
||||||
if (process.server || !nuxtApp.isHydrating) {
|
if (process.server || !nuxtApp.isHydrating) {
|
||||||
await fetchComponent()
|
await fetchComponent()
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import { statSync } from 'node:fs'
|
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 { addPluginTemplate, addTemplate, addVitePlugin, addWebpackPlugin, defineNuxtModule, resolveAlias, updateTemplates } from '@nuxt/kit'
|
||||||
import type { Component, ComponentsDir, ComponentsOptions } from 'nuxt/schema'
|
import type { Component, ComponentsDir, ComponentsOptions } from 'nuxt/schema'
|
||||||
|
|
||||||
@ -227,6 +227,22 @@ export default defineNuxtModule<ComponentsOptions>({
|
|||||||
getComponents
|
getComponents
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
if (!isServer && nuxt.options.experimental.componentIslands) {
|
||||||
|
config.plugins.push({
|
||||||
|
name: 'nuxt-server-component-hmr',
|
||||||
|
handleHotUpdate (ctx) {
|
||||||
|
const components = getComponents()
|
||||||
|
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}`,
|
||||||
|
type: 'custom'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
})
|
})
|
||||||
nuxt.hook('webpack:config', (configs) => {
|
nuxt.hook('webpack:config', (configs) => {
|
||||||
configs.forEach((config) => {
|
configs.forEach((config) => {
|
||||||
|
Loading…
Reference in New Issue
Block a user