-
Notifications
You must be signed in to change notification settings - Fork 27k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: generalize next route error helpers (#72774)
- Loading branch information
Showing
15 changed files
with
214 additions
and
130 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
packages/next/src/client/components/dev-root-http-access-fallback-boundary.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
packages/next/src/client/components/http-access-fallback/error-fallback.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import React from 'react' | ||
|
||
const styles: Record<string, React.CSSProperties> = { | ||
error: { | ||
// https://github.com/sindresorhus/modern-normalize/blob/main/modern-normalize.css#L38-L52 | ||
fontFamily: | ||
'system-ui,"Segoe UI",Roboto,Helvetica,Arial,sans-serif,"Apple Color Emoji","Segoe UI Emoji"', | ||
height: '100vh', | ||
textAlign: 'center', | ||
display: 'flex', | ||
flexDirection: 'column', | ||
alignItems: 'center', | ||
justifyContent: 'center', | ||
}, | ||
|
||
desc: { | ||
display: 'inline-block', | ||
}, | ||
|
||
h1: { | ||
display: 'inline-block', | ||
margin: '0 20px 0 0', | ||
padding: '0 23px 0 0', | ||
fontSize: 24, | ||
fontWeight: 500, | ||
verticalAlign: 'top', | ||
lineHeight: '49px', | ||
}, | ||
|
||
h2: { | ||
fontSize: 14, | ||
fontWeight: 400, | ||
lineHeight: '49px', | ||
margin: 0, | ||
}, | ||
} | ||
|
||
export function HTTPAccessErrorFallback({ | ||
status, | ||
message, | ||
}: { | ||
status: number | ||
message: string | ||
}) { | ||
return ( | ||
<> | ||
{/* <head> */} | ||
<title>{`${status}: ${message}`}</title> | ||
{/* </head> */} | ||
<div style={styles.error}> | ||
<div> | ||
<style | ||
dangerouslySetInnerHTML={{ | ||
/* Minified CSS from | ||
body { margin: 0; color: #000; background: #fff; } | ||
.next-error-h1 { | ||
border-right: 1px solid rgba(0, 0, 0, .3); | ||
} | ||
@media (prefers-color-scheme: dark) { | ||
body { color: #fff; background: #000; } | ||
.next-error-h1 { | ||
border-right: 1px solid rgba(255, 255, 255, .3); | ||
} | ||
} | ||
*/ | ||
__html: `body{color:#000;background:#fff;margin:0}.next-error-h1{border-right:1px solid rgba(0,0,0,.3)}@media (prefers-color-scheme:dark){body{color:#fff;background:#000}.next-error-h1{border-right:1px solid rgba(255,255,255,.3)}}`, | ||
}} | ||
/> | ||
<h1 className="next-error-h1" style={styles.h1}> | ||
{status} | ||
</h1> | ||
<div style={styles.desc}> | ||
<h2 style={styles.h2}>{message}</h2> | ||
</div> | ||
</div> | ||
</div> | ||
</> | ||
) | ||
} |
52 changes: 52 additions & 0 deletions
52
packages/next/src/client/components/http-access-fallback/http-access-fallback.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
const ALLOWED_CODES = new Set([404]) | ||
|
||
export const HTTP_ERROR_FALLBACK_ERROR_CODE = 'NEXT_HTTP_ERROR_FALLBACK' | ||
|
||
export type HTTPAccessFallbackError = Error & { | ||
digest: `${typeof HTTP_ERROR_FALLBACK_ERROR_CODE};${string}` | ||
} | ||
|
||
/** | ||
* Checks an error to determine if it's an error generated by | ||
* the HTTP navigation APIs `notFound()`, `forbidden()` or `unauthorized()`. | ||
* | ||
* @param error the error that may reference a HTTP access error | ||
* @returns true if the error is a HTTP access error | ||
*/ | ||
export function isHTTPAccessFallbackError( | ||
error: unknown | ||
): error is HTTPAccessFallbackError { | ||
if ( | ||
typeof error !== 'object' || | ||
error === null || | ||
!('digest' in error) || | ||
typeof error.digest !== 'string' | ||
) { | ||
return false | ||
} | ||
const [prefix, httpStatus] = error.digest.split(';') | ||
|
||
return ( | ||
prefix === HTTP_ERROR_FALLBACK_ERROR_CODE && | ||
ALLOWED_CODES.has(Number(httpStatus)) | ||
) | ||
} | ||
|
||
export function getAccessFallbackHTTPStatus( | ||
error: HTTPAccessFallbackError | ||
): number { | ||
const httpStatus = error.digest.split(';')[1] | ||
return Number(httpStatus) | ||
} | ||
|
||
export function getAccessFallbackErrorTypeByStatus( | ||
status: number | ||
): 'not-found' | undefined { | ||
// TODO: support 403 and 401 | ||
switch (status) { | ||
case 404: | ||
return 'not-found' | ||
default: | ||
return | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,10 @@ | ||
import React from 'react' | ||
|
||
const styles: Record<string, React.CSSProperties> = { | ||
error: { | ||
// https://github.com/sindresorhus/modern-normalize/blob/main/modern-normalize.css#L38-L52 | ||
fontFamily: | ||
'system-ui,"Segoe UI",Roboto,Helvetica,Arial,sans-serif,"Apple Color Emoji","Segoe UI Emoji"', | ||
height: '100vh', | ||
textAlign: 'center', | ||
display: 'flex', | ||
flexDirection: 'column', | ||
alignItems: 'center', | ||
justifyContent: 'center', | ||
}, | ||
|
||
desc: { | ||
display: 'inline-block', | ||
}, | ||
|
||
h1: { | ||
display: 'inline-block', | ||
margin: '0 20px 0 0', | ||
padding: '0 23px 0 0', | ||
fontSize: 24, | ||
fontWeight: 500, | ||
verticalAlign: 'top', | ||
lineHeight: '49px', | ||
}, | ||
|
||
h2: { | ||
fontSize: 14, | ||
fontWeight: 400, | ||
lineHeight: '49px', | ||
margin: 0, | ||
}, | ||
} | ||
import { HTTPAccessErrorFallback } from './http-access-fallback/error-fallback' | ||
|
||
export default function NotFound() { | ||
return ( | ||
<> | ||
{/* <head> */} | ||
<title>404: This page could not be found.</title> | ||
{/* </head> */} | ||
<div style={styles.error}> | ||
<div> | ||
<style | ||
dangerouslySetInnerHTML={{ | ||
/* Minified CSS from | ||
body { margin: 0; color: #000; background: #fff; } | ||
.next-error-h1 { | ||
border-right: 1px solid rgba(0, 0, 0, .3); | ||
} | ||
@media (prefers-color-scheme: dark) { | ||
body { color: #fff; background: #000; } | ||
.next-error-h1 { | ||
border-right: 1px solid rgba(255, 255, 255, .3); | ||
} | ||
} | ||
*/ | ||
__html: `body{color:#000;background:#fff;margin:0}.next-error-h1{border-right:1px solid rgba(0,0,0,.3)}@media (prefers-color-scheme:dark){body{color:#fff;background:#000}.next-error-h1{border-right:1px solid rgba(255,255,255,.3)}}`, | ||
}} | ||
/> | ||
<h1 className="next-error-h1" style={styles.h1}> | ||
404 | ||
</h1> | ||
<div style={styles.desc}> | ||
<h2 style={styles.h2}>This page could not be found.</h2> | ||
</div> | ||
</div> | ||
</div> | ||
</> | ||
<HTTPAccessErrorFallback | ||
status={404} | ||
message="This page could not be found." | ||
/> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.