You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm encountering an issue where the path specified in the NEXT_PUBLIC_MEDUSA_BACKEND_URL environment variable is being overwritten when constructing API request URLs. This leads to requests being sent to incorrect endpoints, resulting in errors.
I have set up the NEXT_PUBLIC_MEDUSA_BACKEND_URL to point to a backend URL that includes a custom path:
https://api.example.com/custom-path
However, when the storefront makes API requests, the custom path (/custom-path) gets dropped, and the requests are sent to:
https://api.example.com/store/regions
instead of the expected:
https://api.example.com/custom-path/store/regions
This results in 404 Not Found errors or unexpected HTML responses.
Steps to Reproduce
Set NEXT_PUBLIC_MEDUSA_BACKEND_URL to a URL that includes a custom path:
Start the Medusa backend server and ensure it's accessible at the specified URL with the custom path.
Run the Medusa Next.js storefront.
Access the storefront in the browser.
Observe the network requests being made to the backend.
Expected Behavior
API requests should preserve the custom path specified in the NEXT_PUBLIC_MEDUSA_BACKEND_URL environment variable. For example, requests to fetch regions should be sent to:
https://api.example.com/custom-path/store/regions
Actual Behavior
The custom path is omitted, and requests are sent directly to the domain, resulting in incorrect URLs:
https://api.example.com/store/regions
Analysis
The issue appears to be caused by the way URLs are constructed in the storefront code. When using template literals or the URL constructor with paths that start with a leading slash (/), the existing path in the base URL gets overwritten.
If BACKEND_URL includes a path, appending /store/regions results in the base path being discarded.
Proposed Solution
Modify the URL construction logic to correctly handle base URLs that include paths. Here's an example of how this can be achieved:
functionconstructMedusaUrl(path){constbaseUrl=process.env.NEXT_PUBLIC_MEDUSA_BACKEND_URL;consturl=newURL(baseUrl);// Ensure no trailing slash on base URL pathnameconstbasePath=url.pathname.replace(/\/$/,'');// Ensure no leading slash on pathconstrelativePath=path.replace(/^\//,'');// Construct full pathurl.pathname=`${basePath}/${relativePath}`;returnurl.toString();}// UsageconstregionsUrl=constructMedusaUrl('store/regions');constresponse=awaitfetch(regionsUrl,{/* options */});
Alternative Considerations
Documentation Update: If base URLs with paths are not supported, updating the documentation to reflect this limitation would be helpful.
Error Handling: Adding warnings or errors when a base URL with a path is detected could prevent confusion.
Library Support: Ensure that the Medusa JavaScript client and other libraries correctly handle base URLs with paths.
Additional Information
Network Logs: Requests are being sent to https://api.example.com/store/regions instead of https://api.example.com/custom-path/store/regions.
Error Messages: The storefront displays errors related to unexpected responses or fails to load data.
Backend Accessibility: Accessing https://api.example.com/custom-path/store/regions directly returns the expected JSON response.
Request
Could you please advise on whether base URLs with custom paths are supported in the Medusa Next.js storefront? If so, implementing the proposed solution or providing guidance on the correct way to handle this scenario would be greatly appreciated.
Thank you for your time and assistance.
The text was updated successfully, but these errors were encountered:
jane-dot-flowers
changed the title
Title: Base URL Path Gets Overwritten When Constructing API Request URLs in Medusa Next.js Storefront
Base URL Path Gets Overwritten When Constructing API Request URLs in Medusa Next.js Storefront
Nov 18, 2024
I'm encountering an issue where the path specified in the
NEXT_PUBLIC_MEDUSA_BACKEND_URL
environment variable is being overwritten when constructing API request URLs. This leads to requests being sent to incorrect endpoints, resulting in errors.I have set up the
NEXT_PUBLIC_MEDUSA_BACKEND_URL
to point to a backend URL that includes a custom path:However, when the storefront makes API requests, the custom path (
/custom-path
) gets dropped, and the requests are sent to:instead of the expected:
This results in 404 Not Found errors or unexpected HTML responses.
Steps to Reproduce
Set
NEXT_PUBLIC_MEDUSA_BACKEND_URL
to a URL that includes a custom path:Start the Medusa backend server and ensure it's accessible at the specified URL with the custom path.
Run the Medusa Next.js storefront.
Access the storefront in the browser.
Observe the network requests being made to the backend.
Expected Behavior
API requests should preserve the custom path specified in the
NEXT_PUBLIC_MEDUSA_BACKEND_URL
environment variable. For example, requests to fetch regions should be sent to:Actual Behavior
The custom path is omitted, and requests are sent directly to the domain, resulting in incorrect URLs:
Analysis
The issue appears to be caused by the way URLs are constructed in the storefront code. When using template literals or the
URL
constructor with paths that start with a leading slash (/
), the existing path in the base URL gets overwritten.For example, in the middleware or API client:
If
BACKEND_URL
includes a path, appending/store/regions
results in the base path being discarded.Proposed Solution
Modify the URL construction logic to correctly handle base URLs that include paths. Here's an example of how this can be achieved:
Alternative Considerations
Additional Information
https://api.example.com/store/regions
instead ofhttps://api.example.com/custom-path/store/regions
.https://api.example.com/custom-path/store/regions
directly returns the expected JSON response.Request
Could you please advise on whether base URLs with custom paths are supported in the Medusa Next.js storefront? If so, implementing the proposed solution or providing guidance on the correct way to handle this scenario would be greatly appreciated.
Thank you for your time and assistance.
The text was updated successfully, but these errors were encountered: