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
The Next.js starter middleware only accepts MEDUSA_BACKEND_URL but not NEXT_PUBLIC_MEDUSA_BACKEND_URL environment variable, causing confusion and errors when setting up the storefront. The middleware throws this error:
Error: Middleware.ts: Error fetching regions. Did you set up regions in your Medusa Admin and define a MEDUSA_BACKEND_URL environment variable? Note that the variable is no longer named NEXT_PUBLIC_MEDUSA_BACKEND_URL.
However, other parts of the codebase use NEXT_PUBLIC_MEDUSA_BACKEND_URL. This means users need to define both variables to get the application working, which is confusing and not well documented.
Expected behavior
The middleware should accept either environment variable name:
Or at least clearly explain in documentation that both are needed for different parts of the application.
Actual behavior
When only NEXT_PUBLIC_MEDUSA_BACKEND_URL is defined in .env.local (which is what most examples show), the middleware fails with:
Error: Middleware.ts: Error fetching regions. Did you set up regions in your Medusa Admin and define a MEDUSA_BACKEND_URL environment variable? Note that the variable is no longer named NEXT_PUBLIC_MEDUSA_BACKEND_URL.
This happens even though regions are properly set up and accessible via the API directly.
Both of these reference process.env.NEXT_PUBLIC_MEDUSA_BACKEND_URL to set the backend URL for SDK or API requests in the code samples. I’ve also seen similar patterns in other code samples under
www/apps/api-reference/specs/store/code_samples/JavaScript/.
That’s why I suggested supporting both variables in the middleware—to help avoid confusion for users who come across the NEXT_PUBLIC_ version in the docs or code samples.
I totally understand the concern about exposing backend URLs to the client. If you’d prefer a different way to handle this, or if some of these usages are just legacy and should be cleaned up, I’m more than happy to help update the code or clarify the docs.
Let me know what you think, and thanks again for your guidance!
The examples you mentioned appear to be generic ones related to the JS SDK, not specific to the Next.js Starter. As far as I can tell, we no longer reference NEXT_PUBLIC_MEDUSA_BACKEND_URL in the Starter or related docs. This was a deliberate change, so we're not planning to reintroduce support for that public variable.
The Starter should work as expected with just MEDUSA_BACKEND_URL set up. That said, you're of course free to use any environment variables that suit your project setup.
Package.json file
Node.js version
v20.16.0
Operating system name and version
MacOS Sequoia 14.4
Browser name
Chrome
What happended?
The Next.js starter middleware only accepts MEDUSA_BACKEND_URL but not NEXT_PUBLIC_MEDUSA_BACKEND_URL environment variable, causing confusion and errors when setting up the storefront. The middleware throws this error:
Error: Middleware.ts: Error fetching regions. Did you set up regions in your Medusa Admin and define a MEDUSA_BACKEND_URL environment variable? Note that the variable is no longer named NEXT_PUBLIC_MEDUSA_BACKEND_URL.
However, other parts of the codebase use NEXT_PUBLIC_MEDUSA_BACKEND_URL. This means users need to define both variables to get the application working, which is confusing and not well documented.
Expected behavior
The middleware should accept either environment variable name:
MEDUSA_BACKEND_URL (for middleware)
NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY (for client components)
Or at least clearly explain in documentation that both are needed for different parts of the application.
Actual behavior
When only NEXT_PUBLIC_MEDUSA_BACKEND_URL is defined in .env.local (which is what most examples show), the middleware fails with:
Error: Middleware.ts: Error fetching regions. Did you set up regions in your Medusa Admin and define a MEDUSA_BACKEND_URL environment variable? Note that the variable is no longer named NEXT_PUBLIC_MEDUSA_BACKEND_URL.
This happens even though regions are properly set up and accessible via the API directly.
Link to reproduction repo
Clone the repository: git clone https://github.com/medusajs/nextjs-starter-medusa.git
The text was updated successfully, but these errors were encountered: