Fix: 404 errors for static assets when using CMD_URL_PATH configuration#1943
Open
Fix: 404 errors for static assets when using CMD_URL_PATH configuration#1943
Conversation
a89a785 to
c19b948
Compare
- Added support for a configurable URL path in the application, allowing static files and routes to be served under a specified path. - Implemented redirection from the root to the configured URL path. - Updated socket.io initialization to respect the URL path configuration. - Enhanced middleware to prevent redirection for the URL path itself. This change improves flexibility in routing and enhances the application's structure. Signed-off-by: Yukai Huang <yukaihuangtw@gmail.com>
c19b948 to
4e24b74
Compare
Member
Author
Code Simplification Applied ✨I've simplified the implementation by eliminating code duplication: Before (duplicated if/else blocks):
After (unified approach):
Key InsightWhen
This works for both cases without duplication! Analysis document: I also created |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When configuring
CMD_URL_PATHto serve CodiMD under a subdirectory path (e.g.,CMD_URL_PATH=codimd), static assets like CSS, JavaScript files, favicons, and other resources return 404 errors, making the application unusable.Example Issue
With the following Docker configuration:
URLs like these would return 404:
http://localhost:3000/codimd/build/font.68d91a5a4c1afd7f82a9.css→ 404http://localhost:3000/codimd/build/cover.65bca5da2232a62861ce.css→ 404Root Cause
/instead of respecting theurlPathconfigurationurlPathprefix/codimdand/codimd/Solution
1. Fixed Static Asset and Route Mounting (
app.js)Added conditional mounting based on
config.urlPath:2. Fixed Redirect Loop (
lib/middleware/redirectWithoutTrailingSlashes.js)Updated the middleware to avoid redirect loops by allowing the URL path itself to pass through:
3. Socket.IO Configuration
Updated Socket.IO to use the correct path when URL path is configured:
Testing
Local Configuration Used
{ "domain": "", "urlAddPort": false, "urlPath": "codimd" }Results
/codimd/build/.../codimd//codimd/api/...Reverse Proxy Compatibility
The fix also works correctly with reverse proxies. Here's a working Caddy configuration:
Key Points for Reverse Proxy Setup:
/codimdBreaking Changes
None. This is a backward-compatible fix that only affects behavior when
CMD_URL_PATHis configured.Files Modified
app.js- Added URL path-aware mounting logiclib/middleware/redirectWithoutTrailingSlashes.js- Fixed redirect loopFixes issue with 404 errors when using
CMD_URL_PATHconfiguration for serving CodiMD under subdirectory paths.