Skip to content

fix: MSW mock endpoint mismatch when dev server runs on non-3000 ports#178

Open
Vinyl-Davyl wants to merge 1 commit intoNetflix:masterfrom
Vinyl-Davyl:fix/mock-data-setup-hardcoded-routing
Open

fix: MSW mock endpoint mismatch when dev server runs on non-3000 ports#178
Vinyl-Davyl wants to merge 1 commit intoNetflix:masterfrom
Vinyl-Davyl:fix/mock-data-setup-hardcoded-routing

Conversation

@Vinyl-Davyl
Copy link
Copy Markdown

@Vinyl-Davyl Vinyl-Davyl commented Mar 14, 2026

Description of the Change

The mock API setup in src/mocks/browser.js previously hardcoded the base URL to:

http://localhost:3000

This 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.origin

This 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.origin was 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

Screenshot 2026-03-14 at 5 11 49 AM Screenshot 2026-03-14 at 5 12 17 AM

 hardcodes localhost:3000
@Vinyl-Davyl
Copy link
Copy Markdown
Author

Vinyl-Davyl commented Mar 14, 2026

cc: @saikonen @romain-intel

@Vinyl-Davyl Vinyl-Davyl force-pushed the fix/mock-data-setup-hardcoded-routing branch from 682f384 to 3deeab9 Compare March 14, 2026 09:16
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.

MSW mock endpoints hardcoded to localhost:3000 causing mock failures on different dev ports

1 participant