Преглед на файлове

fix 重复网页的问题

guoziyun преди 1 година
родител
ревизия
d6fbb60d50
променени са 2 файла, в които са добавени 17 реда и са изтрити 13 реда
  1. 7 3
      routes/index.js
  2. 10 10
      views/index.ejs

+ 7 - 3
routes/index.js

@@ -883,9 +883,8 @@ router.get('/:lang/coloring-page/:str', function (req, res, next) {
     utils.validators.validateId(id);
 
     let imageType = req.headers.accept?.includes('image/webp') ? 'webp' : 'jpeg';  // 浏览器支持webp就用webp
-    let page = req.query.page ?? 1;
 
-    let cacheKey = `${CACHE_PREFIX}_${imageType}_detail_${id}_${lang}_${page}`;
+    let cacheKey = `${CACHE_PREFIX}_${imageType}_detail_${id}_${lang}_${req.query.page}_${req.query.length}`;
     let htmlData = await redis.getAsync(cacheKey);
     if (!htmlData) {
       let doc = await models.Art
@@ -975,7 +974,7 @@ router.get('/:lang/detail/:id', function (req, res, next) {
     let imageType = req.headers.accept?.includes('image/webp') ? 'webp' : 'jpeg';  // 浏览器支持webp就用webp
     let page = req.query.page ?? 1;
 
-    let cacheKey = `${CACHE_PREFIX}_${imageType}_detail_${id}_${lang}_${page}`;
+    let cacheKey = `${CACHE_PREFIX}_${imageType}_detail_${id}_${lang}_${req.query.page}_${req.query.length}`;
     let htmlData = await redis.getAsync(cacheKey);
     if (!htmlData) {
       let doc = await models.Art
@@ -1229,6 +1228,11 @@ function replaceUriParams(uri, newPage, newLength) {
   // 使用正则表达式替换length参数
   uri = uri.replace(/(length=)\d+/, `length=${newLength}`);
 
+  // 如果是第一页,uri就不带?page=1&length=30了,避免重复页面
+  if (newPage == 1) {
+    uri = uri.replace(/(?:\?|&)((page|length)=\d+)/g, '');
+  }
+
   // 返回替换后的URI
   return uri;
 }

+ 10 - 10
views/index.ejs

@@ -3,7 +3,7 @@
 
 <head>
   <%- include('common-meta') %>
-    <link rel="alternate" href="https://art.pcoloring.com" hrefLang="x-default" />
+    <link rel="alternate" href="https://art.pcoloring.com/en" hrefLang="x-default" />
 
     <link rel="stylesheet" href="/stylesheets/styles.css">
     <link rel="stylesheet" href="/stylesheets/header.css">
@@ -50,15 +50,15 @@
 
 <body>
   <%- include('header') %>
-  <%- include('intro-section') %>
-  <%- include('latest-section') %>
-  <%- include('album-section') %>
-  <%- include('hot-section') %>
-  <%- include('designer-section') %>
-  <%- include('special-section') %>
-  <%- include('footer') %>
-  <%- include('cookie-banner') %>
-  <div style="height: 50px;"></div>
+    <%- include('intro-section') %>
+      <%- include('latest-section') %>
+        <%- include('album-section') %>
+          <%- include('hot-section') %>
+            <%- include('designer-section') %>
+              <%- include('special-section') %>
+                <%- include('footer') %>
+                  <%- include('cookie-banner') %>
+                    <div style="height: 50px;"></div>
 </body>