I am trying to implement my application as a micro-frontend using a single-spa library. The folder structure of the application is as below,
- micro-frontend
- root app
- navbar app (Angular application)
- dashboard app (Angular application)
- service app (Angular application)
I have deployed the same in the server and the website works as expected. The folder structure in the server(/var/www/html/) is as follows,
- home (root) [URL: https://microfrontendapp.com/home]
- dashboard [URL: https://microfrontendapp.com/dashboard/my-dashboard]
- service [URL: https://microfrontendapp.com/service]
- navbar
The issue I am facing is that I start using the website from the home page, then move to the dashboard or service application, and if I try reloading from this URL path, I receive a 404 Page not found error for the web URL.
I am trying to use route hashing to resolve the issue. The code is as below:
Home(Root) App
index.html
<template id="single-spa-layout">
<single-spa-router mode="hash" base="/">
<div style="display: flex">
<nav>
<application name="navbar"></application>
</nav>
<div style="width: 96%; margin: 5% 1%">
<route path="/dashboard">
<application name="dashboard"></application>
</route>
<route path="/service">
<application name="service"></application>
</route>
</div>
</div>
</single-spa-router>
</template>
root-application.js
System.import("single-spa").then(function(singleSpa) {
singleSpa.registerApplication(
"navbar",
function() {
return System.import("navbar");
},
function(location) {
return true;
}
);
singleSpa.registerApplication(
"dashboard",
() => System.import("dashboard"),
location => location.pathname.startsWith("/dashboard"),
{ some: "value" }
);
singleSpa.registerApplication(
"service",
() => System.import("service"),
location => location.pathname.startsWith("/service"),
{ some: "value" }
);
Navbar App
onNavigation() {
window.location.pathname += "/";
// The above was used because on during navigation to dashboard or any other app, URL is changed as https://microfrontendapp.com/home#/dashboard/my-dashboard
window.location.hash = "/" + url; // To point to the selected navigation URL
}
Dashboard App
router.ts
const routes: Routes = [
{
path: 'dashboard',
component: DashboardComponent,
children: [
{
path: 'my-dashboard',
component: MyDashboardComponent,
data: { }
}
}
]
@NgModule({
imports: [RouterModule.forRoot(routes), { useHash: true, preloadingStrategy: NoPreloading }],
exports: [RouterModule],
providers: [
{ provide: APP_BASE_HREF, useValue: '/' },
],
})
export class AppRoutingModule { }
Local server URL behavior is as below,
- Home page: http://localhost:4200/#/
- Navigate to Dashboard: http://localhost:4200/#/dashboard/my-dashboard
- When reloading on the above URL it works just fine.
When deployed in the apache server the behavior is as below,
- Home page: https://microfrontendapp.com/home
- Navigate to Dashboard: https://microfrontend.com/home/#/dashboard/my-dashboard. But the page is not loading. There is no HTTP call to get the main script file (dashboard app compiled script file) to load the page.
Can someone help on why has the HTTP call for the script file not started? I am not sure if I have done something wrong. Or is there any other solution to resolve the 404 Page not found issue
Kindly help me here. Thanks in advance.