Unverified Commit da07c724 authored by Gal Schlezinger's avatar Gal Schlezinger Committed by GitHub

[middleware] avoid fetching a chunk for api endpoints on middleware redirect (#37949)

avoid fetching a chunk for api endpoints on middleware redirect.
This still makes a request to the data endpoint, not sure if this is a good idea,
but a rewrite _can_ happen from that data request.

## Related

- Fixes #37944
- Fixes https://github.com/vercel/next.js/issues/37783



## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm lint`
- [ ] The examples guidelines are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing.md#adding-examples)
parent 82dce6ce
...@@ -790,7 +790,7 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -790,7 +790,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
_parsedUrl.query.__nextDefaultLocale = _parsedUrl.query.__nextDefaultLocale =
defaultLocale || this.nextConfig.i18n.defaultLocale defaultLocale || this.nextConfig.i18n.defaultLocale
if (!detectedLocale) { if (!detectedLocale && !this.router.catchAllMiddleware[0]) {
_parsedUrl.query.__nextLocale = _parsedUrl.query.__nextLocale =
_parsedUrl.query.__nextDefaultLocale _parsedUrl.query.__nextDefaultLocale
await this.render404(req, res, _parsedUrl) await this.render404(req, res, _parsedUrl)
......
...@@ -1153,7 +1153,7 @@ export default class Router implements BaseRouter { ...@@ -1153,7 +1153,7 @@ export default class Router implements BaseRouter {
// The build manifest needs to be loaded before auto-static dynamic pages // The build manifest needs to be loaded before auto-static dynamic pages
// get their query parameters to allow ensuring they can be parsed properly // get their query parameters to allow ensuring they can be parsed properly
// when rewritten to // when rewritten to
let pages: any, rewrites: any let pages: string[], rewrites: any
try { try {
;[pages, { __rewrites: rewrites }] = await Promise.all([ ;[pages, { __rewrites: rewrites }] = await Promise.all([
this.pageLoader.getPageList(), this.pageLoader.getPageList(),
...@@ -1733,6 +1733,11 @@ export default class Router implements BaseRouter { ...@@ -1733,6 +1733,11 @@ export default class Router implements BaseRouter {
: undefined : undefined
} }
if (route === '/api' || route.startsWith('/api/')) {
handleHardNavigation({ url: resolvedAs, router: this })
return new Promise<never>(() => {})
}
const routeInfo = const routeInfo =
cachedRouteInfo || cachedRouteInfo ||
(await this.fetchComponent(route).then<CompletePrivateRouteInfo>( (await this.fetchComponent(route).then<CompletePrivateRouteInfo>(
......
...@@ -36,6 +36,12 @@ export default function Home() { ...@@ -36,6 +36,12 @@ export default function Home() {
<a id="link-to-api-with-locale">Redirect me to api with locale</a> <a id="link-to-api-with-locale">Redirect me to api with locale</a>
</Link> </Link>
<div /> <div />
<Link href="/to?pathname=/old-home">
<a id="link-to-to-old-home">
Redirect me to a redirecting page of new version of page
</a>
</Link>
<div />
</div> </div>
) )
} }
...@@ -147,6 +147,12 @@ describe('Middleware Redirect', () => { ...@@ -147,6 +147,12 @@ describe('Middleware Redirect', () => {
await browser.elementByCss('#link-to-api-with-locale').click() await browser.elementByCss('#link-to-api-with-locale').click()
await browser.waitForCondition('window.location.pathname === "/api/ok"') await browser.waitForCondition('window.location.pathname === "/api/ok"')
await check(() => browser.elementByCss('body').text(), 'ok') await check(() => browser.elementByCss('body').text(), 'ok')
const logs = await browser.log()
const errors = logs
.filter((x) => x.source === 'error')
.map((x) => x.message)
.join('\n')
expect(errors).not.toContain('Failed to lookup route')
}) })
} }
}) })
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment