-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(SSR): 修复多级路由嵌套时,需要合并多级路由serverLoader的数据 & feature(SSR): 支持ssr降级,客…
…户端兜底加载serverLoader数据 (#11894) * fix(SSR): 修复多级路由嵌套时,需要合并多级路由serverLoader的数据 feature(SSR): 支持ssr降级,客户端兜底加载serverLoader数据 * fix(SSR): 修复多级路由嵌套时,需要合并多级路由serverLoader的数据 feature(SSR): 支持ssr降级,客户端兜底加载serverLoader数据 * fix(SSR): 修复多级路由嵌套时,需要合并多级路由serverLoader的数据 feature(SSR): 支持ssr降级,客户端兜底加载serverLoader数据 * refactor: fix circular refer * test: add test case --------- Co-authored-by: 奇风 <[email protected]> Co-authored-by: fz6m <[email protected]>
- Loading branch information
1 parent
8f1cf2f
commit 6d45e02
Showing
6 changed files
with
120 additions
and
25 deletions.
There are no files selected for viewing
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,28 @@ | ||
import { IApi } from 'umi'; | ||
|
||
export default (api: IApi) => { | ||
// Only for mock, this hack is incomplete, do not use it in production environment | ||
api.onBeforeMiddleware(({ app }) => { | ||
app.use((req, res, next) => { | ||
if (req.query?.fallback) { | ||
// modify response | ||
const originWrite = res.write; | ||
// @ts-ignore | ||
res.write = function (chunk) { | ||
const isHtml = ~(res.getHeader('Content-Type') as string)?.indexOf( | ||
'text/html', | ||
); | ||
if (isHtml) { | ||
chunk instanceof Buffer && (chunk = chunk.toString()); | ||
chunk = chunk.replace( | ||
/window\.__UMI_LOADER_DATA__/, | ||
'window.__UMI_LOADER_DATA_FALLBACK__', | ||
); | ||
} | ||
originWrite.apply(this, arguments as any); | ||
}; | ||
} | ||
next(); | ||
}); | ||
}); | ||
}; |
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
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,26 @@ | ||
export function fetchServerLoader({ | ||
id, | ||
basename, | ||
cb, | ||
}: { | ||
id: string; | ||
basename?: string; | ||
cb: (data: any) => void; | ||
}) { | ||
const query = new URLSearchParams({ | ||
route: id, | ||
url: window.location.href, | ||
}).toString(); | ||
// 在有basename的情况下__serverLoader的请求路径需要加上basename | ||
const url = `${withEndSlash(basename)}__serverLoader?${query}`; | ||
fetch(url, { | ||
credentials: 'include', | ||
}) | ||
.then((d) => d.json()) | ||
.then(cb) | ||
.catch(console.error); | ||
} | ||
|
||
function withEndSlash(str: string = '') { | ||
return str.endsWith('/') ? str : `${str}/`; | ||
} |
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