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

Commit 4506966

Browse files
committed
fix: fix isLikelyReactComponent in bundle mode
improve StatusError component
1 parent 0fb2b48 commit 4506966

File tree

4 files changed

+59
-43
lines changed

4 files changed

+59
-43
lines changed

framework/react/error.ts

Lines changed: 45 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, createElement, Fragment } from 'https://esm.sh/react'
1+
import { Component, createElement, CSSProperties } from 'https://esm.sh/react'
22

33
export class ErrorBoundary extends Component {
44
state: { stack: string | null }
@@ -43,7 +43,7 @@ export function E404Page() {
4343
)
4444
}
4545

46-
export function E400MissingDefaultExportAsComponent({ name }: { name: string }) {
46+
export function E400MissingComponent({ name }: { name: string }) {
4747
return createElement(
4848
StatusError,
4949
{
@@ -54,47 +54,64 @@ export function E400MissingDefaultExportAsComponent({ name }: { name: string })
5454
)
5555
}
5656

57-
export function StatusError({ status, message, showRefreshButton }: { status: number, message: string, showRefreshButton?: boolean }) {
57+
const resetStyle: CSSProperties = {
58+
padding: 0,
59+
margin: 0,
60+
lineHeight: 1.5,
61+
fontSize: 15,
62+
fontWeight: 400,
63+
color: '#333',
64+
}
65+
66+
export function StatusError({ status, message }: { status: number, message: string }) {
5867
return (
5968
createElement(
60-
Fragment,
61-
null,
69+
'div',
70+
{
71+
style: {
72+
...resetStyle,
73+
display: 'flex',
74+
alignItems: 'center',
75+
justifyContent: 'center',
76+
flexDirection: 'column',
77+
width: '100vm',
78+
height: '100vh',
79+
}
80+
},
6281
createElement(
6382
'p',
64-
null,
83+
{
84+
style: {
85+
...resetStyle,
86+
fontWeight: 500,
87+
}
88+
},
6589
createElement(
66-
'strong',
67-
null,
68-
createElement(
69-
'code',
70-
null,
71-
status
72-
)
90+
'code',
91+
{
92+
style: {
93+
...resetStyle,
94+
fontWeight: 700,
95+
}
96+
},
97+
status
7398
),
7499
createElement(
75100
'small',
76-
null,
101+
{
102+
style: {
103+
...resetStyle,
104+
fontSize: 4,
105+
color: '#999'
106+
}
107+
},
77108
' - '
78109
),
79110
createElement(
80111
'span',
81112
null,
82113
message
83114
)
84-
),
85-
showRefreshButton && createElement(
86-
'p',
87-
null,
88-
createElement(
89-
'button',
90-
{
91-
onClick() {
92-
const { location } = window as any
93-
location.reload()
94-
}
95-
},
96-
'Refresh'
97-
)
98115
)
99116
)
100117
)

framework/react/renderer.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import util from '../../shared/util.ts'
66
import type { RouterURL } from '../../types.ts'
77
import events from '../core/events.ts'
88
import { RendererContext, RouterContext } from './context.ts'
9-
import { AsyncUseDenoError, E400MissingDefaultExportAsComponent, E404Page } from './error.ts'
9+
import { AsyncUseDenoError, E400MissingComponent, E404Page } from './error.ts'
1010
import { serverStyles } from './style.ts'
1111
import { createPageProps, isLikelyReactComponent } from './util.ts'
1212

@@ -30,21 +30,15 @@ export async function renderPage(
3030
if (isLikelyReactComponent(App)) {
3131
el = createElement(App, pageProps)
3232
} else {
33-
el = createElement(
34-
E400MissingDefaultExportAsComponent,
35-
{ name: 'Custom App' }
36-
)
33+
el = createElement(E400MissingComponent, { name: 'Custom App' })
3734
}
3835
} else {
3936
if (pageProps.Page == null) {
4037
if (E404) {
4138
if (isLikelyReactComponent(E404)) {
4239
el = createElement(E404)
4340
} else {
44-
el = createElement(
45-
E400MissingDefaultExportAsComponent,
46-
{ name: 'Custom 404' }
47-
)
41+
el = createElement(E400MissingComponent, { name: 'Custom 404' })
4842
}
4943
} else {
5044
el = createElement(E404Page)

framework/react/root.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type { RouterURL } from '../../types.ts'
55
import events from '../core/events.ts'
66
import { RouteModule, Routing } from '../core/routing.ts'
77
import { RouterContext } from './context.ts'
8-
import { E400MissingDefaultExportAsComponent, E404Page, ErrorBoundary } from './error.ts'
8+
import { E400MissingComponent, E404Page, ErrorBoundary } from './error.ts'
99
import { createPageProps, importModule, isLikelyReactComponent } from './util.ts'
1010

1111
export default function AlephAppRoot({
@@ -25,7 +25,7 @@ export default function AlephAppRoot({
2525
if (isLikelyReactComponent(E404)) {
2626
return { Component: E404 }
2727
}
28-
return { Component: E400MissingDefaultExportAsComponent, props: { name: 'Custom 404 Page' } }
28+
return { Component: E400MissingComponent, props: { name: 'Custom 404 Page' } }
2929
}
3030
return { Component: E404Page }
3131
})
@@ -35,7 +35,7 @@ export default function AlephAppRoot({
3535
if (isLikelyReactComponent(App)) {
3636
return { Component: App }
3737
}
38-
return { Component: E400MissingDefaultExportAsComponent, props: { name: 'Custom App' } }
38+
return { Component: E400MissingComponent, props: { name: 'Custom App' } }
3939
}
4040
return { Component: null }
4141
})
@@ -99,7 +99,7 @@ export default function AlephAppRoot({
9999
if (isLikelyReactComponent(Component)) {
100100
setApp({ Component })
101101
} else {
102-
setApp({ Component: E400MissingDefaultExportAsComponent, props: { name: 'Custom App' } })
102+
setApp({ Component: E400MissingComponent, props: { name: 'Custom App' } })
103103
}
104104
break
105105
}

framework/react/util.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { ComponentType } from 'https://esm.sh/react'
22
import { hashShort, reModuleExt } from '../../shared/constants.ts'
33
import util from '../../shared/util.ts'
44
import { RouteModule } from '../core/routing.ts'
5-
import { E400MissingDefaultExportAsComponent } from './error.ts'
5+
import { E400MissingComponent } from './error.ts'
66

77
const symbolFor = typeof Symbol === 'function' && Symbol.for
88
const REACT_FORWARD_REF_TYPE = symbolFor ? Symbol.for('react.forward_ref') : 0xead0
@@ -51,6 +51,11 @@ export function isLikelyReactComponent(type: any): Boolean {
5151
return false
5252
}
5353
}
54+
const { __ALEPH } = window as any
55+
if (__ALEPH) {
56+
// in bundle mode, component name will be compressed
57+
return true
58+
}
5459
const name = type.name || type.displayName
5560
return typeof name === 'string' && /^[A-Z]/.test(name)
5661
case 'object':
@@ -96,7 +101,7 @@ function _createPagePropsSegment(seg: { url: string, Component?: ComponentType<a
96101
if (isLikelyReactComponent(seg.Component)) {
97102
pageProps.Page = seg.Component
98103
} else {
99-
pageProps.Page = E400MissingDefaultExportAsComponent
104+
pageProps.Page = E400MissingComponent
100105
pageProps.pageProps = { name: 'Page: ' + util.trimPrefix(seg.url, '/pages').replace(reModuleExt, '') }
101106
}
102107
}

0 commit comments

Comments
 (0)