目录

①对于更成熟的网站,简单的index.html的入口文件的seo已经无法满足,需要在商品详情不同商品被搜索时赋予不同的title和description。

②通过设置站点所有页面都新增Canonical标签,指定规范链接地址给谷歌并规避联盟的重复内容页面。

③产品结构化微数据


①对于更成熟的网站,简单的index.html的入口文件的seo已经无法满足,需要在商品详情不同商品被搜索时赋予不同的title和description。

<!-- src/views/main/goods/goods-details/goods-details.vue -->
<template><!-- 模板部分保持不变 -->
</template><script>
// ... 其他导入保持不变export default {name: "goods-details",// ... 其他配置保持不变data() {return {// ... 其他数据保持不变originalMetaTags: {} // 保存原始meta标签}},// ... 其他配置保持不变methods: {// 设置产品页面的SEO信息setProductSEO(dataInfo) {if (!dataInfo) return;// 保存原始的meta标签信息(只在第一次调用时保存)if (!this.originalMetaTags.title) {this.originalMetaTags.title = document.title;this.originalMetaTags.description = this.getMetaContent('name', 'description');this.originalMetaTags['og:title'] = this.getMetaContent('property', 'og:title');this.originalMetaTags['og:description'] = this.getMetaContent('property', 'og:description');this.originalMetaTags['twitter:title'] = this.getMetaContent('name', 'twitter:title');this.originalMetaTags['twitter:description'] = this.getMetaContent('name', 'twitter:description');}// 获取产品标题const productTitle = dataInfo.subject || '';// 设置新的页面标题const newTitle = `Buy ${productTitle} | Hipobuy`;document.title = newTitle;// 设置新的meta descriptionconst newDescription = `Find the perfect ${productTitle} on Taobao or 1688? Hipobuy is your trusted China shopping agent. Start shopping now!`;// 更新所有meta标签this.updateMetaTag('name', 'description', newDescription);this.updateMetaTag('property', 'og:title', newTitle);this.updateMetaTag('property', 'og:description', newDescription);this.updateMetaTag('name', 'twitter:title', newTitle);this.updateMetaTag('name', 'twitter:description', newDescription);},// 获取meta标签内容的辅助方法getMetaContent(attribute, value) {const metaTag = document.querySelector(`meta[${attribute}="${value}"]`);return metaTag ? metaTag.getAttribute('content') : '';},// 更新meta property标签updateMetaProperty(property, content) {let metaTag = document.querySelector(`meta[property="${property}"]`);if (metaTag) {metaTag.setAttribute('content', content);} else {metaTag = document.createElement('meta');metaTag.setAttribute('property', property);metaTag.content = content;document.head.appendChild(metaTag);}},// 更新meta标签的通用方法updateMetaTag(attribute, value, content) {let metaTag = document.querySelector(`meta[${attribute}="${value}"]`);if (metaTag) {metaTag.setAttribute('content', content);} else {metaTag = document.createElement('meta');metaTag.setAttribute(attribute, value);metaTag.content = content;document.head.appendChild(metaTag);}},// 完整恢复原始meta标签(页面销毁时调用)restoreOriginalMetaTags() {// 恢复titleif (this.originalMetaTags.title) {document.title = this.originalMetaTags.title;}// 恢复descriptionif (this.originalMetaTags.description !== undefined) {const descriptionMeta = document.querySelector('meta[name="description"]');if (descriptionMeta) {if (this.originalMetaTags.description) {descriptionMeta.setAttribute('content', this.originalMetaTags.description);} else {descriptionMeta.remove(); // 如果原来没有,就删除添加的}} else if (this.originalMetaTags.description) {// 如果原来有但现在没有,就重新创建const meta = document.createElement('meta');meta.name = 'description';meta.content = this.originalMetaTags.description;document.head.appendChild(meta);}}// 恢复og:titlethis.restoreMetaTag('property', 'og:title', this.originalMetaTags['og:title']);// 恢复og:descriptionthis.restoreMetaTag('property', 'og:description', this.originalMetaTags['og:description']);// 恢复twitter:titlethis.restoreMetaTag('name', 'twitter:title', this.originalMetaTags['twitter:title']);// 恢复twitter:descriptionthis.restoreMetaTag('name', 'twitter:description', this.originalMetaTags['twitter:description']);},// 恢复单个meta标签的辅助方法restoreMetaTag(attribute, value, originalContent) {const metaTag = document.querySelector(`meta[${attribute}="${value}"]`);if (metaTag) {if (originalContent) {metaTag.setAttribute('content', originalContent);} else {metaTag.remove(); // 如果原来没有,删除添加的标签}} else if (originalContent) { // 如果原来有但现在没有,重新创建const meta = document.createElement('meta');meta.setAttribute(attribute, value);meta.content = originalContent;document.head.appendChild(meta);}},getQueryProductDetail(force) {this.loading = true;queryProductDetail({spuNo: this.id,refresh: !!force ? 1 : 0,channel: this.channel,activityCode: this.activityCode}).then((res) => {if (res.code == 1010) {this.loading = false;this.$refs.tipsPopRef.open(res.message);} else if (res.code == 200) {if (res.result.productGrayscale) {this.$refs.grayPopRef.open(res.result.productGrayscaleName)}this.loading = false;this.dataInfo = res.result;this.dataInfo.description = this.dataInfo.description.replace(/\s*href="[^"]*"/gi, '');// 设置产品SEO信息——getQueryProductDetail此请求只添加了这个设置,其余不需改动this.setProductSEO(this.dataInfo);if (this.dataInfo.channel == 'TAOBAO') {detailPointBtn(this.$route.params.spuNo, 'EXP', 'buy_icon');detailPointBtn(this.$route.params.spuNo, 'EXP', 'addcart_icon');}if (res.result.spuActivityVO) {if (this.$analytics) {this.$analytics.logEvent('pt_1001');}}}}).catch(() => {this.loading = false;})},// ... 其他方法保持不变},beforeDestroy() {// 恢复原始的meta标签this.restoreOriginalMetaTags();}
}
</script><!-- 样式部分保持不变 -->

本地环境自测以html的标签为准即可;

②通过设置站点所有页面都新增Canonical标签,指定规范链接地址给谷歌并规避联盟的重复内容页面。

处理带有URL参数的页面

场景:电商网站的产品页面可能会因为不同的筛选条件(如颜色、尺寸、排序方式等)而生成带有不同参数的URL。
示例:https://example.com/product?id=123&color=blue
https://example.com/product?id=123&size=large
解决方案:在所有带参数的页面中,使用Canonical标签指向主要的产品页面,如:

<link rel="canonical" href="https://example.com/product?id=123" />

分页内容

场景:文章列表或产品列表页面经常会分页(如第1页、第2页)
示例:https://example.com/blog?page=1
https://example.com/blog?page=2
解决方案:可以让每个分页页面的Canonical标签指向第一页,或者让每页的Canonical标签指向自身。<link rel="canonical" href="https://example.com/blog?page=1" />

内容分发与跟踪参数

场景:网站在不同渠道(如社交媒体)分发内容,带有UTM等跟踪参数
示例:https://example.com/blog/post?utm_source=facebook
解决方案:使用Canonical标签指向无参数的主要URL.

<link rel="canonical" href="https://example.com/blog/post" />

下面是我的实践实例动态设置所有页面:

<!-- public/index.html -->
<!DOCTYPE html>
<html lang="en-US">
<head><!-- 其他head内容保持不变 --><!-- 添加基础canonical标签 --><link rel="canonical" href="https://hipobuy.com/" /><!-- 其他head内容保持不变 -->
</head>
<body><div id="app"></div>
</body>
</html>
// 在 src/router/index.js 中添加
router.afterEach((to) => {// 延迟执行确保DOM已更新setTimeout(() => {const canonicalLink = document.querySelector('link[rel="canonical"]');if (canonicalLink) {// 获取基础URL,移除查询参数和hashconst baseUrl = window.location.origin;const pathname = to.path;const canonicalUrl = baseUrl + pathname;canonicalLink.href = canonicalUrl;}}, 100);
});

特殊情况:电商网站中如果路由配置了参数可以用这种配置,不过具体在控制台查看link标签,我发现以上路由设置已经足够,可以把参数也带进canonical标签,如果不行参考以下:

<!-- 在 src/views/main/goods/goods-details/goods-details.vue 的 methods 中添加 -->
methods: {// ... 其他方法保持不变// 添加这个方法来设置产品页的canonicalsetProductCanonical() {const canonicalLink = document.querySelector('link[rel="canonical"]');if (canonicalLink) {// 构建产品页的标准canonical URLconst channelMap = {"0": "1688","1": "taobao","2": "weidian","3": "jd"};const channelName = channelMap[this.channel] || this.channel.toLowerCase();const canonicalUrl = `https://hipobuy.com/product/${channelName}/${this.id}`;canonicalLink.href = canonicalUrl;}},// 在 getQueryProductDetail 方法的成功回调中调用getQueryProductDetail(force) {// ... 其他代码保持不变queryProductDetail({// ... 参数保持不变}).then((res) => {// ... 其他成功处理保持不变if (res.code == 200) {// ... 其他处理保持不变// 设置产品SEO信息this.setProductSEO(this.dataInfo);// 添加这一行来设置产品页canonicalthis.setProductCanonical();// ... 其他处理保持不变}}).catch(() => {this.loading = false;})}
}

③产品结构化微数据

这是代码效果示例,不可以直接添加在html:

<html><head><title>Executive Anvil</title><script type="application/ld+json">{"@context": "https://schema.org/","@type": "Product","name": "Executive Anvil","image": ["https://example.com/photos/1x1/photo.jpg","https://example.com/photos/4x3/photo.jpg","https://example.com/photos/16x9/photo.jpg"],"description": "Sleeker than ACME's Classic Anvil, the Executive Anvil is perfect for the business traveler looking for something to drop from a height.","sku": "0446310786","mpn": "925872","brand": {"@type": "Brand","name": "ACME"},"review": {"@type": "Review","reviewRating": {"@type": "Rating","ratingValue": 4,"bestRating": 5},"author": {"@type": "Person","name": "Fred Benson"}},"aggregateRating": {"@type": "AggregateRating","ratingValue": 4.4,"reviewCount": 89},"offers": {"@type": "AggregateOffer","offerCount": 5,"lowPrice": 119.99,"highPrice": 199.99,"priceCurrency": "USD"}}</script></head><body></body>
</html>

以下才是js效果代码:

<!-- src/views/main/goods/goods-details/goods-details.vue -->
<script>
export default {name: "goods-details",// ... 其他代码保持不变data() {return {// ... 其他数据保持不变structuredDataScript: null // 保存结构化数据script元素}},methods: {// 生成并插入结构化数据generateStructuredData(dataInfo) {if (!dataInfo) return;// 移除已存在的结构化数据this.removeStructuredData();// 构建基础产品信息const structuredData = {"@context": "https://schema.org/","@type": "Product","name": dataInfo.subject || '',"image": this.getProductImages(dataInfo),"description": this.getProductDescription(dataInfo),"sku": dataInfo.spuNo || '',"mpn": dataInfo.spuNo || dataInfo.id || '',"brand": this.getProductBrand(dataInfo),"offers": this.getProductOffers(dataInfo)};// 添加评分信息const ratingInfo = this.getProductRating(dataInfo);if (ratingInfo) {structuredData.aggregateRating = ratingInfo;}// 添加评论信息(如果有)const reviews = this.getProductReviews(dataInfo);if (reviews && reviews.length > 0) {structuredData.review = reviews;}// 创建script标签并插入到head中this.structuredDataScript = document.createElement('script');this.structuredDataScript.type = 'application/ld+json';this.structuredDataScript.textContent = JSON.stringify(structuredData, null, 2);// 添加标识便于清理this.structuredDataScript.setAttribute('data-structured-data', 'product');document.head.appendChild(this.structuredDataScript);},// 获取产品图片列表getProductImages(dataInfo) {const images = [];// 主图if (dataInfo.mainImg) {images.push(dataInfo.mainImg);}// 图片列表if (dataInfo.imageList && Array.isArray(dataInfo.imageList)) {images.push(...dataInfo.imageList);}// 去重return [...new Set(images)].slice(0, 10); // 最多10张图片},// 获取产品描述getProductDescription(dataInfo) {if (!dataInfo.description) return '';// 移除HTML标签const tmp = document.createElement('div');tmp.innerHTML = dataInfo.description;return tmp.textContent || tmp.innerText || '';},// 获取品牌信息getProductBrand(dataInfo) {const channelNames = {"0": "1688","1": "Taobao","2": "Weidian","3": "JD","1688": "1688","TAOBAO": "Taobao","WEIDIAN": "Weidian","JD": "JD"};const brandName = dataInfo.brandName ||channelNames[dataInfo.channel] ||channelNames[this.channel] ||"Chinese E-commerce Platform";return {"@type": "Brand","name": brandName};},// 获取产品报价信息getProductOffers(dataInfo) {const price = this.getProductPrice(dataInfo);const currency = dataInfo.currencyCode || "CNY";const availability = this.getAvailabilityStatus(dataInfo);return {"@type": "Offer","url": window.location.href,"priceCurrency": currency,"price": price,"availability": availability,"priceValidUntil": this.getPriceValidUntil(),"seller": {"@type": "Organization","name": "Hipobuy"}};},// 获取产品价格getProductPrice(dataInfo) {if (dataInfo.price) {return dataInfo.priceCNY ? dataInfo.priceCNY : dataInfo.price;} else if (dataInfo.saleInfo) {if (dataInfo.saleInfo.priceRangeList && dataInfo.saleInfo.priceRangeList.length > 0) {return dataInfo.saleInfo.priceRangeList[0].priceCNY ?dataInfo.saleInfo.priceRangeList[0].priceCNY :dataInfo.saleInfo.priceRangeList[0].price;} else if (dataInfo.saleInfo.priceRanges && dataInfo.saleInfo.priceRanges.length > 0) {return dataInfo.saleInfo.priceRanges[0].priceCNY ?dataInfo.saleInfo.priceRanges[0].priceCNY :dataInfo.saleInfo.priceRanges[0].price;}}return "0";},// 获取产品评分信息getProductRating(dataInfo) {if (dataInfo.score || dataInfo.commentNum) {return {"@type": "AggregateRating","ratingValue": dataInfo.score || 0,"reviewCount": dataInfo.commentNum || 0,"bestRating": 5,"worstRating": 1};}return null;},// 获取产品评论信息getProductReviews(dataInfo) {// 如果有具体的评论数据,可以在这里处理// 目前返回空数组return [];},// 获取库存状态getAvailabilityStatus(dataInfo) {// 根据库存信息判断可用性if (dataInfo.stock !== undefined) {return dataInfo.stock > 0 ? "https://schema.org/InStock" : "https://schema.org/OutOfStock";}// 根据状态判断if (dataInfo.status !== undefined) {switch (dataInfo.status) {case 'ON_SALE':case 'AVAILABLE':return "https://schema.org/InStock";case 'SOLD_OUT':case 'OFF_SALE':return "https://schema.org/OutOfStock";default:return "https://schema.org/InStock";}}return "https://schema.org/InStock"; // 默认有库存},// 获取价格有效期getPriceValidUntil() {const date = new Date();date.setMonth(date.getMonth() + 3); // 价格有效期3个月return date.toISOString().split('T')[0];},// 移除结构化数据removeStructuredData() {if (this.structuredDataScript) {document.head.removeChild(this.structuredDataScript);this.structuredDataScript = null;}},},// 组件销毁时移除结构化数据beforeDestroy() {this.removeStructuredData(); // 移除结构化数据}
}
</script>
          // 生成结构化数据---------在调用商品详情的接口内调用并传递res.dataInfothis.generateStructuredData(this.dataInfo);

执行完之后可以在控制台head标签内查找是否展现:

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.pswp.cn/pingmian/93323.shtml
繁体地址,请注明出处:http://hk.pswp.cn/pingmian/93323.shtml
英文地址,请注明出处:http://en.pswp.cn/pingmian/93323.shtml

如若内容造成侵权/违法违规/事实不符,请联系英文站点网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

ROS move_base 混合功能导航 RealSense D435i + 3D 点云地图 + 楼层切换 + 路径录制 + 路径规划

Mixed-Navigation 这个博客也是记录我们的一个开源项目&#xff0c;其作用是混合功能导航。由于现有的 Fast-Lio-Localization 只实现了定位功能&#xff0c;但对于路径规划和楼层切换没有具体实现&#xff0c;因此我们开出了这个仓库作为参考。该仓库的核心功能如下&#xff…

初识c语言————宏定义和调用

目录&#xff1a;一.不带参数的宏二.带参数宏一.不带参数的宏不带参数的宏是指用#define指令定义的简单文本替换规则&#xff0c;它没有参数列表&#xff0c;直接替换标识符为相应的文本其一般形式为&#xff1a;#define 宏名 文本例如&#xff1a;#define pi 3.14这个代…

数据结构:满二叉树 (Full Binary Tree) 和 完全二叉树 (Complete Binary Tree)

目录 重要的术语澄清 完美二叉树 (Perfect Binary Tree) 完全二叉树 (Complete Binary Tree) 大比拼 (Comparison) 相互关系的第一性推导 我们来深入探讨两种在算法中非常重要的、具有特定“形状”的二叉树&#xff1a;满二叉树 (Full Binary Tree) 和 完全二叉树 (Compl…

OpenJDK 17的C1和C2编译器实现中,方法返回前插入安全点(Safepoint Poll)的机制

OpenJDK 17 JIT编译器堆栈分析-CSDN博客 在OpenJDK 17的C1和C2编译器实现中&#xff0c;方法返回前插入安全点&#xff08;Safepoint Poll&#xff09;的机制主要涉及以下关键步骤&#xff0c;结合源代码进行分析&#xff1a; 1. 安全点轮询桩&#xff08;Safepoint Poll Stu…

【论文笔记】STORYWRITER: A Multi-Agent Framework for Long Story Generation

论文信息 论文标题&#xff1a;StoryWriter: A Multi-Agent Framework for Long Story Generation 论文作者&#xff1a;Haotian Xia, Hao Peng et al. (Tsinghua University) 论文链接&#xff1a;https://arxiv.org/abs/2506.16445 代码链接&#xff1a;https://github.com/…

Cohere 开发企业级大型语言模型(LLM)

Cohere 是一家专注于开发企业级大型语言模型&#xff08;LLM&#xff09;的公司。该公司推出了一系列名为 “Command” 的模型&#xff0c;其中最强大的 “Command A” 于今年三月首次亮相 Cohere 还提供嵌入模型&#xff0c;这是一种将文件转化为神经网络可以理解的紧凑数值形…

Rust Web框架Axum学习指南之入门初体验

一、准备阶段 确保已经安装 rust&#xff0c;开发环境使用 vscode 或者 rustrover 都可以。接着就可以创建项目&#xff0c;通过编辑器创建或者命令行创建都可以&#xff1a; cargo new axum-admin二、添加依赖 添加依赖如下&#xff1a; [package] name "axum-admin&quo…

autofit.js: 自动调整HTML元素大小的JavaScript库

&#x1f90d; 前端开发工程师、技术日更博主、已过CET6 &#x1f368; 阿珊和她的猫_CSDN博客专家、23年度博客之星前端领域TOP1 &#x1f560; 牛客高级专题作者、打造专栏《前端面试必备》 、《2024面试高频手撕题》、《前端求职突破计划》 &#x1f35a; 蓝桥云课签约作者、…

RocketMQ 命名服务器(NameServer)详解

&#x1f680; RocketMQ 命名服务器&#xff08;NameServer&#xff09;详解 NameServer 是 RocketMQ 架构中的轻量级路由发现服务&#xff0c;它不参与消息的收发&#xff0c;但承担着整个集群的“地址簿”和“导航系统”的关键角色。 理解 NameServer 的设计与工作原理&#…

代码随想录算法训练营四十三天|图论part01

深度优先搜索&#xff08;dfs&#xff09;理论基础 dfs就是可一个方向去搜直到尽头再换方向&#xff0c;换方向的过程就涉及到了回溯。 代码框架 因为dfs搜索可一个方向&#xff0c;并需要回溯&#xff0c;所以用递归的方式来实现是最方便的。 先来回顾一下回溯法的代码框架…

飞算JavaAI金融风控场景实践:从实时监测到智能决策的全链路安全防护

目录一、金融风控核心场景的技术突破1.1 实时交易风险监测系统1.1.1 高并发交易数据处理1.2 智能反欺诈系统架构1.2.1 多维度欺诈风险识别1.3 动态风控规则引擎1.3.1 风控规则动态管理二、金融风控系统效能升级实践2.1 风控模型迭代加速机制2.1.1 自动化特征工程结语&#xff1…

Vue 组件二次封装透传slots、refs、attrs、listeners

最近写了一个开源项目&#xff0c;有些地方需要二次封装&#xff0c;需要透传一些数据&#xff0c;需要注意的是ref&#xff0c;我这里使用俩种方式间接传递ref&#xff0c;具体如下&#xff1a; 使用&#xff1a; import VideoPlayer from ./index.jsVue.use(VideoPlayer)inde…

介绍大根堆小根堆

文章目录一、核心定义与结构特性示例&#xff08;以“数组存储堆”为例&#xff09;二、堆的两个核心操作1. 插入操作&#xff08;以小根堆为例&#xff09;2. 删除极值操作&#xff08;以小根堆为例&#xff0c;删除根节点的最小值&#xff09;三、小根堆 vs 大根堆&#xff1…

【Html网页模板】赛博朋克数据分析大屏网页

目录专栏导读✨ 项目概述&#x1f3a8; 设计理念&#x1f6e0;️ 技术架构核心技术栈设计模式&#x1f3af; 核心功能1. 视觉效果系统&#x1f308; 色彩体系2. 数据可视化模块&#x1f4ca; 主图表系统&#x1f4c8; 性能监控面板3. 实时数据流系统⚡ 数据流动画&#x1f4ca;…

【经典上穿突破】副图/选股指标,双均线交叉原理,对价格波动反应灵敏,适合捕捉短期启动点

【经典上穿突破】副图/选股指标&#xff0c;双均线交叉原理&#xff0c;对价格波动反应灵敏&#xff0c;适合捕捉短期启动点 这是一款结合短线与中线信号的趋势跟踪指标&#xff0c;通过双均线交叉原理捕捉股价突破时机&#xff0c;适用于个股分析和盘中选股。 核心功能模块&…

RK3568 NPU RKNN(四):RKNN-ToolKit2性能和内存评估

文章目录1、前言2、目标3、完整的测试程序4、运行测试程序5、程序拆解6、总结1、前言 本文仅记录本人学习过程&#xff0c;不具备教学指导意义。 2、目标 使用野火提供的示例程序&#xff0c;体验 RKNN-ToolKit2 在PC端使用连板推理&#xff0c;进行性能和内存评估。 3、完…

ASP.NET 上传文件安全检测方案

一、前端初步过滤&#xff08;防误操作&#xff09;<!-- HTML部分 --><input type"file" id"fileUpload" accept".jpg,.png,.pdf,.docx" /><button onclick"validateFile()">上传</button><script>func…

Nacos Server 3.0.x安装教程

前言 注&#xff1a; 1.Nacos Server 3.0.x 要求 JDK版本不低于17。 2.Nacos 2.2.0 及以上版本需要 Java 11 或更高版本。 3.Java 8&#xff0c;需要下载 Nacos 2.1.x 及以下版本 JDK17安装 JDK官方下载地址&#xff1a;Oracle官网JDK下载地址 JDK17&#xff1a;JDK17下载地…

【数据库干货】六大范式速记

1NF、2NF、3NF、BCNF、4NF、5NF都是数据库设计中的范式&#xff08;Normalization&#xff09;&#xff0c;用于确保数据库中的数据结构尽可能地减少冗余&#xff0c;避免更新异常、插入异常、删除异常等问题&#xff0c;从而提高数据的存储效率和一致性。 本篇文章简单讲解下各…

Java开发主流框架搭配详解及学习路线指南

文章目录一、前言&#x1f517;二、主流Java框架搭配2.1 Spring Boot MyBatis-Plus Spring Cloud2.2 Spring Boot Spring Data JPA Spring Cloud2.3 Quarkus/Vert.x (响应式编程栈)三、技术选型建议四、Java学习路线指南阶段1&#xff1a;Java基础 (4-6周)阶段2&#xff1a…