@@ -28,28 +28,42 @@ function toAbsolutePath (path: string): string {
2828 return path
2929}
3030
31+ /**
32+ * Do not redirect to `#/` if we are already on the bare domain, otherwise the
33+ * hash can interfere with CTRL+R-style page reloads
34+ */
35+ function doNothingIfClickedOnRoot ( event : React . MouseEvent ) : boolean {
36+ if ( globalThis . location . hash === '' ) {
37+ event . preventDefault ( )
38+ event . stopPropagation ( )
39+ return false
40+ }
41+
42+ return true
43+ }
44+
3145function Header ( ) : React . ReactElement {
3246 let errorPageLink : React . ReactElement | undefined
3347
3448 if ( globalThis . fetchError != null ) {
3549 errorPageLink = (
36- < NavLink to = { `/ ${ HASH_FRAGMENTS . IPFS_SW_FETCH_ERROR_UI } ` } className = { ( { isActive } ) => isActive ? 'white' : '' } >
50+ < NavLink to = '/' className = { ( { isActive } ) => ( isActive ?? globalThis . location . hash === '' ) ? 'yellow-muted' : 'yellow' } onClickCapture = { doNothingIfClickedOnRoot } >
3751 < FaExclamationCircle className = 'ml2 f3' />
3852 </ NavLink >
3953 )
4054 }
4155
4256 if ( globalThis . serverError != null ) {
4357 errorPageLink = (
44- < NavLink to = { `/ ${ HASH_FRAGMENTS . IPFS_SW_SERVER_ERROR_UI } ` } className = { ( { isActive } ) => isActive ? 'white' : '' } >
58+ < NavLink to = '/' className = { ( { isActive } ) => ( isActive ?? globalThis . location . hash === '' ) ? 'red-muted' : 'red' } onClickCapture = { doNothingIfClickedOnRoot } >
4559 < FaExclamationCircle className = 'ml2 f3' />
4660 </ NavLink >
4761 )
4862 }
4963
5064 if ( globalThis . originIsolationWarning != null ) {
5165 errorPageLink = (
52- < NavLink to = { `/ ${ HASH_FRAGMENTS . IPFS_SW_ORIGIN_ISOLATION_WARNING } ` } className = { ( { isActive } ) => isActive ? 'white' : '' } >
66+ < NavLink to = '/' className = { ( { isActive } ) => ( isActive ?? globalThis . location . hash === '' ) ? 'yellow-muted' : 'yellow' } onClickCapture = { doNothingIfClickedOnRoot } >
5367 < FaExclamationTriangle className = 'ml2 f3' />
5468 </ NavLink >
5569 )
@@ -65,7 +79,7 @@ function Header (): React.ReactElement {
6579 < div className = 'pb1 ma0 mr2 inline-flex items-center aqua' >
6680 < h1 className = 'e2e-header-title f3 fw2 ttu sans-serif' > Service Worker Gateway</ h1 >
6781 { errorPageLink }
68- < NavLink to = '/' className = { ( { isActive } ) => isActive ? 'white' : '' } >
82+ < NavLink to = { `/ ${ HASH_FRAGMENTS . IPFS_SW_LOAD_UI } ` } className = { ( { isActive } ) => ( isActive || ( errorPageLink == null && globalThis . location . hash === '' ) ) ? 'white' : '' } >
6983 < FaDownload className = 'ml2 f3' />
7084 </ NavLink >
7185 < NavLink to = { `/${ HASH_FRAGMENTS . IPFS_SW_ABOUT_UI } ` } className = { ( { isActive } ) => isActive ? 'white' : '' } >
@@ -83,31 +97,47 @@ function Header (): React.ReactElement {
8397}
8498
8599/**
86- * This component is used when either:
87- *
88- * 1. the SW encountered an error fulfilling the request
89- * 2. Installing the service worker failed
90- * 3. The user wants to update the service worker config
91- * 4. The user needs to accept the origin isolation warning
100+ * Dynamically create an index route - if an error has occurred, show that on
101+ * the landing page, otherwise show the "Enter a CID" UI page
92102 */
93- function App ( ) : React . ReactElement {
103+ function getIndexRoute ( ) : React . ReactElement {
94104 if ( globalThis . fetchError != null && globalThis . location . hash === '' ) {
95- window . location . hash = `/${ HASH_FRAGMENTS . IPFS_SW_FETCH_ERROR_UI } `
105+ return (
106+ < Route element = { < FetchErrorPage /> } index />
107+ )
96108 }
97109
98110 if ( globalThis . serverError != null && globalThis . location . hash === '' ) {
99- window . location . hash = `/${ HASH_FRAGMENTS . IPFS_SW_SERVER_ERROR_UI } `
111+ return (
112+ < Route element = { < ServerErrorPage /> } index />
113+ )
100114 }
101115
102116 if ( globalThis . originIsolationWarning != null && globalThis . location . hash === '' ) {
103- window . location . hash = `/${ HASH_FRAGMENTS . IPFS_SW_ORIGIN_ISOLATION_WARNING } `
117+ return (
118+ < Route element = { < OriginIsolationWarningPage /> } index />
119+ )
104120 }
105121
122+ return (
123+ < Route element = { < HomePage /> } index />
124+ )
125+ }
126+
127+ /**
128+ * This component is used when either:
129+ *
130+ * 1. the SW encountered an error fulfilling the request
131+ * 2. Installing the service worker failed
132+ * 3. The user wants to update the service worker config
133+ * 4. The user needs to accept the origin isolation warning
134+ */
135+ function App ( ) : React . ReactElement {
106136 return (
107137 < HashRouter >
108138 < Header />
109139 < Routes >
110- < Route index element = { < HomePage /> } /> ,
140+ { getIndexRoute ( ) }
111141 < Route path = { `/${ HASH_FRAGMENTS . IPFS_SW_LOAD_UI } ` } element = { < HomePage /> } /> ,
112142 < Route path = { `/${ HASH_FRAGMENTS . IPFS_SW_ABOUT_UI } ` } element = { < AboutPage /> } /> ,
113143 < Route path = { `/${ HASH_FRAGMENTS . IPFS_SW_CONFIG_UI } ` } element = { < ConfigPage /> } /> ,
0 commit comments