rsnext/test/e2e/link-with-api-rewrite
Wyatt Johnson 329f6cb9ac
Handle Client Rewrites Correctly (#38340)
When an API route is detected for the `getRouteInfo` method (a route with a `/api` prefix), we should redirect the user to the original destination instead of the rewritten destination. This makes the behaviour consistent with how rewrites have been documented thus far.

The reproduction described in #37783 now causes an invariant violation error (for redirecting to the same URL), but this is instead related to the fact that the router (when rehydrated) attempts to redirect the user again to the correct destination. This should be corrected either in this PR or in a future one that addresses the hydration behaviour directly.

## Bug

- Related to #37783
- Related to #37949

## Example

With the following `next.config.js`:

```js
/** @type {import('next').NextConfig} */
const nextConfig = {
  reactStrictMode: true,
  async rewrites() {
    return {
      beforeFiles: [
        {
          source: '/:path(.*)',
          has: [{ type: 'query', key: 'json', value: 'true' }],
          destination: '/api/json?from=:path',
        },
      ],
    }
  },
}

module.exports = nextConfig
```

The following link:

```jsx
<Link href="/not/real?json=true">
  <a>Take me to JSON</a>
</Link>
```

When clicked, will navigate the user to `/not/real?json=true` and have it's contents served by `/api/json` and not simply redirected to `/api/json` which is incorrect.

Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
2022-07-05 22:44:38 +00:00
..
app Handle Client Rewrites Correctly (#38340) 2022-07-05 22:44:38 +00:00
index.test.ts Handle Client Rewrites Correctly (#38340) 2022-07-05 22:44:38 +00:00