Skip to content

feat: Add support for serving frontend from non-root paths#5475

Open
NETIZEN-11 wants to merge 2 commits intolitmuschaos:masterfrom
NETIZEN-11:fix/subpath-deployment-5473-clean
Open

feat: Add support for serving frontend from non-root paths#5475
NETIZEN-11 wants to merge 2 commits intolitmuschaos:masterfrom
NETIZEN-11:fix/subpath-deployment-5473-clean

Conversation

@NETIZEN-11
Copy link
Copy Markdown

Enable Frontend Support for Custom Base Paths

Fixes #5473


#Description

This PR enables the Litmus frontend to be served from custom base paths (e.g., /litmus, /chaos) in addition to the root path (/).

It resolves issues with asset loading failures when deploying behind path-based routing (e.g., Traefik, Nginx Ingress).


#❗ Problem

Previously, the frontend relied on absolute paths (e.g., /main.js) which always resolved from the domain root.

This caused:

  • 404 errors when deployed at subpaths like example.com/litmus
  • Static assets failing to load
  • Broken client-side routing

Solution

This PR introduces dynamic base path handling:

  • Configure webpack to use PUBLIC_URL for asset paths
  • Add <base> tag for correct relative URL resolution
  • Configure React Router with basename
  • Add dynamic nginx configuration via template
  • Generate nginx config at runtime using entrypoint.sh

🔧 Changes

  • webpack.common.js

    • Use PUBLIC_URL for publicPath
  • index.html

    • Add <base href="%PUBLIC_URL%/">
  • bootstrap.tsx

    • Add basename={process.env.PUBLIC_URL} to BrowserRouter
  • nginx.conf.template

    • Add template for dynamic base path support
  • entrypoint.sh

    • Generate nginx config dynamically
  • Dockerfile

    • Copy nginx template
  • package.json

    • Add build:subpath script
  • Documentation

    • Add BASE_PATH_CONFIGURATION.md
    • Add deployment-example.yaml
  • Tests

    • Add unit tests and CI workflow

🧪 Testing

  • Verified application works at /
  • Verified application works at /litmus
  • Verified static assets load correctly
  • Verified routing works with basename
  • Tested with Traefik ingress

⚠️ Backward Compatibility

  • No breaking changes
  • Default behavior remains unchanged when PUBLIC_URL is not set

📦 Usage

env:
  - name: PUBLIC_URL
    value: "/litmus"

🙌 Notes

This improves deployment flexibility and ensures compatibility with path-based routing setups.

Fixes litmuschaos#5473

This commit enables the Litmus frontend to be served from custom base paths
(e.g., /litmus, /chaos) in addition to the root path (/). This allows deployment
behind path-based routing with ingress controllers like Traefik.

Changes:
- Configure webpack to use PUBLIC_URL environment variable for asset paths
- Add <base> tag to HTML template for relative URL resolution
- Configure React Router with basename for proper client-side routing
- Create nginx configuration template with dynamic base path support
- Update entrypoint script to generate nginx config at runtime
- Add comprehensive documentation and deployment examples
- Add unit tests and CI/CD workflow for testing

Usage:
Set PUBLIC_URL environment variable in deployment:
  env:
    - name: PUBLIC_URL
      value: "/litmus"

The application will automatically configure itself to work from that path.

Signed-off-by: NETIZEN-11 <kumarnitesh121411@gmail.com>
@PriteshKiri
Copy link
Copy Markdown
Contributor

Hey @NETIZEN-11

Some checks are failing. Could you please check?

- Replace 'it(' with 'test(' in basePath.test.ts to comply with
  jest/consistent-test-it ESLint rule (fixes frontend-checks)
- Rename forEach param 'path' to 'p' to avoid shadowing issues
- Add explicit types ['jest','node'] to tsconfig.json for test globals
- Inject process.env.PUBLIC_URL via DefinePlugin so bootstrap.tsx
  correctly reads the base path at runtime
- Simplify Docker container test to not depend on upstream services,
  only verify nginx starts and base path is logged correctly

Signed-off-by: NETIZEN-11 <kumarnitesh121411@gmail.com>
@NETIZEN-11
Copy link
Copy Markdown
Author

Hi @PriteshKiri,

Thanks for pointing it out. The failing checks have been fixed now.
Please take another look when you get a chance.

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

Successfully merging this pull request may close these issues.

unable to serve litmus FE from non_root path

2 participants