gpt4 book ai didi

angular - Firebase 上托管的 Ionic 4 应用程序仅在重新加载时返回 404

转载 作者:行者123 更新时间:2023-12-02 12:17:27 25 4
gpt4 key购买 nike

我使用 @angular/pwa、firebase 托管和这个创建了一个 Ionic 4 PWA helpful tutorial.

按照教程中的所有说明进行操作(并将我的应用程序构建到“www”文件夹中)后,我将工作中的 Ionic 4 应用程序部署到 this working link ,在 CLI 中使用“firebase 部署”。

问题是:第一次访问该页面时,一切都运行良好,没有任何缺陷。但是,当我重新加载页面或使用搜索栏访问页面时(例如,输入“https://..link../login ”),Firebase 会返回 404 错误。

This file does not exist and there was no index.html found in the current directory or 404.html in the root directory.

在控制台中:

Failed to load resource: the server responded with a status of 404 ()

我的 firebase.json:

{
"hosting": {
"public": "www",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"headers": [
{
"source": "/build/app/**",
"headers": [
{
"key": "Cache-Control",
"value": "public, max-age=31536000"
}
]
},
{
"source": "sw.js",
"headers": [
{
"key": "Cache-Control",
"value": "no-cache"
}
]
}
]
}
}

我的 Angular 路由模块:(app-routing-module.ts)

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

const routes: Routes = [
{ path: '', loadChildren: './tabs/tabs.module#TabsPageModule' },
{ path: 'landing', loadChildren: './landing/landing.module#LandingPageModule' },
{ path: 'add-event', loadChildren: './add-event/add-event.module#AddEventPageModule' },
{ path: 'signup', loadChildren: './signup/signup.module#SignupPageModule' },
{ path: 'login', loadChildren: './login/login.module#LoginPageModule' },
{ path: 'add-profile', loadChildren: './add-profile/add-profile.module#AddProfilePageModule' }
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule {}

我可以做什么来修复这个错误?

最佳答案

Angular/Ionic 应用程序将所有流量定向到 index.html 文件,并将请求从那里路由到正确的模块/组件。当您尝试访问https://applifybyamplify.firebaseapp.com/login时firebase 正在寻找文件夹 ./login/index.html 所以你会得到 404。

您需要使用rewrite firebase.json 中的规则。

这样的东西应该有效

{
"hosting": {
"public": "www",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [
{
"source": "**",
"destination": "/index.html"
}
],
"headers": [
{
"source": "/build/app/**",
"headers": [
{
"key": "Cache-Control",
"value": "public, max-age=31536000"
}
]
},
{
"source": "sw.js",
"headers": [
{
"key": "Cache-Control",
"value": "no-cache"
}
]
}
]
}
}

关于angular - Firebase 上托管的 Ionic 4 应用程序仅在重新加载时返回 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54102529/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com