Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] 在特定配置下重定向失效 #3877

Closed
5 of 8 tasks
dynilath opened this issue Jan 31, 2024 · 18 comments
Closed
5 of 8 tasks

[Bug] 在特定配置下重定向失效 #3877

dynilath opened this issue Jan 31, 2024 · 18 comments
Assignees
Labels
bug Something isn't working

Comments

@dynilath
Copy link

Checklist

  • I am using LTS version of Node.js.
  • I am using the latest v2 version of VuePress and VuePress official plugins.
  • I am using the latest v2 version of vuepress-theme-hope and all plugins in this repo.
  • I followed the docs and I double checked my configuration.
  • I have searched FAQ and I did not find the answer.

Package name

vuepress-plugin-redirect

Which operating system are you using?

  • macOS
  • Windows
  • Linux

Describe the bug

如后文所述的项目结构,在使用vuepress dev docs可以正常自动语言重定向,发到github pages就不能重定向了

在本地vuepress build docs构建之后,用http-server -p 8080 -c-1 --cors .建立服务也无法重定向

项目结构如下

Mode            Length Hierarchy
----            ------ ---------
d----        204.20 KB redirect-bug
-a---          1.04 KB ├── package.json
-a---        203.13 KB ├── pnpm-lock.yaml
-a---         29.00  B ├── tree.txt
d----          0.00  B └── docs
-a---          0.00  B     ├── A.md
d----          1.42 KB     ├── zh
-a---          1.42 KB     │   └── README.md
d----         18.00  B     ├── other
-a---         18.00  B     │   └── README.md
d----        688.00  B     ├── en
-a---        688.00  B     │   └── README.md
d----          1.75 KB     └── .vuepress
-a---        818.00  B         ├── config.ts
-a---        971.00  B         ├── theme.ts
d----        557.00  B         └── locales
-a---        248.00  B             ├── en.ts
-a---         67.00  B             ├── index.ts
-a---        242.00  B             └── zh.ts

config.ts

import { defineUserConfig } from "vuepress";
import { redirectPlugin } from "vuepress-plugin-redirect";
import { theme } from "./theme";
import viteBundler from "@vuepress/bundler-vite";

export default defineUserConfig({
    bundler: viteBundler(),
    locales: {
        '/zh/': {
            lang: 'zh-CN',
            title: '测试测试测试',
            description: '测试测试测试'
        },
        '/en/': {
            lang: 'en-US',
            title: 'Lorem ipsum dolor sit amet',
            description: 'Lorem ipsum dolor sit amet'
        }
    },
    base: '/Test/',
    dest: 'public',
    plugins: [
        redirectPlugin({
            autoLocale: true,
            defaultBehavior: "homepage",
        })
    ],
    theme
});

我认为和这两个

Mode            Length Hierarchy
----            ------ ---------
d----        204.20 KB redirect-bug
d----         18.00  B     ├── other
-a---         18.00  B     │   └── README.md
-a---          0.00  B     ├── A.md

不在任何语言下内容的有关系,移除掉就正常了


另外,我尝试将redirect_bug/A.md重定向到redirect_bug/zh/A.md
我尝试写了

---
redirectFrom: /A.md
---

---
redirectFrom: A.md
---

都无法重定向

于是我尝试建立了一个redirect_bug/A.md 于是产生了上面的问题
如果这个redirect_bug/A.md里面写

---
redirectTo: /zh/A.md
---

会重定向到HOST_NAME/Test//zh/A.html然后进404.html

如果写成

---
redirectTo: zh/A.md
---

那么如果使用在redirect_bug/other/A.html,会重定向到HOST_NAME/Test/zh/A.htmlother/zh/A.html然后得到404.html

这一部分(横线分隔的)现象在github pages, vuepress dev docs 和本地建立服务都会出现

Additional context

No response

@dynilath dynilath added the bug Something isn't working label Jan 31, 2024
@Mister-Hope
Copy link
Member

I believe the redirectTo issue is fixed, you are reporting multiple issues at same time and that confused me, can you open new pr with what you do, what's expected and what happening about the rest?

@Mister-Hope
Copy link
Member

更新:如果你使用了autoLocale,而你的项目中存在不属于任何一个语言的页面的时候,报错是预期的,因为插件根本不清楚要做什么。

@dynilath
Copy link
Author

dynilath commented Jan 31, 2024

简单来说这是/A.md改成了/zh/A.md

但是在/zh/A.md写redirectFrom没有跳转,redirectTo就得需要弄一个不属于任何语言的页面。

那么 #3878 应该就是预期不工作的。(那么为啥vuepress dev docs的情况能工作)

我尝试一下做个这个redirectFrom的问题的最小重现

@Mister-Hope
Copy link
Member

Mister-Hope commented Jan 31, 2024

我还是没太看懂你表达了什么,你关闭的那个议题,你给出的结构和你底下说的对不上。

输出构建结果的时候,插件只会根据已有的界面来生成能够自动重定向的界面,也就是说如果你想要让 /a 能实现重定向,你需要在至少一个语言下有 a.md,否则压根就不会有这个界面,应该会默认走到服务器404的逻辑。

@Mister-Hope
Copy link
Member

redirectForm 的底层工作原理是插件会便利所有的数据把它们集中起来并推断出原始的重定向界面,并把那些重定向界面创建出来。

autoLocate也是一样的,插件会收集所有语言中存在过的所有路径,并在根目录自动生成他们同时提供自动从定向到一个最贴近用户语言的功能。

Redirect to的工作逻辑是直接在当前界面写一个script的标签,这个script的标签会进行跳转。

@Mister-Hope
Copy link
Member

Mister-Hope commented Jan 31, 2024

Reirectto不工作的原因是因为我忘了去掉pathname 最开始的斜杠了,这个问题很简单,你汇报的也很清晰,但是其他的部分我都没有听懂原因在哪里。

实际上因为插件写的比较久了,我现在也不是很确定,如果说你强行要在启用了自动语言重定向的功能时,添加一些不属于任何语言的界面,会发生什么事情,我认为那些页面会保留,也不会被重定向,也不应该涉及到报错。如果你添加了这些界面之后,整个自动语言程序性功能都不工作了,那可能应该算是一个bug,你可以给一个最小复现,或者说你在action里给出一个明确的文件结构,然后给出一系列步骤,我自己去复现。

@dynilath
Copy link
Author

制作了一个最小重现

minrep.zip

需要实现的目标是访问
http://localhost:8080/Test/A.html
时,自动跳转到
http://localhost:8080/Test/zh/A.html

autoLocale的时候,config或者redirectFrom都无法从base重定向

按我的理解autoLocale:true的时候就算没有任何其他redirect设置也应该能实现这个定向(否则哪儿来的歧义),但是得到的是404

@dynilath
Copy link
Author

如果你添加了这些界面之后,整个自动语言程序性功能都不工作了,那可能应该算是一个bug,你可以给一个最小复现,

针对这个我做了另一个最小复现

minrep_autolocale.zip

在这个复现里
访问http://localhost:8080/Test/A.html能够跳转到http://127.0.0.1:8080/Test//zh/A.html
但是访问http://localhost:8080/Test/会留在这里,无法根据语言跳转

需要注意的是

pnpm docs:dev

提供的页面,http://localhost:8080/Test/是能够根据语言跳转到http://localhost:8080/Test/zh/

但是

pnpm docs:build --dest Test
pnpm serve

这样访问http://localhost:8080/Test/就无法跳转了

@Mister-Hope
Copy link
Member

Mister-Hope commented Jan 31, 2024

制作了一个最小重现

minrep.zip

需要实现的目标是访问 http://localhost:8080/Test/A.html 时,自动跳转到 http://localhost:8080/Test/zh/A.html

autoLocale的时候,config或者redirectFrom都无法从base重定向

按我的理解autoLocale:true的时候就算没有任何其他redirect设置也应该能实现这个定向(否则哪儿来的歧义),但是得到的是404

输出结果我测试是没有问题的:

小写 base:

image
image
image

大写 base:

image
image

Vite 开发服务器配合 chrome 我是测出了一些问题的,在原选项卡单纯的修改大小写似乎实际访问的地址是不变的。

每次开一个新标签卡访问,并不使用任何自动补全是完全正常的。

代码实现里插件选项的 config 和 frontmatter 的 redirectFrom 会合并到一个 redirectMap 处理,也就是上方的 log,所以 config 应该也正常。

@Mister-Hope
Copy link
Member

Mister-Hope commented Jan 31, 2024

另一个案例测试也是正常的,你最好确定你在汇报什么

image

0.3s 内自动从 /Test/ 重定向到 /Test/en/ (Linux 我的环境是英文的)

@dynilath
Copy link
Author

dynilath commented Jan 31, 2024

这是minrep.zip的现象

20198

@dynilath
Copy link
Author

这是minrep_autolocale.zip的现象

20200

@Mister-Hope
Copy link
Member

Vite 开发服务器配合 chrome 我是测出了一些问题的,在原选项卡单纯的修改大小写似乎实际访问的地址是不变的。

每次开一个新标签卡访问,并不使用任何自动补全是完全正常的。

新建一个窗口,把 localhost:8080/Test/A.html 粘进去回车试试

@Mister-Hope
Copy link
Member

关于使用主题,根目录不能重定向的问题,很有可能是 auto-catalog 为你自动生成了页面。

建议配置 plugins.autoCatalog 把根目录排除掉 See https://plugin-auto-catalog.vuejs.press/config.html#exclude

@dynilath
Copy link
Author

Vite 开发服务器配合 chrome 我是测出了一些问题的,在原选项卡单纯的修改大小写似乎实际访问的地址是不变的。
每次开一个新标签卡访问,并不使用任何自动补全是完全正常的。

新建一个窗口,把 localhost:8080/Test/A.html 粘进去回车试试

20201

@Mister-Hope
Copy link
Member

Vite 开发服务器配合 chrome 我是测出了一些问题的,在原选项卡单纯的修改大小写似乎实际访问的地址是不变的。
每次开一个新标签卡访问,并不使用任何自动补全是完全正常的。

新建一个窗口,把 localhost:8080/Test/A.html 粘进去回车试试

20201 20201

我这边在 linux 完全复现不了,而且我是用默认主题测得,建议先切回默认主题测一下确定不是主题的问题,然后把你的路径改成全小写再测测,如果确定跟这两个无关的话,等新版发布之后再给我打个压缩包?

@Mister-Hope
Copy link
Member

Mister-Hope commented Jan 31, 2024

关于使用主题,根目录不能重定向的问题,很有可能是 auto-catalog 为你自动生成了页面。

建议配置 plugins.autoCatalog 把根目录排除掉 See https://plugin-auto-catalog.vuejs.press/config.html#exclude

测了一下似乎有这个问题,但我没验证上面的配置能不能修(99% 大概行)。插件的部分冲突需要你自行解决,因为主页不存在时自动生成一个目录页也是合理的。

@Mister-Hope
Copy link
Member

Mister-Hope commented Jan 31, 2024

43ac200 这个 commit 你 patch 进去之后就不应该出现 // 的路径了

目前因为一个比较大的 PR: #3879 我暂时发不了版。

@vuepress-theme-hope vuepress-theme-hope locked and limited conversation to collaborators Jan 31, 2024
@Mister-Hope Mister-Hope converted this issue into discussion #3881 Jan 31, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants