Next.js: Optional 308 redirect for /api pages

Created on 4 Aug 2020  路  1Comment  路  Source: vercel/next.js

Feature request

Make trailingSlashes optional for /api pages.

Is your feature request related to a problem? Please describe.

After updating to Next 9.5 and enabling trailing slashes my API calls started to report a 308 redirect header. This unfortunately broke some webhook integration because they weren't following the redirect.

Example:
/api/foo.js used to serve /api/foo, with trailingSlashes on /api/foo returns a 308 redirect to /api/foo/.

_A clear and concise description of what you want and what your use case is._
This isn't a huge issue for us since we just updated our various fetch calls, and integrations to use the trailing slashes, but it creates odd semantics around the API.

Describe the solution you'd like

  • Make trailingSlashes optional for API pages

Describe alternatives you've considered

  • The only alternative is to change our usage of the API

Additional context

N/A

Most helpful comment

IMHO I think there should be a way to deactivate the 308 in all situations, not only in /api. Perhaps a regex of paths, allowing to disable the redirects in all paths too.

I'm going to try to explain our case. Before this trailingSlash feature was landed, no redirect was done. With this, using a custom server we implemented the "trailingSlash" feature on the custom server for our project. However, with a small difference:

  • For pages that don't exist, it was returning the 404 status directly, without any redirection before.

old_behavior

We had implemented this in a similar way to:

server.all('*', (req, res) => {
 const redirectUrl = redirectToTrailingSlash(req)

 if (redirectUrl) {
    res.redirect(308, redirectUrl)
    return
  }

  req.url = clearTrailingSlash(req)
  handle(req, res)
})

However, now, with the new trailingSlash feature, this is different:

trailingslash

With this small change, it causes that pages like /whatever first redirect to /whatever/ and then to 404. Maybe it sounds like a little nonsense, but it's a change we don't want to take on right now because of SEO.

We reported this issue here https://github.com/vercel/next.js/issues/16586, but it was closed at once by @timneutkens, saying that it is the expected behavior.

The reason is that we don't want a small degradation in SEO. A 308 redirect triggers a new crawl action to googlebot, and when you manage a large set of URLs, you want to avoid useless redirects. And setting trailingSlash=false is not an option, as it now forces redirects in the opposite direction, from /whatever/ to /whatever.

behaviors

Since @timneutkens closed the issue and it seems that it's something that will not be changed (although I think it should be reconsidered), I think like you @michaelcamdencommon, that there should be a mechanism to be able to deactivate these redirects.

>All comments

IMHO I think there should be a way to deactivate the 308 in all situations, not only in /api. Perhaps a regex of paths, allowing to disable the redirects in all paths too.

I'm going to try to explain our case. Before this trailingSlash feature was landed, no redirect was done. With this, using a custom server we implemented the "trailingSlash" feature on the custom server for our project. However, with a small difference:

  • For pages that don't exist, it was returning the 404 status directly, without any redirection before.

old_behavior

We had implemented this in a similar way to:

server.all('*', (req, res) => {
 const redirectUrl = redirectToTrailingSlash(req)

 if (redirectUrl) {
    res.redirect(308, redirectUrl)
    return
  }

  req.url = clearTrailingSlash(req)
  handle(req, res)
})

However, now, with the new trailingSlash feature, this is different:

trailingslash

With this small change, it causes that pages like /whatever first redirect to /whatever/ and then to 404. Maybe it sounds like a little nonsense, but it's a change we don't want to take on right now because of SEO.

We reported this issue here https://github.com/vercel/next.js/issues/16586, but it was closed at once by @timneutkens, saying that it is the expected behavior.

The reason is that we don't want a small degradation in SEO. A 308 redirect triggers a new crawl action to googlebot, and when you manage a large set of URLs, you want to avoid useless redirects. And setting trailingSlash=false is not an option, as it now forces redirects in the opposite direction, from /whatever/ to /whatever.

behaviors

Since @timneutkens closed the issue and it seems that it's something that will not be changed (although I think it should be reconsidered), I think like you @michaelcamdencommon, that there should be a mechanism to be able to deactivate these redirects.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

irrigator picture irrigator  路  3Comments

YarivGilad picture YarivGilad  路  3Comments

olifante picture olifante  路  3Comments

flybayer picture flybayer  路  3Comments

knipferrc picture knipferrc  路  3Comments