how to change status code of getserverside props next.js?

154 Views Asked by At

I'm using next.js my project and I have dynamic routing using getServersideProps. This is what I want to do in index.tsx(image below) : If user write the URL in the address, when there is any upper case letter in the URL, then the page redirected to the URL with lowercase and set the status of response to 310. This is the folder structure:

folder structure

I tried this:

export async function getServerSideProps(context: any) {
const response=await axios(config)

 
  data=JSON.stringify(response.data)

if(context.params.title!=context.params.title.toLowerCase()){
    response.data.status=301;
    response.status=301
  
    return {
      redirect: {
        
        destination:encodeURI(`/blog/${id}/${standardUrl(JSON.parse(data).title)}`) ,
       statusCode:301
      },
    
    };

  }
}

This redirects to the lowercase version perfectly, but when I write the URL in postman as a get command (for example this URL: localhost:3000.com/blog/1/How-to-understand-You-are-a-good-developer), I received 200 status not 301!

How to change the received status to 301?

0

There are 0 best solutions below