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

Execute middleware on Next.js internal requests #37121

Merged
merged 9 commits into from
May 27, 2022

Conversation

javivelasco
Copy link
Member

Currently a Next.js middleware is executed for every requests but Next.js internal (_next/) requests. This means that when we have a middleware that rewrites from a SSR page /a to /b, middleware will run for the page request but not for the corresponding data request.

With this PR we are changing this behaviour so middleware will run for every request including internal data requests. Since it would hard for users to write code that applies certain behaviour to both pages and data requests we are also adding to the NextURL primitive awareness of such data requests so it will behave as:

const dataURL = new NextURL('http://localhost:3000/_next/data/development/a.json', config)
const url  = new NextURL('http://localhost:3000/a', config)

assert(url.pathname === dataUrl.pathname)
assert(dataUrl.buildId === 'development')

dataURL.pathname = '/b'
assert(String(dataURL) === 'http://localhost:3000/_next/data/development/b.json')

@ijjk
Copy link
Member

ijjk commented May 23, 2022

Stats from current PR

Default Build (Decrease detected ✓)
General Overall increase ⚠️
vercel/next.js canary javivelasco/next.js execute-middleware-on-nextjs-internal Change
buildDuration 16.2s 16s -211ms
buildDurationCached 6.5s 6.5s ⚠️ +43ms
nodeModulesSize 626 MB 626 MB ⚠️ +7.21 kB
Page Load Tests Overall decrease ⚠️
vercel/next.js canary javivelasco/next.js execute-middleware-on-nextjs-internal Change
/ failed reqs 0 0
/ total time (seconds) 3.741 3.791 ⚠️ +0.05
/ avg req/sec 668.35 659.4 ⚠️ -8.95
/error-in-render failed reqs 0 0
/error-in-render total time (seconds) 1.173 1.231 ⚠️ +0.06
/error-in-render avg req/sec 2131.01 2030.32 ⚠️ -100.69
Client Bundles (main, webpack) Overall decrease ✓
vercel/next.js canary javivelasco/next.js execute-middleware-on-nextjs-internal Change
925.HASH.js gzip 179 B 179 B
framework-HASH.js gzip 42 kB 42 kB
main-HASH.js gzip 29.2 kB 29.2 kB -33 B
webpack-HASH.js gzip 1.54 kB 1.54 kB
Overall change 73 kB 72.9 kB -33 B
Legacy Client Bundles (polyfills)
vercel/next.js canary javivelasco/next.js execute-middleware-on-nextjs-internal Change
polyfills-HASH.js gzip 31 kB 31 kB
Overall change 31 kB 31 kB
Client Pages
vercel/next.js canary javivelasco/next.js execute-middleware-on-nextjs-internal Change
_app-HASH.js gzip 1.36 kB 1.36 kB
_error-HASH.js gzip 193 B 193 B
amp-HASH.js gzip 308 B 308 B
css-HASH.js gzip 327 B 327 B
dynamic-HASH.js gzip 2.71 kB 2.71 kB
head-HASH.js gzip 359 B 359 B
hooks-HASH.js gzip 920 B 920 B
image-HASH.js gzip 5.74 kB 5.74 kB
index-HASH.js gzip 263 B 263 B
link-HASH.js gzip 2.65 kB 2.65 kB
routerDirect..HASH.js gzip 320 B 320 B
script-HASH.js gzip 391 B 391 B
withRouter-HASH.js gzip 318 B 318 B
85e02e95b279..7e3.css gzip 107 B 107 B
Overall change 16 kB 16 kB
Client Build Manifests
vercel/next.js canary javivelasco/next.js execute-middleware-on-nextjs-internal Change
_buildManifest.js gzip 459 B 459 B
Overall change 459 B 459 B
Rendered Page Sizes Overall increase ⚠️
vercel/next.js canary javivelasco/next.js execute-middleware-on-nextjs-internal Change
index.html gzip 532 B 533 B ⚠️ +1 B
link.html gzip 545 B 547 B ⚠️ +2 B
withRouter.html gzip 527 B 528 B ⚠️ +1 B
Overall change 1.6 kB 1.61 kB ⚠️ +4 B

