Skip to content
This repository was archived by the owner on Jul 6, 2025. It is now read-only.

Commit 148ab0c

Browse files
author
Je
committed
refactor: improve 404 module hmr
1 parent 1a78e5b commit 148ab0c

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

app.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,16 @@ function ALEPH({ config }: {
2828
}) {
2929
const [manifest, setManifest] = useState(() => config.manifest)
3030
const [data, setData] = useState(() => config.data)
31+
const [e404, setE404] = useState(() => ({
32+
Component: config.E404Component && util.isLikelyReactComponent(config.E404Component) ? config.E404Component : E404Page
33+
}))
3134
const [app, setApp] = useState(() => ({
3235
Component: config.AppComponent ? (util.isLikelyReactComponent(config.AppComponent) ? config.AppComponent : E501App) : null
3336
}))
3437
const [pageModules, setPageModules] = useState(() => config.pageModules)
3538
const [page, setPage] = useState(() => ({
3639
url: config.url,
37-
Component: config.PageComponent ? (util.isLikelyReactComponent(config.PageComponent) ? config.PageComponent : E501Page) : (config.E404Component || E404Page)
40+
Component: config.PageComponent ? (util.isLikelyReactComponent(config.PageComponent) ? config.PageComponent : E501Page) : null
3841
}))
3942
const onpopstate = useCallback(async () => {
4043
const { baseUrl, defaultLocale, locales } = manifest
@@ -58,10 +61,7 @@ function ALEPH({ config }: {
5861
})
5962
}
6063
} else {
61-
setPage({
62-
url,
63-
Component: config.E404Component || E404Page
64-
})
64+
setPage({ url })
6565
}
6666
}, [manifest, pageModules])
6767

@@ -82,7 +82,14 @@ function ALEPH({ config }: {
8282
setData(data)
8383
}
8484
const onAddModule = async ({ moduleId, hash }: Module) => {
85-
if (moduleId === './app.js') {
85+
if (moduleId === './404.js') {
86+
const { default: Component } = await import(getModuleImportUrl(baseUrl, { moduleId, hash }) + '?t=' + Date.now())
87+
if (util.isLikelyReactComponent(Component)) {
88+
setE404({ Component })
89+
} else {
90+
setE404({ Component: E404Page })
91+
}
92+
} else if (moduleId === './app.js') {
8693
const { default: Component } = await import(getModuleImportUrl(baseUrl, { moduleId, hash }) + '?t=' + Date.now())
8794
if (util.isLikelyReactComponent(Component)) {
8895
setApp({ Component })
@@ -104,7 +111,9 @@ function ALEPH({ config }: {
104111
}
105112
}
106113
const onRemoveModule = (moduleId: string) => {
107-
if (moduleId === './app.js') {
114+
if (moduleId === './404.js') {
115+
setE404({ Component: E404Page })
116+
} else if (moduleId === './app.js') {
108117
setApp({})
109118
} else if (moduleId === './data.js' || moduleId === './data/index.js') {
110119
console.log('[DATA]', {})
@@ -134,7 +143,7 @@ function ALEPH({ config }: {
134143
}
135144
}, [manifest])
136145

137-
const pageEl = React.createElement(page.Component)
146+
const pageEl = React.createElement(page.Component || e404.Component)
138147
return React.createElement(
139148
AppManifestContext.Provider,
140149
{ value: manifest },

0 commit comments

Comments
 (0)