fix: MSW mock endpoint mismatch when dev server runs on non-3000 ports#178
Open
Vinyl-Davyl wants to merge 1 commit intoNetflix:masterfrom
Open
fix: MSW mock endpoint mismatch when dev server runs on non-3000 ports#178Vinyl-Davyl wants to merge 1 commit intoNetflix:masterfrom
Vinyl-Davyl wants to merge 1 commit intoNetflix:masterfrom
Conversation
hardcodes localhost:3000
Author
682f384 to
3deeab9
Compare
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.
Description of the Change
The mock API setup in src/mocks/browser.js previously hardcoded the base URL to:
http://localhost:3000This caused issues when the React development server started on a different port (e.g. 3001 or 3002) because Mock Service Worker (MSW) intercepts did not match the request origin.
As a result, the mock endpoints failed to intercept API requests during local development.
This change replaces the hardcoded origin with:
window.location.originThis allows the mock endpoints to dynamically match the current origin of the running application, ensuring that MSW intercepts work regardless of the port used by the development server.
Alternate Designs
Other alternatives considered:
Using a configurable environment variable for the mock base URL.
Detecting the dev server port and dynamically constructing the URL.
However, using
window.location.originwas chosen because it is simpler, requires no configuration, and ensures the mock endpoints always match the current application origin.Possible Drawbacks
None expected. The change only affects local mock endpoint construction and should not impact production behavior.
Verification Process
Steps used to verify the fix:
Started the development server while port 3000 was already in use.
Dev server started on http://localhost:3002.
Observed that mock API requests failed due to the hardcoded localhost:3000 endpoint.
Updated the mock endpoint to use window.location.origin.
Restarted the dev server.
Verified that all mock requests were correctly intercepted by MSW.
Result
Mock API responses were correctly returned regardless of the dev server port. Fixes #177