浅谈在不使用ssr的情况下解决Vue单页面SEO问题(2)
作者:EEEEEZ 时间:2024-04-26 17:37:39
上一篇说了vue单页面解决解决SEO的问题
只是用php预处理了meta标签
但是依然没有内容填充,所以对于内容抓取依然有些乏力,只是解决了从无到有的问题
那接下来可以更进一步的预填充内容了
预填充内容
这里依然使用php来实现
首先在php中拉取需要填充的数据,列表或是具体内容
修改拉取数据部分
$urlExp = explode('/',$_SERVER['REQUEST_URI']);
if(count($urlExp)>2 && $urlExp[1] == 'article'){
//文章页拉取内容
$ret = @json_decode(http_Req('http://127.0.0.1/api/Blog/getsinglelist',['tuid'=>$urlExp[2]],'POST'),true);
if($ret){
$valKeywords = $ret['info'][0]['tt'].','.$valKeywords;
$valDescription = $ret['info'][0]['txt'].' - '.$valTitle.','.$valDescription;
$valTitle = $ret['info'][0]['tt'].' - '.$valTitle;
$info = $ret['info'][0]['info'];
$textData = @file_get_contents("你的文章路径") ?? $valDescription;
}else{
$textData='none';
}
}
if(!$textData){
//列表页拉取列表
$ret = @json_decode(http_Req('http://127.0.0.1/api/Blog/getlist',['page'=>1,'type'=>0],'POST'),true);
if($ret){
$textData = '';
foreach ($ret['info'] as $key=>$val) {
$textData.='标题:'.$val['tt'].'.';
$textData.='描述:'.$val['txt'].'.';
$textData.='创建时间:'.$val['ctime'].'.';
$textData.='浏览次数:'.$val['fl'].'.';
}
}
}
然后在html部分输出相关内容
在body下放一个div,并且隐藏掉他
<div class="pre-view" style="position:absolute;z-index: -99999;opacity: 0;top: -9999px;left: -9999px">
<?php echo $textData; ?>
</div>
这样爬虫就可以抓取到我们的内容并且不影响前端渲染
优化vue构建
之前的构建是在构建完成后修改html为php,有点蠢
这里改下webpack的配置就好了
修改 build/webpack.prod.conf
new HtmlWebpackPlugin({
filename: config.build.index,
//这里改为index.php
template: 'index.php',
inject: true,
minify: {
removeComments: true,
collapseWhitespace: true,
removeAttributeQuotes: true
// more options:
// https://github.com/kangax/html-minifier#options-quick-reference
},
// necessary to consistently work with multiple chunks via CommonsChunkPlugin
chunksSortMode: 'dependency'
}),
修改 config/index.js
build: {
// Template for index.html
// 这里改为index.php
index: path.resolve(__dirname, '../dist/index.php'),
// Paths
assetsRoot: path.resolve(__dirname, '../dist'),
assetsSubDirectory: 'static',
assetsPublicPath: 'http://cdn.linkvall.cn/',
productionSourceMap: true,
devtool: '#source-map',
productionGzip: false,
productionGzipExtensions: ['js', 'css'],
bundleAnalyzerReport: true
}
这样构建时候的入口文件就变成index.php,构建完成的页面入口也为index.php
最终效果
等爬虫更新后就搜到我们的文章了
写在最后
目前还是用php来实现主要是实现起来比较简单,对于像我一样后端是php的比较友好
如果再使用node去监听个端口的话需要额外开销和额外的精力去维护
如果后端是纯node的当然用node或者直接配置个SSR更好
关于首页渲染问题推荐是用骨架屏
来源:https://segmentfault.com/a/1190000016946397
标签:Vue,单页面,SEO
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Dreamweaver 网页编辑常用表现的实现方法
2010-10-20 20:05:00
如何创建支持FILESTREAM的数据库示例探讨
2024-01-26 15:30:13
详解PHP结构型设计模式之桥接模式Bridge Pattern
2023-05-25 06:58:55
Python 内置函数complex详解
2022-02-16 03:57:41
python元组简单介绍
2023-07-31 18:06:12
K8ssandra入门教程之Linux上部署K8ssandra到Kubernetes的过程
2022-04-02 03:12:59
![](https://img.aspxhome.com/file/2023/8/120548_0s.jpg)
CSS Expression 的优化
2009-09-24 13:27:00
Flask框架单例模式实现方法详解
2023-01-24 17:04:55
对Python 检查文件名是否规范的实例详解
2021-11-13 02:04:23
利用Python复制文件的9种方法总结
2022-06-25 15:00:27
![](https://img.aspxhome.com/file/2023/6/70656_0s.png)
python numpy库之如何使用matpotlib库绘图
2023-02-07 22:22:24
![](https://img.aspxhome.com/file/2023/2/104392_0s.png)
MYSQL SERVER收缩日志文件实现方法
2024-01-26 15:14:29
![](https://img.aspxhome.com/file/2023/8/104078_0s.png)
python3+PyQt5+Qt Designer实现扩展对话框
2023-08-01 00:16:45
![](https://img.aspxhome.com/file/2023/6/91836_0s.png)
python3写的简单本地文件上传服务器实例
2023-08-23 02:50:58
javascript图片预加载
2009-08-30 12:47:00
删除重复数据的算法
2024-05-13 09:36:14
PyQt5每天必学之QSplitter实现窗口分隔
2021-12-26 16:33:48
Pytorch.nn.conv2d 过程验证方式(单,多通道卷积过程)
2021-12-24 13:28:27
![](https://img.aspxhome.com/file/2023/0/97720_0s.jpg)
canvas时钟效果
2024-05-05 09:14:22
![](https://img.aspxhome.com/file/2023/7/129577_0s.png)
Python 中将二进制转换为整数的多种方法
2023-01-11 17:30:53