[英]GitHub Pages doesn't allow automatic redirects with Angular, it shows 404 GitHub Page
GitHub Pages shows me 404 when I use automatic redirection to the login path or try to directly access that path other than the base path in Angular. GitHub 当我使用自动重定向到登录路径或尝试直接访问该路径而不是 Angular 中的基本路径时,页面显示 404。
I created an application in Angular that automatically redirects to "/auth/login" when it detects that the session has not been logged in.我在 Angular 创建了一个应用程序,当它检测到 session 未登录时自动重定向到“/auth/login”。
So the problem I have is if I directly access the URL "user.github.io/example-app" the application works fine and perfect, but if I directly access the URL "user.github.io/example-app/auth/login" (or any path other than the parent) throws me the 404 GitHub Pages.所以我遇到的问题是,如果我直接访问 URL“user.github.io/example-app”,应用程序可以正常工作,但是如果我直接访问 URL“user.github.io/example-app/auth/ login”(或父路径以外的任何路径)向我抛出 404 GitHub 页面。
Even I am using PWA and when installing the app on my Android device it directly throws the 404 of GitHub Pages.即使我正在使用 PWA,在我的 Android 设备上安装应用程序时,它也会直接抛出 GitHub 页面的 404。
Note:笔记:
I have also noticed that if I use Lighthouse and do a test using http-server, it also throws a 404 when executing the test.我还注意到,如果我使用 Lighthouse 并使用 http-server 进行测试,它也会在执行测试时抛出 404。
Using http-server I can install the application on Windows and this does show me the application, not the 404 that happened to me with GitHub Pages.使用 http-server 我可以在 Windows 上安装应用程序,这确实向我显示了应用程序,而不是我在 GitHub 页面上遇到的 404。
tl;dr Use HashLocationStrategy or Scully . tl;dr 使用HashLocationStrategy或Scully 。
Angular, by default, uses the History API for routing. Angular,默认使用History API进行路由。
This system allows the JavaScript to tell the browser:该系统允许 JavaScript 告诉浏览器:
I have manipulated the DOM so the user is effectively looking at a new page.我操纵了 DOM,因此用户可以有效地查看新页面。 If you went to this URL then the server would send you this page .
如果你去这个 URL然后服务器会发送给你这个页面。
Unfortunately, Angular does nothing to make that possibly by default.不幸的是,默认情况下 Angular 没有做任何事情。 There are three approaches that are used to get the server to play ball.
可以使用三种方法让服务器开始玩球。
Alternatively, keep having a single HTML document, ditch the history API, and replace it with a URL scheme that is based on the premise that you have only a single HTML document.或者,保持单个 HTML 文档,放弃历史 API,并将其替换为 URL 方案,该方案基于您只有一个 HTML 文档的前提。 Use the HashLocationStrategy ).
使用HashLocationStrategy )。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.