Add support for app deployment under sub-paths
Overview
This pull request refactors the routing system in the frontend to allow the application to be hosted under sub-paths such as example.com/somerandomdir.
With this implementation, we can now install the applications under subdirectories rather than only at the root domain level.
Example:
The base directory /ticketing will be calculated from the VITE_FRONTEND_URL environment variable:
VITE_FRONTEND_URL=https://example.com/ticketing
The changes include separating route definitions and options, introducing a utility function for determining the base path, and updating relevant configurations and imports to align with the new structure.
Additional Info
Routing Refactor:
-
frontend/src/router.tsx: Refactored to exportroutesandoptionsseparately for clarity. AddedgetBasePathto dynamically determine the base path for the application based on an environment variable. [1] [2] -
frontend/src/entry.client.tsxandfrontend/src/entry.server.tsx: Updated to use the newly namedroutesandoptionsexports instead of the previously namedrouterconstant. [1] [2] [3] [4] [5] - Updated
frontend/src/App.tsxto enforce that the app is always accessed via the base path by redirecting users if the current path does not match the base path. This is required as react-router v6 no longer does this automatically as it did in v5.
Base Path Utility:
-
frontend/src/utilites/basePath.ts: Introduced a new functiongetBasePathto compute the base path dynamically based on the environment variableVITE_FRONTEND_URL. Includes fallback and error handling for invalid URLs.
Configuration Updates:
-
frontend/vite.config.ts: Updated the Vite configuration to use relative paths for assets by setting thebaseproperty to"./". This allows resource URI's such as/assets/*to change to/somebasedir/assetsdynamically.
Reviews:
This is my first PR on the project! Hopefully all is well but feel free to drop a message if there is anything more I can do as I hope to contribute more in future.
All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.
Thanks @creativeindustriesgroup! I'll test this soon
@daveearley I kept as a draft as theres still a few issues with this implementation. I think it is something to do with PHP in Docker.
Thanks, Joe Berry.