自定义 404
页面找不到时,服务器按这个顺序取 404 页:
.space/template/404.html- 平台默认 404 页
只有 .space/template/404.html 会被当作 404 页,根目录的 404.html 不生效。404 页会以 404 状态返回,不会被当成正常页面。
例子
把下面这个文件放到 .space/template/404.html:
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>404 · 页面不存在</title>
<style>
body { margin: 0; min-height: 100vh; display: grid; place-items: center; font: 16px/1.6 system-ui, sans-serif; }
a { color: inherit; }
</style>
</head>
<body>
<main>
<h1>页面不存在</h1>
<p><a href="/">返回首页</a></p>
</main>
</body>
</html>
相对路径
放在 .space/template/404.html 时,404 页实际由 /.space/template/404.html 提供。页面里的相对路径(./style.css、img/a.png)会相对 /.space/template/ 解析,和访客请求的路径无关。
所以 404 页里的链接和资源建议都用绝对路径(/style.css、/docs/),或者把资源放在 .space/template/ 下面再用相对路径。
静态生成器
很多静态生成器会在构建产物根目录生成 404.html(Docusaurus 默认就有)。平台不会把它当作 404 页,要用的话把它放到 .space/template/404.html,资源路径按上面「相对路径」一节处理。
.space 不对外时
如果站点配置里把 exposeSpace 设成 false,.space/ 下除 404 模板外都不对外。404 模板本身仍然能被访客看到,因为它就是 404 页。
延伸:站点配置