矿石收音机论坛,史大平,三台
说了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.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
最终效果
等爬虫更新后就搜到我们的文章了
写在最后
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
VUE+elementui组件在table-cell单元格中绘制微型echarts图
Vue通过getAction的finally来最大程度避免影响主数据呈现问题
vue 路由懒加载中给 Webpack Chunks 命名的方法
网友评论