Vue 路由器、GitHub 页面和自定义域不适用于路由链接 [英] Vue Router, GitHub Pages, and Custom Domain Not Working With Routed Links
问题描述
我的域名:myname.com
我的 GitHub 存储库:myname
我的 GitHub 名称:myname
底层 GH 页面 URL:myname.github.io/myname
我的问题:我使用历史 Vue 路由器设置了以下页面:主页、联系人、项目.
每当我访问 myname.com/contact
时,它都会将我路由到 404.当我点击一个链接将我重定向到 /contacts
时,它会将其推送到地址栏,但如果您刷新,它会路由到 404.我已经阅读了诸如以下的帖子:将 Vue 部署到 GitHub 页面.vue-router 错误 和 页面刷新时的 Vue 路由器得到 404 但问题仍然存在并且自定义域增加了复杂性.我也读过这个:Vue Router return 404 when revisit to theurl 但是,有一个带有浏览器路由器的 React 路由器页面在 GH 页面上工作的示例:https://milosrancic.github.io/reactjs-website/activities,repo:https://github.com/milosrancic/reactjs-website.
这是我所做的:
- 我的
package.json
有以下几行:"homepage":"https://myname.com/"
- 我的
vue.config.js
正是这样:
module.exports = {公共路径:'/'}
- 我的路由器在我的路径上:
/src/router/index.js
,传入main.js
new Vue()
,并像这样导出:
export default new VueRouter({基础:process.env.BASE_URL,模式:'历史',路线})
- 在我的
App.vue
(入口组件)中,模板如下所示:
<div id="应用程序"><nav-bar></nav-bar><路由器视图></路由器视图>
</模板>
我无法通过 Vue 解决问题,但是,作为 tao 提到,这可能是 GitHub 以及他们如何处理 URL 的问题.最简单的解决方法是改用哈希路由器,以便条目 URL 保持静态.但是,这会在您的 URL 中引入可能令人反感的 /#/
.
鉴于本地不支持所有路由,有人找到了解决方法:https://github.com/rafgraph/spa-github-pages.注意:这可能对 SEO 不利,因为预期的 URL 实际上并不存在.这对他们的 404 重定向做了一个技巧,并在索引页面上处理它.这是一个投资组合网站,因此,我现在可以接受.如果我或其他人找到更好的解决方案,则会更新.
解决方法:
在/public
里面添加一个名为404.html
的文件并粘贴以下内容:
<头><元字符集=utf-8"><title>将其更改为您的标题</title><script type="text/javascript">//麻省理工学院许可证//https://github.com/rafgraph/spa-github-pages//此脚本获取当前 url 并转换路径和查询//string 变成一个查询字符串,然后重定向浏览器//到只有查询字符串和哈希片段的新 url,//例如https://www.foo.tld/one/two?a=b&c=d#qwe,变成//https://www.foo.tld/?/one/two&a=b~and~c=d#qwe//注意:这个 404.html 文件必须至少有 512 字节才能工作//使用 Internet Explorer(当前 > 512 字节)//如果您正在创建项目页面站点而不使用自定义域,//然后将 pathSegmentsToKeep 设置为 1(企业用户可能需要将其设置为 > 1).//这样代码只会替换路径的路由部分,而不是//应用所在的真实目录,例如://https://username.github.io/repo-name/one/two?a=b&c=d#qwe 变成//https://username.github.io/repo-name/?/one/two&a=b~and~c=d#qwe//否则,将 pathSegmentsToKeep 保留为 0.var pathSegmentsToKeep = 0;var l = window.location;l.替换(l.protocol + '//' + l.hostname + (l.port ? ':' + l.port : '') +l.pathname.split('/').slice(0, 1 + pathSegmentsToKeep).join('/') + '/?/' +l.pathname.slice(1).split('/').slice(pathSegmentsToKeep).join('/').replace(/&/g, '~and~') +(l.search ? '&' + l.search.slice(1).replace(/&/g, '~and~') : '') +l.hash);头部><身体></html>
在 /public/index.html
中,在
之后的 中添加以下内容:>
这对我有用,我现在只需输入 myname.com/contact
即可访问联系人页面.如果您使用相同的技巧在 /public
中嵌套了 SPA,这也应该有效.
My domain: myname.com
My GitHub repo: myname
My GitHub name: myname
Underlying GH-Pages URL: myname.github.io/myname
My Issue: I have the following pages set up with a History Vue Router: home, contact, projects.
Whenever I go to myname.com/contact
, it routes me to 404 instead. When I click on a link to redirect me to /contacts
, it pushes it to the address bar but if you refresh, it routes to 404. I have read posts such as: Deploy Vue to GitHub Pages. Error with vue-router and Vue router on page refresh gets 404 but the issue persists and the custom domain is an added complexity. I have also read this: Vue Router return 404 when revisit to the url but, there is an example of a React Router page with a Browser Router working on GH-Pages: https://milosrancic.github.io/reactjs-website/activities, repo: https://github.com/milosrancic/reactjs-website.
Here is what I have done:
- My
package.json
has the following line:"homepage": "https://myname.com/"
- My
vue.config.js
is exactly this:
module.exports = {
publicPath: '/'
}
- My Router is on my path:
/src/router/index.js
, passed intomain.js
new Vue()
, and is exported like this:
export default new VueRouter({
base: process.env.BASE_URL,
mode: 'history',
routes
})
- Inside my
App.vue
(entry component), the template looks like this:
<template>
<div id="app">
<nav-bar></nav-bar>
<router-view></router-view>
</div>
</template>
I was unable to fix the problem through Vue but, as tao mentioned, this is likely an issue with GitHub and how they handle URLs. The simplest fix is to use a Hash Router instead so that the entry URL stays static. However, this will introduce that, arguably distasteful /#/
in your URL.
Given how catch-all routes are not supported natively, someone has found a workaround for this: https://github.com/rafgraph/spa-github-pages. Note: This is likely not good for SEO because the intended URLs do not actually exist. This is doing a trick with their 404 redirects and handling it on the index page. This was for a portfolio site and as such, I am ok with this for now. If I, or someone else, finds a better solution, this will be updated.
Workaround:
Inside /public
add a file called 404.html
and paste the following contents:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>CHANGE THIS TO YOUR TITLE</title>
<script type="text/javascript">
// MIT License
// https://github.com/rafgraph/spa-github-pages
// This script takes the current url and converts the path and query
// string into just a query string, and then redirects the browser
// to the new url with only a query string and hash fragment,
// e.g. https://www.foo.tld/one/two?a=b&c=d#qwe, becomes
// https://www.foo.tld/?/one/two&a=b~and~c=d#qwe
// Note: this 404.html file must be at least 512 bytes for it to work
// with Internet Explorer (it is currently > 512 bytes)
// If you're creating a Project Pages site and NOT using a custom domain,
// then set pathSegmentsToKeep to 1 (enterprise users may need to set it to > 1).
// This way the code will only replace the route part of the path, and not
// the real directory in which the app resides, for example:
// https://username.github.io/repo-name/one/two?a=b&c=d#qwe becomes
// https://username.github.io/repo-name/?/one/two&a=b~and~c=d#qwe
// Otherwise, leave pathSegmentsToKeep as 0.
var pathSegmentsToKeep = 0;
var l = window.location;
l.replace(
l.protocol + '//' + l.hostname + (l.port ? ':' + l.port : '') +
l.pathname.split('/').slice(0, 1 + pathSegmentsToKeep).join('/') + '/?/' +
l.pathname.slice(1).split('/').slice(pathSegmentsToKeep).join('/').replace(/&/g, '~and~') +
(l.search ? '&' + l.search.slice(1).replace(/&/g, '~and~') : '') +
l.hash
);
</script>
</head>
<body>
</body>
</html>
Inside /public/index.html
, add the following inside the <head>
right after <title>
:
<script type="text/javascript">
// MIT License
// https://github.com/rafgraph/spa-github-pages
// This script checks to see if a redirect is present in the query string,
// converts it back into the correct url and adds it to the
// browser's history using window.history.replaceState(...),
// which won't cause the browser to attempt to load the new url.
// When the single page app is loaded further down in this file,
// the correct url will be waiting in the browser's history for
// the single page app to route accordingly.
(function(l) {
if (l.search[1] === '/' ) {
var decoded = l.search.slice(1).split('&').map(function(s) {
return s.replace(/~and~/g, '&')
}).join('?');
window.history.replaceState(null, null,
l.pathname.slice(0, -1) + decoded + l.hash
);
}
}(window.location))
</script>
This has worked for me and I can now visit the contacts page by simply entering myname.com/contact
. This should also work if you have nested SPA inside /public
, using the same trick.
这篇关于Vue 路由器、GitHub 页面和自定义域不适用于路由链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!