SEO优化 prerender-spa-plugin工具使用 踩坑记录

news2024/9/17 8:52:22
  1. 安装prerender-spa-plugin
yarn add prerender-spa-plugin
或
npm install prerender-spa-plugin

初始配置 后面记录踩的坑

  1. 配置路由

const routes = [
  {
    path: '/',
    redirect: {
      path: '/HomeView'
    },
  },
  {
    path: '/home',
    redirect: {
      path: '/HomeView'
    },
  },
  { 
    path: '/HomeView',
    component: HomeView
  },
  {
    path: '/CompanyProfile',
    component: CompanyProfile
  },
  {
    path: '/ConsultationChannel',
    component: ConsultationChannel
  },
  {
    path: '/IndustryIntroduction',
    name: 'IndustryIntroduction',
    component: IndustryIntroduction,
  },
  {
    path: '/ProductIntroduction',
    name: 'ProductIntroduction',
    component: ProductIntroduction,
  },
  {
    path: '/ProductsServices',
    name: 'ProductsServices',
    component: ProductsServices,
  },
  {
    path: '/CompanyCulture',
    name: 'CompanyCulture',
    component: CompanyCulture,
  },
  {
    
    path: '/CompanyAbout',
    name: 'CompanyAbout',
    component: CompanyAbout,
  },
  {
    path: '/CompanyIntroduction',
    name: 'CompanyIntroduction',
    component: CompanyIntroduction,
  },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes,
})
  1. vue.config.js配置
const PrerenderSPAPlugin = require('prerender-spa-plugin');
const Renderer = PrerenderSPAPlugin.PuppeteerRenderer;

module.exports = {
  // 服务器环境production  本地环境development 
  // "../"  其他配置无法打包
  publicPath: process.env.NODE_ENV === "production" ? "../" : "./",
  // 在npm run build 或 yarn build 时 ,生成文件的目录名称(要和baseUrl的生产环境路径一致)(默认dist)
  outputDir: 'home',
  configureWebpack: config => {
    if (process.env.NODE_ENV !== 'production') return;
    return {
      plugins: [
        new PrerenderSPAPlugin({
          // 生成文件的路径,这里使用了相对路径
          staticDir: path.join(__dirname, 'home'),
          // 需要进行预渲染的路由,这里需要根据你的实际路由配置来设置
          routes: [ '/', '/HomeView', '/CompanyProfile', '/ConsultationChannel', '/IndustryIntroduction', '/ProductIntroduction', '/ProductsServices', '/CompanyCulture', '/CompanyAbout', '/CompanyIntroduction'],
          // 渲染器配置,这里使用 Puppeteer 渲染
          renderer: new Renderer({
            inject: {
              foo: 'bar'
            },
            // 在渲染之前等待一定时间,以确保 SPA 中的异步操作完成
            renderAfterTime: 5000,
            headless: false,
            // 在 main.js 中 document.dispatchEvent(new Event('render-event')),两者的事件名称要对应上。
            renderAfterDocumentEvent: 'render-event'
          })
        })
      ]
    }
  }
}

遇到的问题

  1. publicPath配置为…/后, 在服务器静态资源和路由加载找不到子路径的目录,publicPath改为真实的子路径后无法build 不加publicPath静态文件访问存在问题

解决

此问题解决出处
解决方法

  publicPath: process.env.NODE_ENV === "production" ? '/home/' : "./",,
  outputDir: path.join(__dirname, "./home", "/home/"),
    configureWebpack: config => {
    if (process.env.NODE_ENV !== 'production') return;
    return {
      plugins: [
        new PrerenderSPAPlugin({
          // 生成文件的路径,这里使用了相对路径
          staticDir: path.join(__dirname, 'home/'),
          indexPath: path.join(__dirname, "home/home/index.html"), //重要
          // 需要进行预渲染的路由,这里需要根据你的实际路由配置来设置
          routes: [ '/', '/HomeView', '/CompanyProfile', '/ConsultationChannel', '/IndustryIntroduction', '/ProductIntroduction', '/ProductsServices', '/CompanyCulture', '/CompanyAbout', '/CompanyIntroduction'],
          // 渲染器配置,这里使用 Puppeteer 渲染
          renderer: new Renderer({
            inject: {
              foo: 'bar'
            },
            // 在渲染之前等待一定时间,以确保 SPA 中的异步操作完成
            renderAfterTime: 5000,
            headless: false,
            // 在 main.js 中 document.dispatchEvent(new Event('render-event')),两者的事件名称要对应上。
            renderAfterDocumentEvent: 'render-event'
          })
        })
      ]
    }
  }
  1. 升级到服务器后,在使用 prerender-spa-plugin 进行预渲染处理时,如果手动刷新 Vue 路由后路径末尾会出现 /,通常这是由于浏览器的默认行为导致的,特别是在使用 history 模式的 Vue 路由时比较常见。这种行为可能会影响到预期的路由匹配和渲染