Diffs

Diff for main-HASH.js
@@ -1567,18 +1567,17 @@
       /***/
     },
 
-    /***/ 2700: /***/ function(module, exports) {
+    /***/ 2700: /***/ function(module, exports, __webpack_require__) {
       "use strict";
 
       Object.defineProperty(exports, "__esModule", {
         value: true
       });
-      exports.removePathTrailingSlash = removePathTrailingSlash;
       exports.normalizePathTrailingSlash = void 0;
-      function removePathTrailingSlash(path) {
-        return path.endsWith("/") && path !== "/" ? path.slice(0, -1) : path;
-      }
-      var normalizePathTrailingSlash = false ? 0 : removePathTrailingSlash;
+      var _removeTrailingSlash = __webpack_require__(12);
+      var normalizePathTrailingSlash = false
+        ? 0
+        : _removeTrailingSlash.removeTrailingSlash;
       exports.normalizePathTrailingSlash = normalizePathTrailingSlash;
       if (
         (typeof exports.default === "function" ||
@@ -1627,7 +1626,7 @@
       );
       var _isDynamic = __webpack_require__(6238);
       var _parseRelativeUrl = __webpack_require__(2864);
-      var _normalizeTrailingSlash = __webpack_require__(2700);
+      var _removeTrailingSlash = __webpack_require__(12);
       var _routeLoader = __webpack_require__(2497);
       function _interopRequireDefault(obj) {
         return obj && obj.__esModule
@@ -1709,7 +1708,7 @@
                   return path + search + (search ? "&" : "?") + "__flight__=1";
                 }
                 var dataRoute = (0, _getAssetPathFromRoute).default(
-                  (0, _normalizeTrailingSlash).removePathTrailingSlash(
+                  (0, _removeTrailingSlash).removeTrailingSlash(
                     (0, _router).addLocale(path, locale)
                   ),
                   ".json"
@@ -4537,6 +4536,7 @@
       exports.createKey = createKey;
       exports["default"] = void 0;
       var _normalizeTrailingSlash = __webpack_require__(2700);
+      var _removeTrailingSlash = __webpack_require__(12);
       var _routeLoader = __webpack_require__(2497);
       var _script = __webpack_require__(3573);
       var _isError = _interopRequireWildcard(__webpack_require__(676));
@@ -4835,8 +4835,7 @@
         };
       }
       function resolveDynamicRoute(pathname, pages) {
-        var cleanPathname = (0,
-        _normalizeTrailingSlash).removePathTrailingSlash(
+        var cleanPathname = (0, _removeTrailingSlash).removeTrailingSlash(
           (0, _denormalizePagePath).denormalizePagePath(pathname)
         );
         if (cleanPathname === "/404" || cleanPathname === "/_error") {
@@ -4855,7 +4854,7 @@
             }
           });
         }
-        return (0, _normalizeTrailingSlash).removePathTrailingSlash(pathname);
+        return (0, _removeTrailingSlash).removeTrailingSlash(pathname);
       }
       var manualScrollRestoration =
         /* unused pure expression or super */ null && false && 0;
@@ -5027,9 +5026,7 @@
             );
           };
           // represents the current component key
-          var route = (0, _normalizeTrailingSlash).removePathTrailingSlash(
-            pathname1
-          );
+          var route = (0, _removeTrailingSlash).removeTrailingSlash(pathname1);
           // set up the component cache (by route keys)
           this.components = {};
           // We should not keep the cache, if there's an error
@@ -5448,8 +5445,7 @@
                             // point by either next/link or router.push/replace so strip the
                             // basePath from the pathname to match the pages dir 1-to-1
                             pathname = pathname
-                              ? (0,
-                                _normalizeTrailingSlash).removePathTrailingSlash(
+                              ? (0, _removeTrailingSlash).removeTrailingSlash(
                                   delBasePath(pathname)
                                 )
                               : pathname;
@@ -5544,7 +5540,7 @@
                                 (options._h !== 1 ||
                                   (0, _isDynamic).isDynamicRoute(
                                     (0,
-                                    _normalizeTrailingSlash).removePathTrailingSlash(
+                                    _removeTrailingSlash).removeTrailingSlash(
                                       pathname
                                     )
                                   ))
@@ -5629,9 +5625,7 @@
                             );
                           case 104:
                             route = (0,
-                            _normalizeTrailingSlash).removePathTrailingSlash(
-                              pathname
-                            );
+                            _removeTrailingSlash).removeTrailingSlash(pathname);
                             if (!(0, _isDynamic).isDynamicRoute(route)) {
                               _ctx.next = 120;
                               break;
@@ -6508,8 +6502,7 @@
                             resolvedAs = effects.asPath;
                             url = (0, _formatUrl).formatWithValidation(parsed);
                           }
-                          route = (0,
-                          _normalizeTrailingSlash).removePathTrailingSlash(
+                          route = (0, _removeTrailingSlash).removeTrailingSlash(
                             pathname
                           );
                           _ctx.next = 32;
@@ -6741,7 +6734,7 @@
                               ).pathname
                             );
                             fsPathname = (0,
-                            _normalizeTrailingSlash).removePathTrailingSlash(
+                            _removeTrailingSlash).removeTrailingSlash(
                               parsed.pathname
                             );
                             if (options.pages.includes(fsPathname)) {
@@ -6776,7 +6769,7 @@
                               break;
                             }
                             cleanRedirect = (0,
-                            _normalizeTrailingSlash).removePathTrailingSlash(
+                            _removeTrailingSlash).removeTrailingSlash(
                               (0, _normalizeLocalePath).normalizeLocalePath(
                                 hasBasePath(preflight.redirect)
                                   ? delBasePath(preflight.redirect)
@@ -7555,7 +7548,7 @@
           _catchAll = ref1.catchAll,
           catchAll = _catchAll === void 0 ? true : _catchAll;
         if (parameterizedRoute === "/") {
-          var catchAllRegex = catchAll ? "(?!_next).*" : "";
+          var catchAllRegex = catchAll ? ".*" : "";
           return {
             groups: {},
             re: new RegExp("^/".concat(catchAllRegex, "$"))
@@ -7575,7 +7568,7 @@
         var _catchAll = options.catchAll,
           catchAll = _catchAll === void 0 ? true : _catchAll;
         if (parameterizedRoute === "/") {
-          var catchAllRegex = catchAll ? "(?!_next).*" : "";
+          var catchAllRegex = catchAll ? ".*" : "";
           return {
             namedRegex: "^/".concat(catchAllRegex, "$")
           };
Diff for index.html
@@ -19,7 +19,7 @@
       defer=""
     ></script>
     <script
-      src="/_next/static/chunks/main-ba19ede6c55107dc.js"
+      src="/_next/static/chunks/main-001abe5a68919521.js"
       defer=""
     ></script>
     <script
Diff for link.html
@@ -19,7 +19,7 @@
       defer=""
     ></script>
     <script
-      src="/_next/static/chunks/main-ba19ede6c55107dc.js"
+      src="/_next/static/chunks/main-001abe5a68919521.js"
       defer=""
     ></script>
     <script
Diff for withRouter.html
@@ -19,7 +19,7 @@
       defer=""
     ></script>
     <script
-      src="/_next/static/chunks/main-ba19ede6c55107dc.js"
+      src="/_next/static/chunks/main-001abe5a68919521.js"
       defer=""
     ></script>
     <script

Default Build with SWC (Decrease detected ✓)
General Overall increase ⚠️
vercel/next.js canary javivelasco/next.js execute-middleware-on-nextjs-internal Change
buildDuration 18.4s 18.5s ⚠️ +183ms
buildDurationCached 6.5s 6.4s -51ms
nodeModulesSize 626 MB 626 MB ⚠️ +7.21 kB
Page Load Tests Overall decrease ⚠️
vercel/next.js canary javivelasco/next.js execute-middleware-on-nextjs-internal Change
/ failed reqs 0 0
/ total time (seconds) 3.721 3.762 ⚠️ +0.04
/ avg req/sec 671.92 664.52 ⚠️ -7.4
/error-in-render failed reqs 0 0
/error-in-render total time (seconds) 1.155 1.216 ⚠️ +0.06
/error-in-render avg req/sec 2164.39 2056.71 ⚠️ -107.68
Client Bundles (main, webpack) Overall decrease ✓
vercel/next.js canary javivelasco/next.js execute-middleware-on-nextjs-internal Change
925.HASH.js gzip 178 B 178 B
framework-HASH.js gzip 42.7 kB 42.7 kB
main-HASH.js gzip 29.7 kB 29.7 kB -24 B
webpack-HASH.js gzip 1.54 kB 1.54 kB
Overall change 74.1 kB 74 kB -24 B
Legacy Client Bundles (polyfills)
vercel/next.js canary javivelasco/next.js execute-middleware-on-nextjs-internal Change
polyfills-HASH.js gzip 31 kB 31 kB
Overall change 31 kB 31 kB
Client Pages
vercel/next.js canary javivelasco/next.js execute-middleware-on-nextjs-internal Change
_app-HASH.js gzip 1.35 kB 1.35 kB
_error-HASH.js gzip 179 B 179 B
amp-HASH.js gzip 311 B 311 B
css-HASH.js gzip 324 B 324 B
dynamic-HASH.js gzip 2.9 kB 2.9 kB
head-HASH.js gzip 357 B 357 B
hooks-HASH.js gzip 920 B 920 B
image-HASH.js gzip 5.84 kB 5.84 kB
index-HASH.js gzip 261 B 261 B
link-HASH.js gzip 2.78 kB 2.78 kB
routerDirect..HASH.js gzip 322 B 322 B
script-HASH.js gzip 392 B 392 B
withRouter-HASH.js gzip 317 B 317 B
85e02e95b279..7e3.css gzip 107 B 107 B
Overall change 16.4 kB 16.4 kB
Client Build Manifests
vercel/next.js canary javivelasco/next.js execute-middleware-on-nextjs-internal Change
_buildManifest.js gzip 458 B 458 B
Overall change 458 B 458 B
Rendered Page Sizes Overall decrease ✓
vercel/next.js canary javivelasco/next.js execute-middleware-on-nextjs-internal Change
index.html gzip 532 B 532 B
link.html gzip 546 B 545 B -1 B
withRouter.html gzip 529 B 527 B -2 B
Overall change 1.61 kB 1.6 kB -3 B

Diffs

Diff for main-HASH.js
@@ -1567,18 +1567,17 @@
       /***/
     },
 
-    /***/ 2700: /***/ function(module, exports) {
+    /***/ 2700: /***/ function(module, exports, __webpack_require__) {
       "use strict";
 
       Object.defineProperty(exports, "__esModule", {
         value: true
       });
-      exports.removePathTrailingSlash = removePathTrailingSlash;
       exports.normalizePathTrailingSlash = void 0;
-      function removePathTrailingSlash(path) {
-        return path.endsWith("/") && path !== "/" ? path.slice(0, -1) : path;
-      }
-      var normalizePathTrailingSlash = false ? 0 : removePathTrailingSlash;
+      var _removeTrailingSlash = __webpack_require__(12);
+      var normalizePathTrailingSlash = false
+        ? 0
+        : _removeTrailingSlash.removeTrailingSlash;
       exports.normalizePathTrailingSlash = normalizePathTrailingSlash;
       if (
         (typeof exports.default === "function" ||
@@ -1627,7 +1626,7 @@
       );
       var _isDynamic = __webpack_require__(6238);
       var _parseRelativeUrl = __webpack_require__(2864);
-      var _normalizeTrailingSlash = __webpack_require__(2700);
+      var _removeTrailingSlash = __webpack_require__(12);
       var _routeLoader = __webpack_require__(2497);
       function _interopRequireDefault(obj) {
         return obj && obj.__esModule
@@ -1709,7 +1708,7 @@
                   return path + search + (search ? "&" : "?") + "__flight__=1";
                 }
                 var dataRoute = (0, _getAssetPathFromRoute).default(
-                  (0, _normalizeTrailingSlash).removePathTrailingSlash(
+                  (0, _removeTrailingSlash).removeTrailingSlash(
                     (0, _router).addLocale(path, locale)
                   ),
                   ".json"
@@ -4537,6 +4536,7 @@
       exports.createKey = createKey;
       exports["default"] = void 0;
       var _normalizeTrailingSlash = __webpack_require__(2700);
+      var _removeTrailingSlash = __webpack_require__(12);
       var _routeLoader = __webpack_require__(2497);
       var _script = __webpack_require__(3573);
       var _isError = _interopRequireWildcard(__webpack_require__(676));
@@ -4835,8 +4835,7 @@
         };
       }
       function resolveDynamicRoute(pathname, pages) {
-        var cleanPathname = (0,
-        _normalizeTrailingSlash).removePathTrailingSlash(
+        var cleanPathname = (0, _removeTrailingSlash).removeTrailingSlash(
           (0, _denormalizePagePath).denormalizePagePath(pathname)
         );
         if (cleanPathname === "/404" || cleanPathname === "/_error") {
@@ -4855,7 +4854,7 @@
             }
           });
         }
-        return (0, _normalizeTrailingSlash).removePathTrailingSlash(pathname);
+        return (0, _removeTrailingSlash).removeTrailingSlash(pathname);
       }
       var manualScrollRestoration =
         /* unused pure expression or super */ null && false && 0;
@@ -5027,9 +5026,7 @@
             );
           };
           // represents the current component key
-          var route = (0, _normalizeTrailingSlash).removePathTrailingSlash(
-            pathname1
-          );
+          var route = (0, _removeTrailingSlash).removeTrailingSlash(pathname1);
           // set up the component cache (by route keys)
           this.components = {};
           // We should not keep the cache, if there's an error
@@ -5448,8 +5445,7 @@
                             // point by either next/link or router.push/replace so strip the
                             // basePath from the pathname to match the pages dir 1-to-1
                             pathname = pathname
-                              ? (0,
-                                _normalizeTrailingSlash).removePathTrailingSlash(
+                              ? (0, _removeTrailingSlash).removeTrailingSlash(
                                   delBasePath(pathname)
                                 )
                               : pathname;
@@ -5544,7 +5540,7 @@
                                 (options._h !== 1 ||
                                   (0, _isDynamic).isDynamicRoute(
                                     (0,
-                                    _normalizeTrailingSlash).removePathTrailingSlash(
+                                    _removeTrailingSlash).removeTrailingSlash(
                                       pathname
                                     )
                                   ))
@@ -5629,9 +5625,7 @@
                             );
                           case 104:
                             route = (0,
-                            _normalizeTrailingSlash).removePathTrailingSlash(
-                              pathname
-                            );
+                            _removeTrailingSlash).removeTrailingSlash(pathname);
                             if (!(0, _isDynamic).isDynamicRoute(route)) {
                               _ctx.next = 120;
                               break;
@@ -6508,8 +6502,7 @@
                             resolvedAs = effects.asPath;
                             url = (0, _formatUrl).formatWithValidation(parsed);
                           }
-                          route = (0,
-                          _normalizeTrailingSlash).removePathTrailingSlash(
+                          route = (0, _removeTrailingSlash).removeTrailingSlash(
                             pathname
                           );
                           _ctx.next = 32;
@@ -6741,7 +6734,7 @@
                               ).pathname
                             );
                             fsPathname = (0,
-                            _normalizeTrailingSlash).removePathTrailingSlash(
+                            _removeTrailingSlash).removeTrailingSlash(
                               parsed.pathname
                             );
                             if (options.pages.includes(fsPathname)) {
@@ -6776,7 +6769,7 @@
                               break;
                             }
                             cleanRedirect = (0,
-                            _normalizeTrailingSlash).removePathTrailingSlash(
+                            _removeTrailingSlash).removeTrailingSlash(
                               (0, _normalizeLocalePath).normalizeLocalePath(
                                 hasBasePath(preflight.redirect)
                                   ? delBasePath(preflight.redirect)
@@ -7555,7 +7548,7 @@
           _catchAll = ref1.catchAll,
           catchAll = _catchAll === void 0 ? true : _catchAll;
         if (parameterizedRoute === "/") {
-          var catchAllRegex = catchAll ? "(?!_next).*" : "";
+          var catchAllRegex = catchAll ? ".*" : "";
           return {
             groups: {},
             re: new RegExp("^/".concat(catchAllRegex, "$"))
@@ -7575,7 +7568,7 @@
         var _catchAll = options.catchAll,
           catchAll = _catchAll === void 0 ? true : _catchAll;
         if (parameterizedRoute === "/") {
-          var catchAllRegex = catchAll ? "(?!_next).*" : "";
+          var catchAllRegex = catchAll ? ".*" : "";
           return {
             namedRegex: "^/".concat(catchAllRegex, "$")
           };
Diff for index.html
@@ -19,7 +19,7 @@
       defer=""
     ></script>
     <script
-      src="/_next/static/chunks/main-ba19ede6c55107dc.js"
+      src="/_next/static/chunks/main-001abe5a68919521.js"
       defer=""
     ></script>
     <script
Diff for link.html
@@ -19,7 +19,7 @@
       defer=""
     ></script>
     <script
-      src="/_next/static/chunks/main-ba19ede6c55107dc.js"
+      src="/_next/static/chunks/main-001abe5a68919521.js"
       defer=""
     ></script>
     <script
Diff for withRouter.html
@@ -19,7 +19,7 @@
       defer=""
     ></script>
     <script
-      src="/_next/static/chunks/main-ba19ede6c55107dc.js"
+      src="/_next/static/chunks/main-001abe5a68919521.js"
       defer=""
     ></script>
     <script
Commit: 82fb7a3

@javivelasco javivelasco force-pushed the execute-middleware-on-nextjs-internal branch from 2460be7 to 118568b Compare May 24, 2022 10:35
@javivelasco javivelasco marked this pull request as ready for review May 24, 2022 10:35
@javivelasco javivelasco force-pushed the execute-middleware-on-nextjs-internal branch 3 times, most recently from f90c56b to 3c4f2b6 Compare May 24, 2022 18:28
@Schniz Schniz mentioned this pull request May 25, 2022
7 tasks
Copy link
Member

@timneutkens timneutkens left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good once the changes JJ suggested are applied

@javivelasco javivelasco force-pushed the execute-middleware-on-nextjs-internal branch from dfd7096 to bc3c418 Compare May 26, 2022 13:26
@ijjk

This comment was marked as outdated.

@javivelasco javivelasco force-pushed the execute-middleware-on-nextjs-internal branch 4 times, most recently from 6df843f to ac61a1b Compare May 26, 2022 18:45
@javivelasco javivelasco force-pushed the execute-middleware-on-nextjs-internal branch from ac61a1b to 53ed5dc Compare May 27, 2022 09:04
@javivelasco javivelasco force-pushed the execute-middleware-on-nextjs-internal branch 2 times, most recently from 2b53864 to 4970d15 Compare May 27, 2022 12:35
@javivelasco javivelasco force-pushed the execute-middleware-on-nextjs-internal branch from 752eff9 to bb75f48 Compare May 27, 2022 15:36
Copy link
Member

@ijjk ijjk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! 🙌

@ijjk ijjk merged commit 523704b into vercel:canary May 27, 2022
@javivelasco javivelasco deleted the execute-middleware-on-nextjs-internal branch May 30, 2022 08:52
kodiakhq bot pushed a commit that referenced this pull request Jun 3, 2022
This PR will allow Middleware to set its matcher through `export const config = { matching: ... }`

## Related

* This PR is rebased off #37121 

## 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 `yarn lint`
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 29, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants