简体   繁体   English

GitHub 页面不允许使用 Angular 自动重定向,它显示 404 GitHub 页面

[英]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 使用HashLocationStrategyScully


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.可以使用三种方法让服务器开始玩球。

  • Configure the server to serve the bootstrap HTML document and then depend on JavaScript to update it for the URL (this is bad for caching, bad for HTTP status codes, back for search engines, and not possible on Github Pages).配置服务器以服务引导程序 HTML 文档,然后依赖 JavaScript 为 URL 更新它(这对缓存不利,对 HTTP 状态代码不利,对搜索引擎不利,对搜索引擎不利,并且在 Github 页面上不可能)。
  • Implement server-side rendering (eg with Angular Universal ) where you use Node.js or similar to generate HTML server side for initial page loads using the same code as you would send to run in the browser.实施服务器端呈现(例如,使用Angular Universal ),您可以在其中使用 Node.js 或类似的代码生成 HTML 服务器端以进行初始页面加载,使用与您发送以在浏览器中运行的代码相同的代码。 (This is not possible on Github Pages). (这在 Github 页面上是不可能的)。
  • Static site generation (eg with Scully ) where you generate static HTML documents for all your pages at build time. Static 站点生成(例如使用Scully ),您可以在构建时为所有页面生成 static HTML 文档。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM