可以参考这篇文章,《B君笔记SEO 方案自爆》。
总结成2点
mavonEditor 自动生成的格式是:#字母_编号
https://bjun.tech/blog/xphp/51#_4
e.replace(/[^\w\s]/gi, "").split(" ").join("_")
原理就是利用锚点跳到设定的文章位置。
了。
<a href="#_1">点我</a>
<h1 id="#_1">到这里</h1>
| 旧版route hash | 出现的情况 |
|---|---|
| 原本的路径 | https://bjun.tech/#/blog/xphp/51 |
| 点击标题 “#_4” 变成 | https://bjun.tech/#_4 |
| 结果 | 跳转到对应标题 |
其中第四点实我最不能忍的,记笔记写东西引用旧文章很正常,不能跳转到标题只能跳转到章节很不爽
a.addEventListener('click',function(a){
document.querySelector(e.getAttribute('href')).scrollIntoView(true)
a.preventDefault();
},false);
let id = that.$route.params.anchor
id && document.querySelector(id).scrollIntoView(true)
https://bjun.tech/blog/xphp/51#_4
window.location.hash && document.querySelector(window.location.hash).scrollIntoView(true)
转发地址
location ~* ^\/(blog)\/? {
rewrite ^/(.*)$ /index.php last;
}
index.php 主要是加载了vue的html文件
<?php
include .'/index.html';
http://127.0.0.4:8080 为 npm run serve 的监听地址和端口
publicPath 为 npm run serve 配置的 publicPath
location ~* ^\/(blog)\/? {
proxy_pass http://127.0.0.4:8080;
}
location ^~ /publicPath/ {
proxy_pass http://127.0.0.4:8080;
}
| 情况 | 是否解决 | 示例 |
|---|---|---|
| 点击跳转到指定位置 | Y | 原来的方案。 |
| 黏贴链接地址跳转到指定位置 | Y | https://bjun.tech/blog/xphp/51#_4。 |
| 内链引用 | Y | 《B君笔记SEO 方案自爆-反馈记录》。 |
| 前进后退 | Y | route hash 方案和 mavonEditor 目录跳转不兼容 |
hash部分也就是 /#/blog/1 是不会提交到服务器,无论是第一次还是后续不刷新跳转。
正因为这个原因,如果是hash模式,是使用不了SSR方案的。
history的path第一次进入是会提交到服务器的,这也是nginx路径可以进行重写的原因。
在php中 中可以通过$_SERVER[‘REQUEST_URI’]获取。
// url https://bjun.tech/blog/xphp/1?page=1
echo $_SERVER['REQUEST_URI'];
// 输出 /blog/xphp/1?page=1
无论是hash或者history,都是异步加载的页面,像百度这类搜索引擎,只能爬到一推js.
灵感来自 如何让搜索引擎抓取AJAX内容?
根据 $_SERVER[‘REQUEST_URI’]判断是否需要Noscript内容输出和解析出页面参数。
可以改的参数title,description,打印页面内容的noscript
<!DOCTYPE html>
<html lang="">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>{% titile %}</title>
<meta name="description" content="{% description %}">
</head>
<body>
<div id="app"></div>
<noscript>
{% noscript %}
</noscript>
</body>
</html>
按自己定义的规则解析路径,获取页面内容,进行替换,以下为简化的代码
//获取 title、description和noscript内容
$title = 'title';
$description = 'description';
$noscript = 'noscript 内容';
$callback = function($content){
return strtr($content,['{% titile %}'=>$title,'{% description %}'=>$description,'{% noscript %}'=>$noscript]);
};
ob_start($callback);
include .'/index.html';
ob_end_flush();
该方案才更新上了不久,记2021年10月3日
后续后变动再更新