- 上一篇说了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'
}),
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更好
- 关于首页渲染问题推荐是用骨架屏