解决

  • Vue Router 中设置 strict: true,这样在路由跳转时会自动去掉末尾的斜杠,避免不必要的重定向。
const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes,
})
  • 在路由的 beforeEnter 钩子中,可以检查并处理路径末尾的斜杠,确保它符合预期的路由结构。
const routes = [
  {
    path: '/',
    redirect: {
      path: '/HomeView'
    },
  },
  { 
    path: '/HomeView',
    component: HomeView,
    beforeEnter: (to, from, next) => {
      if (to.path.endsWith('/')) {
        next({ path: to.path.slice(0, -1) });
      } else {
        next();
      }
    }
  },
  {
    path: '/CompanyProfile',
    component: CompanyProfile,
    beforeEnter: (to, from, next) => {
      if (to.path.endsWith('/')) {
        next({ path: to.path.slice(0, -1) });
      } else {
        next();
      }
    }
  },
  {
    path: '/ConsultationChannel',
    component: ConsultationChannel,
    beforeEnter: (to, from, next) => {
      if (to.path.endsWith('/')) {
        next({ path: to.path.slice(0, -1) });
      } else {
        next();
      }
    }
  },
  {
    path: '/IndustryIntroduction',
    name: 'IndustryIntroduction',
    component: IndustryIntroduction,
    beforeEnter: (to, from, next) => {
      if (to.path.endsWith('/')) {
        next({ path: to.path.slice(0, -1) });
      } else {
        next();
      }
    }
  },
  {
    path: '/ProductIntroduction',
    name: 'ProductIntroduction',
    component: ProductIntroduction,
    beforeEnter: (to, from, next) => {
      if (to.path.endsWith('/')) {
        next({ path: to.path.slice(0, -1) });
      } else {
        next();
      }
    }
  },
  {
    path: '/ProductsServices',
    name: 'ProductsServices',
    component: ProductsServices,
    beforeEnter: (to, from, next) => {
      if (to.path.endsWith('/')) {
        next({ path: to.path.slice(0, -1) });
      } else {
        next();
      }
    }
  },
  {
    path: '/CompanyCulture',
    name: 'CompanyCulture',
    component: CompanyCulture,
    beforeEnter: (to, from, next) => {
      if (to.path.endsWith('/')) {
        next({ path: to.path.slice(0, -1) });
      } else {
        next();
      }
    }
  },
  {
    
    path: '/CompanyAbout',
    name: 'CompanyAbout',
    component: CompanyAbout,
    beforeEnter: (to, from, next) => {
      if (to.path.endsWith('/')) {
        next({ path: to.path.slice(0, -1) });
      } else {
        next();
      }
    }
  },
  {
    path: '/CompanyIntroduction',
    name: 'CompanyIntroduction',
    component: CompanyIntroduction,
    beforeEnter: (to, from, next) => {
      if (to.path.endsWith('/')) {
        next({ path: to.path.slice(0, -1) });
      } else {
        next();
      }
    }
  },
 ]
  1. 在以上问题处理完后,升级到服务器后,静态资源访问不到
资源访问不到
http://ip:port/home/static/css/app.e2f5f186.css

解决

打包生成home文件夹,在home文件夹下还有一个home文件夹,把这个里面的静态资源复制一份放在和它同级的目录下即可!

home/home下的文件夹,复制这些静态资源
home文件夹,复制后放在这个文件夹

自此目前遇到的所有问题都以解决

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

postgresql密码复杂度验证和有效期

前言 为了数据库安全以及应对等保测评等要求,我们需要设置密码复杂度。我们通过passwordcheck模块实现复杂度检测功能。 启用密码复杂度验证 找到自己安装pg库的配置文件目录,修改postgresql.conf vim postgresql.conf修改如下内容 shared_preload_…

2023版IDEA安装通义灵码屡遭挫败:重复尝试,安装依旧失败

目录 背景: 过程: 第一步: 第二步: 第三步: 安装成功: 总结: 通义灵码的优点: 背景: 小编使用的是2023版本IDEA,在安装通义灵码的时候出现了一件很让人头痛的问题,我在IEDA中的插件中心里面去下载,但是当我我安装的进度条加载完成之…

命令行创建git仓库

方法1:初始化自己的仓库 git init创建完成之后可以用ls -a查看是否存在.git文件 如果不想要git仓库,可以使用rm -rf .git删除仓库 方法2:克隆别人的仓库 git clone [http][http]是仓库网址 总体流程 可以看到文件分为四种状态&#xff0c…

windows无法打开添加打印机原因分析及解决方法

在日常办公和生活中,打印机是不可或缺的重要设备。然而,有时在添加打印机的过程中,经常会遇各种问题。今天有个小伙伴问我windows无法打开添加打印机怎么回事?今天就教大家windows无法打开添加打印机原因分析及解决方法。 添加打打…

氧传感器在码头油气回收船岸安全装置中的重要作用

随着全球对环境保护和安全生产要求的日益提升,石化码头的油气回收问题已成为行业关注的焦点。在汽油、航煤、苯、对二甲苯等油品和化学品的装船过程中,大量油气挥发不仅加剧了大气污染,还潜藏着对人体健康的严重威胁。因此,推广和…

芋道以开源之名行下作之事 恬不知耻 标榜自己开源 公开源码+sql 不用再加入知识星球

资源 链接: https://pan.baidu.com/s/1TeuxbAUfLQ5_BqMBF1kniQ?pwdcqud 提 取码: cqud 依次为后端、补充版的sql、前端 此文档内安装部署等一应俱全

天气预报的爬虫内容打印并存储用户操作

系统名称: 基于网络爬虫技术的天气数据查询系统文档作者:清馨创作时间:2024-7-29最新修改时间:2024-7-29最新版本号: 1.0 1.背景描述 该系统将基于目前比较流行的网络爬虫技术,对网站上(NowAPI…

数据结构之八大排序(上)

找往期文章包括但不限于本期文章中不懂的知识点: 个人主页:我要学编程(ಥ_ಥ)-CSDN博客 所属专栏:数据结构(Java版) 目录 排序的相关介绍 直接插入排序 希尔排序(缩小增量排序) 选择排序 …

Datawhale AI夏令营 AI+逻辑推理 Task2总结

Datawhale AI夏令营 AI逻辑推理 Task2总结 一、大语言模型解题方案介绍 1.1 大模型推理介绍 ​ 推理是建立在训练完成的基础上,将训练好的模型应用于新的、未见过的数据,模型利用先前学到的规律进行预测、分类和生成新内容,使得AI在实际应…

【Linux】3.Linux 指令大揭秘:常见八个指令的妙用(下)

欢迎来到 CILMY23 的博客 🏆本篇主题为:Linux 指令大揭秘:常见八个指令的妙用(下) 🏆个人主页:CILMY23-CSDN博客 🏆系列专栏:Python | C | C语言 | 数据结构与算法 | …

华为机试HJ76尼科彻斯定理

华为机试HJ76尼科彻斯定理 题目: 想法: 从题目可以找到规律,输出的第一个奇数为 ( 当前输入数值 − 1 ) 当前输入数值 1 (当前输入数值-1)当前输入数值1 (当前输入数值−1)当前输入数值1,输出是连续的输入数值个数个奇数&#…

资金管理平台 -SAP创建凭证测试程序及增强!

文章目录 主要程序创建程序程序 代码解析变量定义抬头和项目初始值表头赋值调用BAPI其他的子例程 核心内表增强部分LFACIF5D程序FI_DOCUMENT_CHECK完整程序 BADI增强 主要程序 创建程序 程序 &---------------------------------------------------------------------* *…

2.Redis安装

1.安装需要的依赖 因为是c语言编写的,所以需要gcc依赖。 yum install -y gcc tcl 这一步可能会遇到的问题解决方案参照博文地址: yum 报错:Could not retrieve mirrorlist http://mirrorlist.centos.org_yum install could not retrieve mi…

花几千上万学习Java,真没必要!(三十五)

1、Map: Map接口的基本且常用的操作,用于管理键值对集合。 V put(K key, V value) 作用:向映射中添加一个键值对。 参数:K key 是键的类型,V value 是与键关联的值。 返回值:如果映射以前包含该键的映射关…

upload-labs靶场(1-19关)

upload-labs靶场 简介 upload-labs是一个使用php语言编写的&#xff0c;专门收集渗透测试过程中遇到的各种上传漏洞的靶场。旨在帮助大家对上传漏洞有一个全面的了解。目前一共19关&#xff0c;每一关都包含着不同上传方式。 注意&#xff1a;能运行<?php phpinfo();?&…

1._专题1_双指针_C++

双指针 常见的双指针有两种形式&#xff0c;一种是对撞指针&#xff0c;一种是左右指针。对撞指针&#xff1a;一般用于顺序结构中&#xff0c;也称左右指针。 对撞指针从两端向中间移动。一个指针从最左端开始&#xff0c;另一个从最右端开始&#xff0c;然后逐渐往中间逼近…

php 一个极简的类例子

https://andi.cn/page/621627.html

WATLOW Power Series SSR User’s Manual

WATLOW Power Series SSR User’s Manual

RTC实时通信技术:GPT-4o急速响应背后的技术浅谈

RTC实时通信技术&#xff1a;GPT-4o急速响应背后的技术浅谈 RTC实时通信技术概述 RTC&#xff08;Real Time Communication&#xff09;&#xff0c;即实时通信技术&#xff0c;是实时音视频通信的简称。其核心在于实现低延迟、高质量的音视频数据传输和处理&#xff0c;广泛…

Java使用POI创建带样式和公式的Excel文件

这篇文章将演示如何使用POI 创建带样式和公式的Excel文件。 代码 import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook;import java.io.FileOutputStream; import java.io.IOException;public class ExcelDemo {public static void mai…