catto
November 30, 2024, 8:38pm
1
When fetching a post on a production next.js 15 hosted on Fly.io the URL that place in the fetch is changing & causing a 404 not found;
The request URL in the network tab of the chrome dev tools is:
https://www.rleaguez.com/organizationz/www.rleaguez.com/api/v2/organization
In console log I log out the url I am using & it’s:
https:www.rleaguez.com/api/v2/organization
The fetch code I am using is:
const postUrl = process.env.NEXT_PUBLIC_BASE_URL + '/api/v2/organization';
console.log("🚀 ~ CreateOrg ~ postUrl:", postUrl)
const orgResponse = await fetch(
postUrl,
{
method: 'POST',
headers: {
'content-type': 'application/json',
},
body: JSON.stringify(data),
},
);
Why does the URL change in production and does not stay the same as what I place in the code?
catto
November 30, 2024, 8:48pm
3
If I place this line:
‘https://www.rleaguez.com/api/v2/organization ’,
instead of the variable
postUrl,
then the fetch post will function correctly;
khuezy
November 30, 2024, 8:59pm
4
I mentioned this in your other thread…
This is not valid URL
catto
November 30, 2024, 9:25pm
5
@khuezy I appreciate the feedback; The URL https://www.rleaguez.com/api/v2/organization is a valid url for the API to post to; however not a valid frontend page;
I am wondering why the URL changes when it’s a variable in production but when the url to post to in the fetch is hardcoded it stays & is successful.
rubys
November 30, 2024, 9:28pm
6
The problem appears to be that NEXT_PUBLIC_BASE_URL
in production contains the full path.
Try:
const postUrl = new URL('/api/v2/organization', process.env.NEXT_PUBLIC_BASE_URL).toString()
khuezy
November 30, 2024, 9:30pm
7
You don’t have the two forward slashes, the fetch will fail as it’s an invalid protocol via the URL constructor
2 Likes
catto
November 30, 2024, 10:09pm
8
I appreciate all of the assistance! That is correct I did not have the 2 forward slashes; after I added that then the post url variable was correct & it too posted successfully. After reading this latest comment I realized it.
system
Closed
December 7, 2024, 10:09pm
9
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.