@@ -894,7 +894,8 @@ export type ScriptProps = Omit<
894
894
* @see https://remix.run/components/scripts
895
895
*/
896
896
export function Scripts ( props : ScriptProps ) {
897
- let { manifest, serverHandoffString, abortDelay } = useRemixContext ( ) ;
897
+ let { manifest, serverHandoffString, abortDelay, serializeError } =
898
+ useRemixContext ( ) ;
898
899
let { router, static : isStatic , staticContext } = useDataRouterContext ( ) ;
899
900
let { matches } = useDataRouterStateContext ( ) ;
900
901
let navigation = useNavigation ( ) ;
@@ -903,6 +904,16 @@ export function Scripts(props: ScriptProps) {
903
904
isHydrated = true ;
904
905
} , [ ] ) ;
905
906
907
+ let serializeErrorImp = ( error : unknown ) => {
908
+ let toSerialize : unknown ;
909
+ if ( serializeError && error instanceof Error ) {
910
+ toSerialize = serializeError ( error ) ;
911
+ } else {
912
+ toSerialize = error ;
913
+ }
914
+ return toSerialize ;
915
+ } ;
916
+
906
917
let deferredScripts : any [ ] = [ ] ;
907
918
let initialScripts = React . useMemo ( ( ) => {
908
919
let contextScript = staticContext
@@ -981,16 +992,7 @@ export function Scripts(props: ScriptProps) {
981
992
} else {
982
993
let trackedPromise = deferredData . data [ key ] as TrackedPromise ;
983
994
if ( typeof trackedPromise . _error !== "undefined" ) {
984
- let toSerialize : { message : string ; stack ?: string } =
985
- process . env . NODE_ENV === "development"
986
- ? {
987
- message : trackedPromise . _error . message ,
988
- stack : trackedPromise . _error . stack ,
989
- }
990
- : {
991
- message : "Unexpected Server Error" ,
992
- stack : undefined ,
993
- } ;
995
+ let toSerialize = serializeErrorImp ( trackedPromise . _error ) ;
994
996
return `${ JSON . stringify (
995
997
key
996
998
) } :__remixContext.p(!1, ${ escapeHtml (
@@ -1008,7 +1010,14 @@ export function Scripts(props: ScriptProps) {
1008
1010
try {
1009
1011
serializedData = JSON . stringify ( data ) ;
1010
1012
} catch ( error ) {
1011
- console . error ( error ) ;
1013
+ let toSerialize = serializeErrorImp (
1014
+ trackedPromise . _error
1015
+ ) ;
1016
+ return `${ JSON . stringify (
1017
+ key
1018
+ ) } :__remixContext.p(!1, ${ escapeHtml (
1019
+ JSON . stringify ( toSerialize )
1020
+ ) } )`;
1012
1021
}
1013
1022
return `${ JSON . stringify (
1014
1023
key
0 commit comments