Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kubernetes deployment under ingress path not supported. BASE_HREF not recognized. #38

Open
StoyanStAtanasov opened this issue Jan 14, 2025 · 0 comments

Comments

@StoyanStAtanasov
Copy link

I'm deploying this project for evaluation in our company's kubernetes cluster. As we have many services we use ingress to make them available under a path. From what I could see this is not a supported feature.

Just as an example I ran this by o1-mini:

Solution

Configure the Angular app to recognize the BASE_HREF by dynamically setting it at runtime using the BASE_HREF environment variable.

Steps

  1. Create a Configuration Script (config.js):

    • This script will read the BASE_HREF environment variable and set it for the Angular app.
  2. Modify index.html to Include config.js and Set base href Dynamically:

    • Update the index.html to load the config.js before the Angular bundles and set the base tag accordingly.
  3. Update Angular App to Use Dynamic BASE_HREF:

    • Modify the Angular app to use the dynamically set BASE_HREF.

Changes

1. Create config.js

Create a new file named config.js in the src directory of your Angular project.

(function(window) {
  window.__env__ = window.__env__ || {};
  window.__env__.BASE_HREF = window.__env__.BASE_HREF || '/';
})(this);

2. Modify index.html

Update the index.html to include the config.js and set the base href dynamically.

<!doctype html>
<html lang="en">
<head>
  <!-- ...existing code... -->
  <script src="config.js"></script>
  <base href="%BASE_HREF%">
  <!-- ...existing code... -->
</head>
<body>
  <app-root></app-root>
  <!-- ...existing code... -->
</body>
</html>

3. Update Angular's main.ts

Modify main.ts to replace the placeholder %BASE_HREF% with the value from config.js.

import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';

import { AppModule } from './app/app.module';
import { environment } from './environments/environment';

if (environment.production) {
  enableProdMode();
}

// Replace the base href placeholder with the value from config.js
const baseHref = window.__env__?.BASE_HREF || '/';
const baseTag = document.querySelector('base');
if (baseTag) {
  baseTag.setAttribute('href', baseHref);
}

platformBrowserDynamic().bootstrapModule(AppModule)
  .catch(err => console.error(err));

4. Adjust Kubernetes Deployment to Serve config.js

Ensure that config.js is accessible by the Angular app. You might need to update your Dockerfile or Kubernetes configuration to include config.js in the served files.

# Example Dockerfile changes

# Copy config.js to the Angular build output
COPY src/config.js /usr/share/nginx/html/

Summary of Actions

  • Created config.js to hold the BASE_HREF configuration.
  • Modified index.html to include config.js and set the base href dynamically.
  • Updated main.ts to apply the BASE_HREF from config.js.
  • Ensured Kubernetes serves config.js by updating the Dockerfile.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant