Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure external beforeFiles rewrites are handled with next/link #33888

Merged
merged 2 commits into from Feb 2, 2022

Conversation

ijjk
Copy link
Member

@ijjk ijjk commented Feb 2, 2022

This ensures we properly handle external beforeFiles rewrites client-side so that a different result doesn't occur client-side versus on a direct visit.

Bug

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

Fixes: #32348

@ijjk ijjk added created-by: Next.js team type: next labels Feb 2, 2022
@ijjk

This comment has been minimized.

@ijjk ijjk marked this pull request as ready for review Feb 2, 2022
@ijjk ijjk requested review from huozhi, shuding and timneutkens as code owners Feb 2, 2022
@ijjk ijjk requested review from balazsorban44 and styfle Feb 2, 2022
huozhi
huozhi approved these changes Feb 2, 2022
@ijjk
Copy link
Member Author

ijjk commented Feb 2, 2022

Stats from current PR

Default Build (Increase detected ⚠️)
General Overall increase ⚠️
vercel/next.js canary ijjk/next.js fix/external-beforefiles Change
buildDuration 12.9s 12.6s -278ms
buildDurationCached 3.2s 3.2s -36ms
nodeModulesSize 359 MB 359 MB ⚠️ +645 B
Page Load Tests Overall increase ✓
vercel/next.js canary ijjk/next.js fix/external-beforefiles Change
/ failed reqs 0 0
/ total time (seconds) 2.956 2.974 ⚠️ +0.02
/ avg req/sec 845.63 840.59 ⚠️ -5.04
/error-in-render failed reqs 0 0
/error-in-render total time (seconds) 1.409 1.385 -0.02
/error-in-render avg req/sec 1774.36 1804.6 +30.24
Client Bundles (main, webpack, commons) Overall increase ⚠️
vercel/next.js canary ijjk/next.js fix/external-beforefiles Change
450.HASH.js gzip 179 B 179 B
framework-HASH.js gzip 42.2 kB 42.2 kB
main-HASH.js gzip 27.2 kB 27.3 kB ⚠️ +83 B
webpack-HASH.js gzip 1.44 kB 1.44 kB
Overall change 71.1 kB 71.2 kB ⚠️ +83 B
Legacy Client Bundles (polyfills)
vercel/next.js canary ijjk/next.js fix/external-beforefiles Change
polyfills-HASH.js gzip 31 kB 31 kB
Overall change 31 kB 31 kB
Client Pages
vercel/next.js canary ijjk/next.js fix/external-beforefiles Change
_app-HASH.js gzip 1.37 kB 1.37 kB
_error-HASH.js gzip 194 B 194 B
amp-HASH.js gzip 312 B 312 B
css-HASH.js gzip 326 B 326 B
dynamic-HASH.js gzip 2.37 kB 2.37 kB
head-HASH.js gzip 350 B 350 B
hooks-HASH.js gzip 919 B 919 B
image-HASH.js gzip 4.94 kB 4.94 kB
index-HASH.js gzip 263 B 263 B
link-HASH.js gzip 2.19 kB 2.19 kB
routerDirect..HASH.js gzip 321 B 321 B
script-HASH.js gzip 383 B 383 B
withRouter-HASH.js gzip 318 B 318 B
85e02e95b279..7e3.css gzip 107 B 107 B
Overall change 14.3 kB 14.3 kB
Client Build Manifests
vercel/next.js canary ijjk/next.js fix/external-beforefiles Change
_buildManifest.js gzip 459 B 459 B
Overall change 459 B 459 B
Rendered Page Sizes Overall increase ⚠️
vercel/next.js canary ijjk/next.js fix/external-beforefiles Change
index.html gzip 529 B 532 B ⚠️ +3 B
link.html gzip 544 B 546 B ⚠️ +2 B
withRouter.html gzip 525 B 527 B ⚠️ +2 B
Overall change 1.6 kB 1.6 kB ⚠️ +7 B

Diffs

Diff for main-HASH.js
@@ -5064,29 +5064,65 @@
                                   delBasePath(pathname)
                                 )
                               : pathname;
-                            if (shouldResolveHref && pathname !== "/_error") {
-                              options._shouldResolveHref = true;
-                              if (false) {
-                              } else {
-                                parsed.pathname = resolveDynamicRoute(
-                                  pathname,
-                                  pages
-                                );
-                                if (parsed.pathname !== pathname) {
-                                  pathname = parsed.pathname;
-                                  parsed.pathname = addBasePath(pathname);
-                                  url = (0, _utils).formatWithValidation(
-                                    parsed
-                                  );
-                                }
-                              }
+                            if (
+                              !(shouldResolveHref && pathname !== "/_error")
+                            ) {
+                              _ctx.next = 69;
+                              break;
+                            }
+                            options._shouldResolveHref = true;
+                            if (true) {
+                              _ctx.next = 68;
+                              break;
+                            }
+                            rewritesResult = (0, _resolveRewrites).default(
+                              addBasePath(addLocale(cleanedAs, _this.locale)),
+                              pages,
+                              rewrites,
+                              query,
+                              function(p) {
+                                return resolveDynamicRoute(p, pages);
+                              },
+                              _this.locales
+                            );
+                            if (!rewritesResult.externalDest) {
+                              _ctx.next = 64;
+                              break;
+                            }
+                            location.href = as;
+                            return _ctx.abrupt("return", true);
+                          case 64:
+                            resolvedAs = rewritesResult.asPath;
+                            if (
+                              rewritesResult.matchedPage &&
+                              rewritesResult.resolvedHref
+                            ) {
+                              // if this directly matches a page we need to update the href to
+                              // allow the correct page chunk to be loaded
+                              pathname = rewritesResult.resolvedHref;
+                              parsed.pathname = addBasePath(pathname);
+                              url = (0, _utils).formatWithValidation(parsed);
+                            }
+                            _ctx.next = 69;
+                            break;
+                          case 68: {
+                            parsed.pathname = resolveDynamicRoute(
+                              pathname,
+                              pages
+                            );
+                            if (parsed.pathname !== pathname) {
+                              pathname = parsed.pathname;
+                              parsed.pathname = addBasePath(pathname);
+                              url = (0, _utils).formatWithValidation(parsed);
                             }
+                          }
+                          case 69:
                             if (isLocalURL(as)) {
-                              _ctx.next = 63;
+                              _ctx.next = 74;
                               break;
                             }
                             if (true) {
-                              _ctx.next = 61;
+                              _ctx.next = 72;
                               break;
                             }
                             throw new Error(
@@ -5098,10 +5134,10 @@
                                 ) +
                                 "\nSee more info: https://nextjs.org/docs/messages/invalid-relative-url-external-as"
                             );
-                          case 61:
+                          case 72:
                             window.location.href = as;
                             return _ctx.abrupt("return", false);
-                          case 63:
+                          case 74:
                             resolvedAs = delLocale(
                               delBasePath(resolvedAs),
                               _this.locale
@@ -5117,10 +5153,10 @@
                                 )
                               )
                             ) {
-                              _ctx.next = 84;
+                              _ctx.next = 95;
                               break;
                             }
-                            _ctx.next = 67;
+                            _ctx.next = 78;
                             return _this._preflightRequest({
                               as: as,
                               cache: "production" === "production",
@@ -5128,10 +5164,10 @@
                               pathname: pathname,
                               query: query
                             });
-                          case 67:
+                          case 78:
                             effect = _ctx.sent;
                             if (!(effect.type === "rewrite")) {
-                              _ctx.next = 72;
+                              _ctx.next = 83;
                               break;
                             }
                             {
@@ -5145,11 +5181,11 @@
                               parsed.pathname = effect.resolvedHref;
                               url = (0, _utils).formatWithValidation(parsed);
                             }
-                            _ctx.next = 84;
+                            _ctx.next = 95;
                             break;
-                          case 72:
+                          case 83:
                             if (!(effect.type === "redirect" && effect.newAs)) {
-                              _ctx.next = 76;
+                              _ctx.next = 87;
                               break;
                             }
                             return _ctx.abrupt(
@@ -5161,13 +5197,13 @@
                                 options
                               )
                             );
-                          case 76:
+                          case 87:
                             if (
                               !(
                                 effect.type === "redirect" && effect.destination
                               )
                             ) {
-                              _ctx.next = 81;
+                              _ctx.next = 92;
                               break;
                             }
                             window.location.href = effect.destination;
@@ -5175,14 +5211,14 @@
                               "return",
                               new Promise(function() {})
                             );
-                          case 81:
+                          case 92:
                             if (
                               !(
                                 effect.type === "refresh" &&
                                 as !== window.location.pathname
                               )
                             ) {
-                              _ctx.next = 84;
+                              _ctx.next = 95;
                               break;
                             }
                             window.location.href = as;
@@ -5190,13 +5226,13 @@
                               "return",
                               new Promise(function() {})
                             );
-                          case 84:
+                          case 95:
                             route = (0,
                             _normalizeTrailingSlash).removePathTrailingSlash(
                               pathname
                             );
                             if (!(0, _isDynamic).isDynamicRoute(route)) {
-                              _ctx.next = 100;
+                              _ctx.next = 111;
                               break;
                             }
                             parsedAs1 = (0, _parseRelativeUrl).parseRelativeUrl(
@@ -5217,7 +5253,7 @@
                                 (shouldInterpolate && !interpolatedAs.result)
                               )
                             ) {
-                              _ctx.next = 99;
+                              _ctx.next = 110;
                               break;
                             }
                             missingParams = Object.keys(
@@ -5226,7 +5262,7 @@
                               return !query[param];
                             });
                             if (!(missingParams.length > 0)) {
-                              _ctx.next = 97;
+                              _ctx.next = 108;
                               break;
                             }
                             if (false) {
@@ -5254,10 +5290,10 @@
                                     : "incompatible-href-as"
                                 )
                             );
-                          case 97:
-                            _ctx.next = 100;
+                          case 108:
+                            _ctx.next = 111;
                             break;
-                          case 99:
+                          case 110:
                             if (shouldInterpolate) {
                               as = (0, _utils).formatWithValidation(
                                 Object.assign({}, parsedAs1, {
@@ -5272,14 +5308,14 @@
                               // Merge params into `query`, overwriting any specified in search
                               Object.assign(query, routeMatch);
                             }
-                          case 100:
+                          case 111:
                             Router.events.emit(
                               "routeChangeStart",
                               as,
                               routeProps
                             );
-                            _ctx.prev = 101;
-                            _ctx.next = 105;
+                            _ctx.prev = 112;
+                            _ctx.next = 116;
                             return _this.getRouteInfo(
                               route,
                               pathname,
@@ -5288,20 +5324,20 @@
                               resolvedAs,
                               routeProps
                             );
-                          case 105:
+                          case 116:
                             routeInfo = _ctx.sent;
                             (error = routeInfo.error),
                               (props = routeInfo.props),
                               (__N_SSG = routeInfo.__N_SSG),
                               (__N_SSP = routeInfo.__N_SSP);
                             if (!((__N_SSG || __N_SSP) && props)) {
-                              _ctx.next = 132;
+                              _ctx.next = 143;
                               break;
                             }
                             if (
                               !(props.pageProps && props.pageProps.__N_REDIRECT)
                             ) {
-                              _ctx.next = 117;
+                              _ctx.next = 128;
                               break;
                             }
                             destination = props.pageProps.__N_REDIRECT;
@@ -5311,7 +5347,7 @@
                                 props.pageProps.__N_REDIRECT_BASE_PATH !== false
                               )
                             ) {
-                              _ctx.next = 115;
+                              _ctx.next = 126;
                               break;
                             }
                             parsedHref = (0,
@@ -5331,31 +5367,31 @@
                               "return",
                               _this.change(method, newUrl, newAs, options)
                             );
-                          case 115:
+                          case 126:
                             window.location.href = destination;
                             return _ctx.abrupt(
                               "return",
                               new Promise(function() {})
                             );
-                          case 117:
+                          case 128:
                             _this.isPreview = !!props.__N_PREVIEW;
                             if (!(props.notFound === SSG_DATA_NOT_FOUND)) {
-                              _ctx.next = 132;
+                              _ctx.next = 143;
                               break;
                             }
-                            _ctx.prev = 120;
-                            _ctx.next = 123;
+                            _ctx.prev = 131;
+                            _ctx.next = 134;
                             return _this.fetchComponent("/404");
-                          case 123:
+                          case 134:
                             notFoundRoute = "/404";
-                            _ctx.next = 129;
+                            _ctx.next = 140;
                             break;
-                          case 126:
-                            _ctx.prev = 126;
-                            _ctx.t3 = _ctx["catch"](120);
+                          case 137:
+                            _ctx.prev = 137;
+                            _ctx.t3 = _ctx["catch"](131);
                             notFoundRoute = "/_error";
-                          case 129:
-                            _ctx.next = 131;
+                          case 140:
+                            _ctx.next = 142;
                             return _this.getRouteInfo(
                               notFoundRoute,
                               notFoundRoute,
@@ -5366,9 +5402,9 @@
                                 shallow: false
                               }
                             );
-                          case 131:
+                          case 142:
                             routeInfo = _ctx.sent;
-                          case 132:
+                          case 143:
                             Router.events.emit(
                               "beforeHistoryChange",
                               as,
@@ -5406,7 +5442,7 @@
                                   y: 0
                                 }
                               : null;
-                            _ctx.next = 141;
+                            _ctx.next = 152;
                             return _this
                               .set(
                                 route,
@@ -5422,9 +5458,9 @@
                                 if (e.cancelled) error = error || e;
                                 else throw e;
                               });
-                          case 141:
+                          case 152:
                             if (!error) {
-                              _ctx.next = 144;
+                              _ctx.next = 155;
                               break;
                             }
                             Router.events.emit(
@@ -5434,7 +5470,7 @@
                               routeProps
                             );
                             throw error;
-                          case 144:
+                          case 155:
                             if (false) {
                             }
                             Router.events.emit(
@@ -5443,22 +5479,22 @@
                               routeProps
                             );
                             return _ctx.abrupt("return", true);
-                          case 149:
-                            _ctx.prev = 149;
-                            _ctx.t4 = _ctx["catch"](101);
+                          case 160:
+                            _ctx.prev = 160;
+                            _ctx.t4 = _ctx["catch"](112);
                             if (
                               !(
                                 (0, _isError).default(_ctx.t4) &&
                                 _ctx.t4.cancelled
                               )
                             ) {
-                              _ctx.next = 153;
+                              _ctx.next = 164;
                               break;
                             }
                             return _ctx.abrupt("return", false);
-                          case 153:
+                          case 164:
                             throw _ctx.t4;
-                          case 154:
+                          case 165:
                           case "end":
                             return _ctx.stop();
                         }
@@ -5467,8 +5503,8 @@
                     null,
                     [
                       [38, 50],
-                      [101, 149],
-                      [120, 126]
+                      [112, 160],
+                      [131, 137]
                     ]
                   );
                 })
@@ -5944,7 +5980,7 @@
                           pages = _ctx.sent;
                           resolvedAs = asPath;
                           if (true) {
-                            _ctx.next = 20;
+                            _ctx.next = 22;
                             break;
                           }
                           _ctx.next = 12;
@@ -5963,6 +5999,12 @@
                             },
                             _this.locales
                           );
+                          if (!rewritesResult.externalDest) {
+                            _ctx.next = 18;
+                            break;
+                          }
+                          return _ctx.abrupt("return");
+                        case 18:
                           resolvedAs = delLocale(
                             delBasePath(rewritesResult.asPath),
                             _this.locale
@@ -5977,9 +6019,9 @@
                             parsed.pathname = pathname;
                             url = (0, _utils).formatWithValidation(parsed);
                           }
-                          _ctx.next = 21;
+                          _ctx.next = 23;
                           break;
-                        case 20: {
+                        case 22: {
                           parsed.pathname = resolveDynamicRoute(
                             parsed.pathname,
                             pages
@@ -5990,14 +6032,14 @@
                             url = (0, _utils).formatWithValidation(parsed);
                           }
                         }
-                        case 21:
+                        case 23:
                           if (true) {
-                            _ctx.next = 23;
+                            _ctx.next = 25;
                             break;
                           }
                           return _ctx.abrupt("return");
-                        case 23:
-                          _ctx.next = 25;
+                        case 25:
+                          _ctx.next = 27;
                           return _this._preflightRequest({
                             as: addBasePath(asPath),
                             cache: true,
@@ -6005,7 +6047,7 @@
                             pathname: pathname,
                             query: query
                           });
-                        case 25:
+                        case 27:
                           effects = _ctx.sent;
                           if (effects.type === "rewrite") {
                             parsed.pathname = effects.resolvedHref;
@@ -6022,7 +6064,7 @@
                           _normalizeTrailingSlash).removePathTrailingSlash(
                             pathname
                           );
-                          _ctx.next = 30;
+                          _ctx.next = 32;
                           return Promise.all([
                             _this.pageLoader
                               ._isSsg(route)
@@ -6049,7 +6091,7 @@
                               options.priority ? "loadPage" : "prefetch"
                             ](route)
                           ]);
-                        case 30:
+                        case 32:
                         case "end":
                           return _ctx.stop();
                       }
Diff for index.html
@@ -19,7 +19,7 @@
       defer=""
     ></script>
     <script
-      src="/_next/static/chunks/main-4b217ddd923fa165.js"
+      src="/_next/static/chunks/main-98b55543970d6b99.js"
       defer=""
     ></script>
     <script
Diff for link.html
@@ -19,7 +19,7 @@
       defer=""
     ></script>
     <script
-      src="/_next/static/chunks/main-4b217ddd923fa165.js"
+      src="/_next/static/chunks/main-98b55543970d6b99.js"
       defer=""
     ></script>
     <script
Diff for withRouter.html
@@ -19,7 +19,7 @@
       defer=""
     ></script>
     <script
-      src="/_next/static/chunks/main-4b217ddd923fa165.js"
+      src="/_next/static/chunks/main-98b55543970d6b99.js"
       defer=""
     ></script>
     <script

Default Build with SWC (Increase detected ⚠️)
General Overall increase ⚠️
vercel/next.js canary ijjk/next.js fix/external-beforefiles Change
buildDuration 15.7s 15.8s ⚠️ +112ms
buildDurationCached 3.2s 3.2s ⚠️ +43ms
nodeModulesSize 359 MB 359 MB ⚠️ +645 B
Page Load Tests Overall decrease ⚠️
vercel/next.js canary ijjk/next.js fix/external-beforefiles Change
/ failed reqs 0 0
/ total time (seconds) 2.969 2.97 0
/ avg req/sec 842.01 841.62 ⚠️ -0.39
/error-in-render failed reqs 0 0
/error-in-render total time (seconds) 1.353 1.39 ⚠️ +0.04
/error-in-render avg req/sec 1848.19 1798.87 ⚠️ -49.32
Client Bundles (main, webpack, commons) Overall increase ⚠️
vercel/next.js canary ijjk/next.js fix/external-beforefiles Change
450.HASH.js gzip 179 B 179 B
framework-HASH.js gzip 42.3 kB 42.3 kB
main-HASH.js gzip 27.4 kB 27.5 kB ⚠️ +81 B
webpack-HASH.js gzip 1.44 kB 1.44 kB
Overall change 71.3 kB 71.4 kB ⚠️ +81 B
Legacy Client Bundles (polyfills)
vercel/next.js canary ijjk/next.js fix/external-beforefiles Change
polyfills-HASH.js gzip 31 kB 31 kB
Overall change 31 kB 31 kB
Client Pages
vercel/next.js canary ijjk/next.js fix/external-beforefiles Change
_app-HASH.js gzip 1.35 kB 1.35 kB
_error-HASH.js gzip 180 B 180 B
amp-HASH.js gzip 305 B 305 B
css-HASH.js gzip 321 B 321 B
dynamic-HASH.js gzip 2.36 kB 2.36 kB
head-HASH.js gzip 342 B 342 B
hooks-HASH.js gzip 911 B 911 B
image-HASH.js gzip 4.98 kB 4.98 kB
index-HASH.js gzip 256 B 256 B
link-HASH.js gzip 2.21 kB 2.21 kB
routerDirect..HASH.js gzip 314 B 314 B
script-HASH.js gzip 375 B 375 B
withRouter-HASH.js gzip 309 B 309 B
85e02e95b279..7e3.css gzip 107 B 107 B
Overall change 14.3 kB 14.3 kB
Client Build Manifests
vercel/next.js canary ijjk/next.js fix/external-beforefiles Change
_buildManifest.js gzip 458 B 458 B
Overall change 458 B 458 B
Rendered Page Sizes Overall increase ⚠️
vercel/next.js canary ijjk/next.js fix/external-beforefiles Change
index.html gzip 530 B 530 B
link.html gzip 542 B 543 B ⚠️ +1 B
withRouter.html gzip 524 B 525 B ⚠️ +1 B
Overall change 1.6 kB 1.6 kB ⚠️ +2 B

Diffs

Diff for main-HASH.js
@@ -5064,29 +5064,65 @@
                                   delBasePath(pathname)
                                 )
                               : pathname;
-                            if (shouldResolveHref && pathname !== "/_error") {
-                              options._shouldResolveHref = true;
-                              if (false) {
-                              } else {
-                                parsed.pathname = resolveDynamicRoute(
-                                  pathname,
-                                  pages
-                                );
-                                if (parsed.pathname !== pathname) {
-                                  pathname = parsed.pathname;
-                                  parsed.pathname = addBasePath(pathname);
-                                  url = (0, _utils).formatWithValidation(
-                                    parsed
-                                  );
-                                }
-                              }
+                            if (
+                              !(shouldResolveHref && pathname !== "/_error")
+                            ) {
+                              _ctx.next = 69;
+                              break;
+                            }
+                            options._shouldResolveHref = true;
+                            if (true) {
+                              _ctx.next = 68;
+                              break;
+                            }
+                            rewritesResult = (0, _resolveRewrites).default(
+                              addBasePath(addLocale(cleanedAs, _this.locale)),
+                              pages,
+                              rewrites,
+                              query,
+                              function(p) {
+                                return resolveDynamicRoute(p, pages);
+                              },
+                              _this.locales
+                            );
+                            if (!rewritesResult.externalDest) {
+                              _ctx.next = 64;
+                              break;
+                            }
+                            location.href = as;
+                            return _ctx.abrupt("return", true);
+                          case 64:
+                            resolvedAs = rewritesResult.asPath;
+                            if (
+                              rewritesResult.matchedPage &&
+                              rewritesResult.resolvedHref
+                            ) {
+                              // if this directly matches a page we need to update the href to
+                              // allow the correct page chunk to be loaded
+                              pathname = rewritesResult.resolvedHref;
+                              parsed.pathname = addBasePath(pathname);
+                              url = (0, _utils).formatWithValidation(parsed);
+                            }
+                            _ctx.next = 69;
+                            break;
+                          case 68: {
+                            parsed.pathname = resolveDynamicRoute(
+                              pathname,
+                              pages
+                            );
+                            if (parsed.pathname !== pathname) {
+                              pathname = parsed.pathname;
+                              parsed.pathname = addBasePath(pathname);
+                              url = (0, _utils).formatWithValidation(parsed);
                             }
+                          }
+                          case 69:
                             if (isLocalURL(as)) {
-                              _ctx.next = 63;
+                              _ctx.next = 74;
                               break;
                             }
                             if (true) {
-                              _ctx.next = 61;
+                              _ctx.next = 72;
                               break;
                             }
                             throw new Error(
@@ -5098,10 +5134,10 @@
                                 ) +
                                 "\nSee more info: https://nextjs.org/docs/messages/invalid-relative-url-external-as"
                             );
-                          case 61:
+                          case 72:
                             window.location.href = as;
                             return _ctx.abrupt("return", false);
-                          case 63:
+                          case 74:
                             resolvedAs = delLocale(
                               delBasePath(resolvedAs),
                               _this.locale
@@ -5117,10 +5153,10 @@
                                 )
                               )
                             ) {
-                              _ctx.next = 84;
+                              _ctx.next = 95;
                               break;
                             }
-                            _ctx.next = 67;
+                            _ctx.next = 78;
                             return _this._preflightRequest({
                               as: as,
                               cache: "production" === "production",
@@ -5128,10 +5164,10 @@
                               pathname: pathname,
                               query: query
                             });
-                          case 67:
+                          case 78:
                             effect = _ctx.sent;
                             if (!(effect.type === "rewrite")) {
-                              _ctx.next = 72;
+                              _ctx.next = 83;
                               break;
                             }
                             {
@@ -5145,11 +5181,11 @@
                               parsed.pathname = effect.resolvedHref;
                               url = (0, _utils).formatWithValidation(parsed);
                             }
-                            _ctx.next = 84;
+                            _ctx.next = 95;
                             break;
-                          case 72:
+                          case 83:
                             if (!(effect.type === "redirect" && effect.newAs)) {
-                              _ctx.next = 76;
+                              _ctx.next = 87;
                               break;
                             }
                             return _ctx.abrupt(
@@ -5161,13 +5197,13 @@
                                 options
                               )
                             );
-                          case 76:
+                          case 87:
                             if (
                               !(
                                 effect.type === "redirect" && effect.destination
                               )
                             ) {
-                              _ctx.next = 81;
+                              _ctx.next = 92;
                               break;
                             }
                             window.location.href = effect.destination;
@@ -5175,14 +5211,14 @@
                               "return",
                               new Promise(function() {})
                             );
-                          case 81:
+                          case 92:
                             if (
                               !(
                                 effect.type === "refresh" &&
                                 as !== window.location.pathname
                               )
                             ) {
-                              _ctx.next = 84;
+                              _ctx.next = 95;
                               break;
                             }
                             window.location.href = as;
@@ -5190,13 +5226,13 @@
                               "return",
                               new Promise(function() {})
                             );
-                          case 84:
+                          case 95:
                             route = (0,
                             _normalizeTrailingSlash).removePathTrailingSlash(
                               pathname
                             );
                             if (!(0, _isDynamic).isDynamicRoute(route)) {
-                              _ctx.next = 100;
+                              _ctx.next = 111;
                               break;
                             }
                             parsedAs1 = (0, _parseRelativeUrl).parseRelativeUrl(
@@ -5217,7 +5253,7 @@
                                 (shouldInterpolate && !interpolatedAs.result)
                               )
                             ) {
-                              _ctx.next = 99;
+                              _ctx.next = 110;
                               break;
                             }
                             missingParams = Object.keys(
@@ -5226,7 +5262,7 @@
                               return !query[param];
                             });
                             if (!(missingParams.length > 0)) {
-                              _ctx.next = 97;
+                              _ctx.next = 108;
                               break;
                             }
                             if (false) {
@@ -5254,10 +5290,10 @@
                                     : "incompatible-href-as"
                                 )
                             );
-                          case 97:
-                            _ctx.next = 100;
+                          case 108:
+                            _ctx.next = 111;
                             break;
-                          case 99:
+                          case 110:
                             if (shouldInterpolate) {
                               as = (0, _utils).formatWithValidation(
                                 Object.assign({}, parsedAs1, {
@@ -5272,14 +5308,14 @@
                               // Merge params into `query`, overwriting any specified in search
                               Object.assign(query, routeMatch);
                             }
-                          case 100:
+                          case 111:
                             Router.events.emit(
                               "routeChangeStart",
                               as,
                               routeProps
                             );
-                            _ctx.prev = 101;
-                            _ctx.next = 105;
+                            _ctx.prev = 112;
+                            _ctx.next = 116;
                             return _this.getRouteInfo(
                               route,
                               pathname,
@@ -5288,20 +5324,20 @@
                               resolvedAs,
                               routeProps
                             );
-                          case 105:
+                          case 116:
                             routeInfo = _ctx.sent;
                             (error = routeInfo.error),
                               (props = routeInfo.props),
                               (__N_SSG = routeInfo.__N_SSG),
                               (__N_SSP = routeInfo.__N_SSP);
                             if (!((__N_SSG || __N_SSP) && props)) {
-                              _ctx.next = 132;
+                              _ctx.next = 143;
                               break;
                             }
                             if (
                               !(props.pageProps && props.pageProps.__N_REDIRECT)
                             ) {
-                              _ctx.next = 117;
+                              _ctx.next = 128;
                               break;
                             }
                             destination = props.pageProps.__N_REDIRECT;
@@ -5311,7 +5347,7 @@
                                 props.pageProps.__N_REDIRECT_BASE_PATH !== false
                               )
                             ) {
-                              _ctx.next = 115;
+                              _ctx.next = 126;
                               break;
                             }
                             parsedHref = (0,
@@ -5331,31 +5367,31 @@
                               "return",
                               _this.change(method, newUrl, newAs, options)
                             );
-                          case 115:
+                          case 126:
                             window.location.href = destination;
                             return _ctx.abrupt(
                               "return",
                               new Promise(function() {})
                             );
-                          case 117:
+                          case 128:
                             _this.isPreview = !!props.__N_PREVIEW;
                             if (!(props.notFound === SSG_DATA_NOT_FOUND)) {
-                              _ctx.next = 132;
+                              _ctx.next = 143;
                               break;
                             }
-                            _ctx.prev = 120;
-                            _ctx.next = 123;
+                            _ctx.prev = 131;
+                            _ctx.next = 134;
                             return _this.fetchComponent("/404");
-                          case 123:
+                          case 134:
                             notFoundRoute = "/404";
-                            _ctx.next = 129;
+                            _ctx.next = 140;
                             break;
-                          case 126:
-                            _ctx.prev = 126;
-                            _ctx.t3 = _ctx["catch"](120);
+                          case 137:
+                            _ctx.prev = 137;
+                            _ctx.t3 = _ctx["catch"](131);
                             notFoundRoute = "/_error";
-                          case 129:
-                            _ctx.next = 131;
+                          case 140:
+                            _ctx.next = 142;
                             return _this.getRouteInfo(
                               notFoundRoute,
                               notFoundRoute,
@@ -5366,9 +5402,9 @@
                                 shallow: false
                               }
                             );
-                          case 131:
+                          case 142:
                             routeInfo = _ctx.sent;
-                          case 132:
+                          case 143:
                             Router.events.emit(
                               "beforeHistoryChange",
                               as,
@@ -5406,7 +5442,7 @@
                                   y: 0
                                 }
                               : null;
-                            _ctx.next = 141;
+                            _ctx.next = 152;
                             return _this
                               .set(
                                 route,
@@ -5422,9 +5458,9 @@
                                 if (e.cancelled) error = error || e;
                                 else throw e;
                               });
-                          case 141:
+                          case 152:
                             if (!error) {
-                              _ctx.next = 144;
+                              _ctx.next = 155;
                               break;
                             }
                             Router.events.emit(
@@ -5434,7 +5470,7 @@
                               routeProps
                             );
                             throw error;
-                          case 144:
+                          case 155:
                             if (false) {
                             }
                             Router.events.emit(
@@ -5443,22 +5479,22 @@
                               routeProps
                             );
                             return _ctx.abrupt("return", true);
-                          case 149:
-                            _ctx.prev = 149;
-                            _ctx.t4 = _ctx["catch"](101);
+                          case 160:
+                            _ctx.prev = 160;
+                            _ctx.t4 = _ctx["catch"](112);
                             if (
                               !(
                                 (0, _isError).default(_ctx.t4) &&
                                 _ctx.t4.cancelled
                               )
                             ) {
-                              _ctx.next = 153;
+                              _ctx.next = 164;
                               break;
                             }
                             return _ctx.abrupt("return", false);
-                          case 153:
+                          case 164:
                             throw _ctx.t4;
-                          case 154:
+                          case 165:
                           case "end":
                             return _ctx.stop();
                         }
@@ -5467,8 +5503,8 @@
                     null,
                     [
                       [38, 50],
-                      [101, 149],
-                      [120, 126]
+                      [112, 160],
+                      [131, 137]
                     ]
                   );
                 })
@@ -5944,7 +5980,7 @@
                           pages = _ctx.sent;
                           resolvedAs = asPath;
                           if (true) {
-                            _ctx.next = 20;
+                            _ctx.next = 22;
                             break;
                           }
                           _ctx.next = 12;
@@ -5963,6 +5999,12 @@
                             },
                             _this.locales
                           );
+                          if (!rewritesResult.externalDest) {
+                            _ctx.next = 18;
+                            break;
+                          }
+                          return _ctx.abrupt("return");
+                        case 18:
                           resolvedAs = delLocale(
                             delBasePath(rewritesResult.asPath),
                             _this.locale
@@ -5977,9 +6019,9 @@
                             parsed.pathname = pathname;
                             url = (0, _utils).formatWithValidation(parsed);
                           }
-                          _ctx.next = 21;
+                          _ctx.next = 23;
                           break;
-                        case 20: {
+                        case 22: {
                           parsed.pathname = resolveDynamicRoute(
                             parsed.pathname,
                             pages
@@ -5990,14 +6032,14 @@
                             url = (0, _utils).formatWithValidation(parsed);
                           }
                         }
-                        case 21:
+                        case 23:
                           if (true) {
-                            _ctx.next = 23;
+                            _ctx.next = 25;
                             break;
                           }
                           return _ctx.abrupt("return");
-                        case 23:
-                          _ctx.next = 25;
+                        case 25:
+                          _ctx.next = 27;
                           return _this._preflightRequest({
                             as: addBasePath(asPath),
                             cache: true,
@@ -6005,7 +6047,7 @@
                             pathname: pathname,
                             query: query
                           });
-                        case 25:
+                        case 27:
                           effects = _ctx.sent;
                           if (effects.type === "rewrite") {
                             parsed.pathname = effects.resolvedHref;
@@ -6022,7 +6064,7 @@
                           _normalizeTrailingSlash).removePathTrailingSlash(
                             pathname
                           );
-                          _ctx.next = 30;
+                          _ctx.next = 32;
                           return Promise.all([
                             _this.pageLoader
                               ._isSsg(route)
@@ -6049,7 +6091,7 @@
                               options.priority ? "loadPage" : "prefetch"
                             ](route)
                           ]);
-                        case 30:
+                        case 32:
                         case "end":
                           return _ctx.stop();
                       }
Diff for index.html
@@ -19,7 +19,7 @@
       defer=""
     ></script>
     <script
-      src="/_next/static/chunks/main-4b217ddd923fa165.js"
+      src="/_next/static/chunks/main-98b55543970d6b99.js"
       defer=""
     ></script>
     <script
Diff for link.html
@@ -19,7 +19,7 @@
       defer=""
     ></script>
     <script
-      src="/_next/static/chunks/main-4b217ddd923fa165.js"
+      src="/_next/static/chunks/main-98b55543970d6b99.js"
       defer=""
     ></script>
     <script
Diff for withRouter.html
@@ -19,7 +19,7 @@
       defer=""
     ></script>
     <script
-      src="/_next/static/chunks/main-4b217ddd923fa165.js"
+      src="/_next/static/chunks/main-98b55543970d6b99.js"
       defer=""
     ></script>
     <script
Commit: 907b341

@kodiakhq kodiakhq bot merged commit 7b1bace into vercel:canary Feb 2, 2022
40 of 42 checks passed
@ijjk ijjk deleted the fix/external-beforefiles branch Feb 2, 2022
natew pushed a commit to natew/next.js that referenced this issue Feb 16, 2022
…el#33888)

This ensures we properly handle external `beforeFiles` rewrites client-side so that a different result doesn't occur client-side versus on a direct visit. 

## Bug

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

Fixes: vercel#32348
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 5, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants