1

Obviously, this code always returns 404, because of fetch path 'api/registration/' + username, it doesn't work properly with related path .I don't see solution anywhere. Everybody just hardcode link like http://localhost:3000/api/registration/something but why should I hardcode it ? Is there any civilized way to fetch data from api of current server, where actually getServerSideProps was called? (that wouldn't work but anyway window.location.host+"/api/registration/something")

import axios from 'axios';
import { UserData } from '../src/common/types/user-types/UserData';

export default function UsernamePage(props) {
  console.log('Got props', props);
  return <div>Username Page</div>;
}

export async function getServerSideProps({ params: { username }, ...rest }) {
  console.log(rest);
  try {
    const { data } = await axios.get<UserData>('api/registration/' + username);
    return { props: { ...data } };
  } catch (e) {
    return { notFound: true };
  }
}
juliomalves
  • 42,130
  • 20
  • 150
  • 146
fake364
  • 99
  • 8
  • You shouldn't be making an axios request to an internal API route. See [Internal API fetch with getServerSideProps? (Next.js)](https://stackoverflow.com/questions/65752932/internal-api-fetch-with-getserversideprops-next-js). – juliomalves Aug 26 '22 at 19:17

0 Answers0