Skip to content

Commit

Permalink
fix: 修复静态资源不存在时返回 notFound 页面的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
zhongzhi107 committed Nov 19, 2019
1 parent 119e8b6 commit d6f2f53
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 26 deletions.
48 changes: 26 additions & 22 deletions examples/with-custom-document/src/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,29 @@
*/

export default ({
head, cssTags, jsTags, markup, state
}) => `<!doctype html>
<html ${head.htmlAttributes.toString()}>
<head>
<meta charset="utf-8" />
${head.title.toString()}
${head.meta.toString()}
${head.link.toString()}
${head.style.toString()}
${head.script.toString()}
${head.noscript.toString()}
${cssTags}
</head>
<body ${head.bodyAttributes.toString()}>
<h1>with-custom-document</h1>
<div id="root">${markup}</div>
<script>
window.INITIAL_STATE=${state};
</script>
${jsTags}
</body>
</html>`;
head = {},
markup = '',
state = '{}',
styleTags = '',
scriptTags = ''
}) => {
const print = (key) => {
if (head[key] && typeof head[key].toString === 'function') {
return head[key].toString();
}
return '';
};

return `<!doctype html>
<html ${print('htmlAttributes')}>
<head>
<meta charset="utf-8" />
${print('title') + print('meta') + print('link') + process.env.HEADER_STYLES + print('style') + print('script') + styleTags}
</head>
<body ${print('bodyAttributes')}>
<h1>with-custom-document</h1>
<div id="root">${markup}</div>
<script>window.INITIAL_STATE=${state};</script>${scriptTags}
</body>
</html>`;
};
14 changes: 10 additions & 4 deletions src/runtime/createServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,16 @@ server
const notFound = branch[branch.length - 1].route.component.name === 'NotFound';

if (notFound) {
const html = document({
markup: renderToString(<NotFound location={{ pathname }} />)
});
res.status(200).end(html);
// 只有网页类请求才走自定义404页面
if (/html/.test(req.get('accept'))) {
const html = document({
markup: renderToString(<NotFound location={{ pathname }} />)
});
res.status(200).end(html);
} else {
// 非网页类请求直接返回404
res.status(404).end();
}
} else {
let initialProps = {};
if (ssr) {
Expand Down

0 comments on commit d6f2f53

Please sign in to comment.