# Redirects
Redirects are handled based on custom API response:
{
"redirectUrl": "/newUrl",
"statusCode": 301
}
# Issue with native redirects
There is no way to handle redirects in the universal way.
# Client side
On the client side browsers follow redirect for XHR or fetch automatically - from the frontend perspective we are not aware about the redirect. We can receive only 200 OK status code, but we don't know what was the full trace of the request.
# What is the issue ?
We have the same url path after we get 200 OK with the new content from the redirected page:
# Example scenario:
- You are already on the page
- Click "About us" link with
about-usurl - App call API
headless/about-usto get the page - API returns
statusCode: 307 location: /headless/about-us/contact - Browser follows the
/headless/about-us/contactand return new content - In the Browser URL bar we can see old url
/headless/about-us- becase we are not aware of the redirect
So in that case we would have to detect redirect and setup new url path for the new page - about-us/contact.
# Server Side
On the SSR context we would have to return the new page URL without API path in the url.
# Example scenario:
- Enter the page
http://mysite.com/about-us - APP call the API in the node context
headless/about-us - API returns
statusCode: 307 location: /headless/about-us/contact - APP in the node context see
statusCodeandlocation - APP in the node context make redirect to
locationheader - Browser get the new page with
/headless/about-us/contact- but this is the API endpoint, not frontend page
So in that case we would have to detect new url in step 4 and strip headless API path from the URL - or API should return new URL without headless part - but this is more complex because for the client side we should get headless in the url to call API correct.
# TYPO3 Headless way
The API response for the page that should be redirected should returns follow response:
{
"redirectUrl": "/newUrl",
"statusCode": 301
}
So in that case we can handle it in universal way on the response level.
On the SSR nuxt provides redirect method to make redirect in node context.
On the client side we can use just location.href to replace current url.
if (process.server) {
return context.redirect(response.data.statusCode || 301, response.data.redirectUrl)
} else if (process.client && typeof window !== 'undefined') {
window.location.href = response.data.redirectUrl
return context.next(false)
}