diff --git a/.changeset/fifty-starfishes-remain.md b/.changeset/fifty-starfishes-remain.md new file mode 100644 index 00000000000..a1de76ab56a --- /dev/null +++ b/.changeset/fifty-starfishes-remain.md @@ -0,0 +1,6 @@ +--- +'@modern-js/runtime': patch +--- + +fix: parse `routerDataFnArgs` in `mergeLoaderData` function +fix: 在 `mergeLoaderData` 函数中解析 routerDataFnArgs diff --git a/packages/runtime/plugin-runtime/src/router/runtime/DeferredDataScripts.node.tsx b/packages/runtime/plugin-runtime/src/router/runtime/DeferredDataScripts.node.tsx index 82505304b6a..3d981b2296a 100644 --- a/packages/runtime/plugin-runtime/src/router/runtime/DeferredDataScripts.node.tsx +++ b/packages/runtime/plugin-runtime/src/router/runtime/DeferredDataScripts.node.tsx @@ -82,7 +82,7 @@ const DeferredDataScripts = (props?: { return { key, routerDataFnName: 's', - routerDataFnArgs: [routeId, key], + routerDataFnArgs: [serializeJson(routeId), serializeJson(key)], }; } else { const trackedPromise = deferredData.data[key] as TrackedPromise; @@ -98,7 +98,10 @@ const DeferredDataScripts = (props?: { return { key, routerDataFnName: 'p', - routerDataFnArgs: [undefined, serializeJson(error)], + routerDataFnArgs: [ + serializeJson(undefined), + serializeJson(error), + ], }; } else { if (typeof trackedPromise._data === 'undefined') { diff --git a/packages/runtime/plugin-runtime/src/router/runtime/constants.ts b/packages/runtime/plugin-runtime/src/router/runtime/constants.ts index 56d9fded8d9..ed31cb2c7a9 100644 --- a/packages/runtime/plugin-runtime/src/router/runtime/constants.ts +++ b/packages/runtime/plugin-runtime/src/router/runtime/constants.ts @@ -101,9 +101,15 @@ export const preResolvedFnStr = `function p(e,r){return void 0!==r?Promise.rejec * Abstracting the above logic to `mergeLoaderData`: * function mergeLoaderData (routeIdJsonStr, deferredKeyPromiseManifests) { const source = deferredKeyPromiseManifests.reduce(function(o, {key, routerDataFnName, routerDataFnArgs }) { - return {...o, [key]: _ROUTER_DATA[routerDataFnName](...routerDataFnArgs)} + const args = routerDataFnArgs.map(arg => { + if (arg === 'undefined' || arg === null) { + return undefined; + } + return JSON.parse(arg); + }); + return {...o, [key]: _ROUTER_DATA[routerDataFnName](...args)}; }, {}); Object.assign(_ROUTER_DATA.loaderData[routeIdJsonStr], source); }; */ -export const mergeLoaderDataStr = `function mergeLoaderData(a,e){e=e.reduce(function(a,{key:e,routerDataFnName:r,routerDataFnArgs:t}){return{...a,[e]:_ROUTER_DATA[r](...t)}},{});Object.assign(_ROUTER_DATA.loaderData[a],e)}`; +export const mergeLoaderDataStr = `function mergeLoaderData(e,n){const r=n.reduce((function(e,{key:n,routerDataFnName:r,routerDataFnArgs:a}){const t=a.map((e=>{if("undefined"!==e&&null!==e)return JSON.parse(e)}));return console.info("args",t),{...e,[n]:_ROUTER_DATA[r](...t)}}),{});Object.assign(_ROUTER_DATA.loaderData[e],r)}`;