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

Allow scroll prevention on hash change #31921

Merged
merged 4 commits into from Feb 7, 2022
Merged

Conversation

wouterraateland
Copy link
Contributor

@wouterraateland wouterraateland commented Nov 29, 2021

Currently, scrollToHash is performed on every hash change, even when this change is caused by <Link scroll={false} {...props} />.
This change prevents scrolling in this case and allows users to specify the desired scrolling behavior in the router's hashChangeComplete event.

Closes: #27195
x-ref: #13804

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

Currently, `scrollToHash` is performed on every hash change, even when this change is caused by `<Link scroll={false} {...props} />`.
This change prevents scrolling in this case and allows users to specify the desired scrolling behavior in the router's `hashChangeComplete` event.
@ijjk

This comment has been minimized.

@ijjk
Copy link
Member

ijjk commented Feb 7, 2022

Stats from current PR

Default Build (Increase detected ⚠️)
General Overall decrease ✓
vercel/next.js canary wouterraateland/next.js patch-1 Change
buildDuration 17.5s 18.4s ⚠️ +921ms
buildDurationCached 7s 6.7s -318ms
nodeModulesSize 359 MB 359 MB -34 B
Page Load Tests Overall increase ✓
vercel/next.js canary wouterraateland/next.js patch-1 Change
/ failed reqs 0 0
/ total time (seconds) 4.063 3.865 -0.2
/ avg req/sec 615.25 646.79 +31.54
/error-in-render failed reqs 0 0
/error-in-render total time (seconds) 1.906 1.845 -0.06
/error-in-render avg req/sec 1311.57 1354.96 +43.39
Client Bundles (main, webpack, commons) Overall increase ⚠️
vercel/next.js canary wouterraateland/next.js patch-1 Change
450.HASH.js gzip 179 B 179 B
framework-HASH.js gzip 42 kB 42 kB
main-HASH.js gzip 27.4 kB 27.4 kB ⚠️ +40 B
webpack-HASH.js gzip 1.44 kB 1.44 kB
Overall change 71 kB 71.1 kB ⚠️ +40 B
Legacy Client Bundles (polyfills)
vercel/next.js canary wouterraateland/next.js patch-1 Change
polyfills-HASH.js gzip 31 kB 31 kB
Overall change 31 kB 31 kB
Client Pages Overall decrease ✓
vercel/next.js canary wouterraateland/next.js patch-1 Change
_app-HASH.js gzip 1.36 kB 1.36 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.57 kB 2.57 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.2 kB 2.17 kB -23 B
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.6 kB 14.5 kB -23 B
Client Build Manifests Overall decrease ✓
vercel/next.js canary wouterraateland/next.js patch-1 Change
_buildManifest.js gzip 460 B 459 B -1 B
Overall change 460 B 459 B -1 B
Rendered Page Sizes Overall increase ⚠️
vercel/next.js canary wouterraateland/next.js patch-1 Change
index.html gzip 531 B 531 B
link.html gzip 545 B 546 B ⚠️ +1 B
withRouter.html gzip 526 B 526 B
Overall change 1.6 kB 1.6 kB ⚠️ +1 B

Diffs

Diff for _buildManifest.js
@@ -13,7 +13,7 @@ self.__BUILD_MANIFEST = {
   "/head": ["static\u002Fchunks\u002Fpages\u002Fhead-7100d3b2a548f0e4.js"],
   "/hooks": ["static\u002Fchunks\u002Fpages\u002Fhooks-538d621a0e670391.js"],
   "/image": ["static\u002Fchunks\u002Fpages\u002Fimage-f97943edf7ae3dd3.js"],
-  "/link": ["static\u002Fchunks\u002Fpages\u002Flink-27386a147a7100fa.js"],
+  "/link": ["static\u002Fchunks\u002Fpages\u002Flink-9b89bf672b4775b4.js"],
   "/routerDirect": [
     "static\u002Fchunks\u002Fpages\u002FrouterDirect-76232dd6bc335a24.js"
   ],
Diff for link-HASH.js
@@ -161,10 +161,6 @@
           return;
         }
         e.preventDefault();
-        //  avoid scroll for urls with anchor refs
-        if (scroll == null && as.indexOf("#") >= 0) {
-          scroll = false;
-        }
         // replace state instead of push if prop is present
         router[replace ? "replace" : "push"](href, as, {
           shallow: shallow,
Diff for main-HASH.js
@@ -4844,6 +4844,8 @@
                     asNoBasePath,
                     _shallow,
                     shallow,
+                    _scroll,
+                    scroll,
                     routeProps,
                     cleanedAs,
                     localeChange,
@@ -4879,7 +4881,7 @@
                     newAs,
                     notFoundRoute,
                     isValidShallowRoute,
-                    _scroll,
+                    _scroll1,
                     shouldScroll,
                     resetScroll;
                   return _runtimeJs.default.wrap(
@@ -4968,7 +4970,9 @@
                             }
                             (_shallow = options.shallow),
                               (shallow =
-                                _shallow === void 0 ? false : _shallow);
+                                _shallow === void 0 ? false : _shallow),
+                              (_scroll = options.scroll),
+                              (scroll = _scroll === void 0 ? true : _scroll);
                             routeProps = {
                               shallow: shallow
                             };
@@ -5008,8 +5012,17 @@
                               routeProps
                             );
                             // TODO: do we need the resolved href when only a hash change?
-                            _this.changeState(method, url, as, options);
-                            _this.scrollToHash(cleanedAs);
+                            _this.changeState(
+                              method,
+                              url,
+                              as,
+                              _objectSpread({}, options, {
+                                scroll: false
+                              })
+                            );
+                            if (scroll) {
+                              _this.scrollToHash(cleanedAs);
+                            }
                             _this.notify(_this.components[_this.route], null);
                             Router.events.emit(
                               "hashChangeComplete",
@@ -5435,9 +5448,9 @@
                             isValidShallowRoute =
                               options.shallow && _this.route === route;
                             shouldScroll =
-                              (_scroll = options.scroll) !== null &&
-                              _scroll !== void 0
-                                ? _scroll
+                              (_scroll1 = options.scroll) !== null &&
+                              _scroll1 !== void 0
+                                ? _scroll1
                                 : !isValidShallowRoute;
                             resetScroll = shouldScroll
                               ? {
Diff for index.html
@@ -19,7 +19,7 @@
       defer=""
     ></script>
     <script
-      src="/_next/static/chunks/main-148a2d7bd7ba1351.js"
+      src="/_next/static/chunks/main-33e91156e136ce2b.js"
       defer=""
     ></script>
     <script
Diff for link.html
@@ -19,7 +19,7 @@
       defer=""
     ></script>
     <script
-      src="/_next/static/chunks/main-148a2d7bd7ba1351.js"
+      src="/_next/static/chunks/main-33e91156e136ce2b.js"
       defer=""
     ></script>
     <script
@@ -27,7 +27,7 @@
       defer=""
     ></script>
     <script
-      src="/_next/static/chunks/pages/link-27386a147a7100fa.js"
+      src="/_next/static/chunks/pages/link-9b89bf672b4775b4.js"
       defer=""
     ></script>
     <script src="/_next/static/BUILD_ID/_buildManifest.js" defer=""></script>
Diff for withRouter.html
@@ -19,7 +19,7 @@
       defer=""
     ></script>
     <script
-      src="/_next/static/chunks/main-148a2d7bd7ba1351.js"
+      src="/_next/static/chunks/main-33e91156e136ce2b.js"
       defer=""
     ></script>
     <script

Default Build with SWC (Increase detected ⚠️)
General Overall decrease ✓
vercel/next.js canary wouterraateland/next.js patch-1 Change
buildDuration 21.4s 22s ⚠️ +628ms
buildDurationCached 7s 6.7s -340ms
nodeModulesSize 359 MB 359 MB -34 B
Page Load Tests Overall increase ✓
vercel/next.js canary wouterraateland/next.js patch-1 Change
/ failed reqs 0 0
/ total time (seconds) 3.97 4.231 ⚠️ +0.26
/ avg req/sec 629.77 590.83 ⚠️ -38.94
/error-in-render failed reqs 0 0
/error-in-render total time (seconds) 2.076 1.942 -0.13
/error-in-render avg req/sec 1204.19 1287.66 +83.47
Client Bundles (main, webpack, commons) Overall increase ⚠️
vercel/next.js canary wouterraateland/next.js patch-1 Change
450.HASH.js gzip 179 B 179 B
framework-HASH.js gzip 42.1 kB 42.1 kB
main-HASH.js gzip 27.4 kB 27.4 kB ⚠️ +34 B
webpack-HASH.js gzip 1.44 kB 1.44 kB
Overall change 71.2 kB 71.2 kB ⚠️ +34 B
Legacy Client Bundles (polyfills)
vercel/next.js canary wouterraateland/next.js patch-1 Change
polyfills-HASH.js gzip 31 kB 31 kB
Overall change 31 kB 31 kB
Client Pages Overall decrease ✓
vercel/next.js canary wouterraateland/next.js patch-1 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.56 kB 2.56 kB
head-HASH.js gzip 342 B 342 B
hooks-HASH.js gzip 911 B 911 B
image-HASH.js gzip 4.97 kB 4.97 kB
index-HASH.js gzip 256 B 256 B
link-HASH.js gzip 2.22 kB 2.2 kB -23 B
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.5 kB 14.5 kB -23 B
Client Build Manifests
vercel/next.js canary wouterraateland/next.js patch-1 Change
_buildManifest.js gzip 459 B 459 B
Overall change 459 B 459 B
Rendered Page Sizes Overall decrease ✓
vercel/next.js canary wouterraateland/next.js patch-1 Change
index.html gzip 534 B 533 B -1 B
link.html gzip 547 B 547 B
withRouter.html gzip 528 B 528 B
Overall change 1.61 kB 1.61 kB -1 B

Diffs

Diff for _buildManifest.js
@@ -13,7 +13,7 @@ self.__BUILD_MANIFEST = {
   "/head": ["static\u002Fchunks\u002Fpages\u002Fhead-7100d3b2a548f0e4.js"],
   "/hooks": ["static\u002Fchunks\u002Fpages\u002Fhooks-538d621a0e670391.js"],
   "/image": ["static\u002Fchunks\u002Fpages\u002Fimage-f97943edf7ae3dd3.js"],
-  "/link": ["static\u002Fchunks\u002Fpages\u002Flink-27386a147a7100fa.js"],
+  "/link": ["static\u002Fchunks\u002Fpages\u002Flink-9b89bf672b4775b4.js"],
   "/routerDirect": [
     "static\u002Fchunks\u002Fpages\u002FrouterDirect-76232dd6bc335a24.js"
   ],
Diff for link-HASH.js
@@ -161,10 +161,6 @@
           return;
         }
         e.preventDefault();
-        //  avoid scroll for urls with anchor refs
-        if (scroll == null && as.indexOf("#") >= 0) {
-          scroll = false;
-        }
         // replace state instead of push if prop is present
         router[replace ? "replace" : "push"](href, as, {
           shallow: shallow,
Diff for main-HASH.js
@@ -4844,6 +4844,8 @@
                     asNoBasePath,
                     _shallow,
                     shallow,
+                    _scroll,
+                    scroll,
                     routeProps,
                     cleanedAs,
                     localeChange,
@@ -4879,7 +4881,7 @@
                     newAs,
                     notFoundRoute,
                     isValidShallowRoute,
-                    _scroll,
+                    _scroll1,
                     shouldScroll,
                     resetScroll;
                   return _runtimeJs.default.wrap(
@@ -4968,7 +4970,9 @@
                             }
                             (_shallow = options.shallow),
                               (shallow =
-                                _shallow === void 0 ? false : _shallow);
+                                _shallow === void 0 ? false : _shallow),
+                              (_scroll = options.scroll),
+                              (scroll = _scroll === void 0 ? true : _scroll);
                             routeProps = {
                               shallow: shallow
                             };
@@ -5008,8 +5012,17 @@
                               routeProps
                             );
                             // TODO: do we need the resolved href when only a hash change?
-                            _this.changeState(method, url, as, options);
-                            _this.scrollToHash(cleanedAs);
+                            _this.changeState(
+                              method,
+                              url,
+                              as,
+                              _objectSpread({}, options, {
+                                scroll: false
+                              })
+                            );
+                            if (scroll) {
+                              _this.scrollToHash(cleanedAs);
+                            }
                             _this.notify(_this.components[_this.route], null);
                             Router.events.emit(
                               "hashChangeComplete",
@@ -5435,9 +5448,9 @@
                             isValidShallowRoute =
                               options.shallow && _this.route === route;
                             shouldScroll =
-                              (_scroll = options.scroll) !== null &&
-                              _scroll !== void 0
-                                ? _scroll
+                              (_scroll1 = options.scroll) !== null &&
+                              _scroll1 !== void 0
+                                ? _scroll1
                                 : !isValidShallowRoute;
                             resetScroll = shouldScroll
                               ? {
Diff for index.html
@@ -19,7 +19,7 @@
       defer=""
     ></script>
     <script
-      src="/_next/static/chunks/main-148a2d7bd7ba1351.js"
+      src="/_next/static/chunks/main-33e91156e136ce2b.js"
       defer=""
     ></script>
     <script
Diff for link.html
@@ -19,7 +19,7 @@
       defer=""
     ></script>
     <script
-      src="/_next/static/chunks/main-148a2d7bd7ba1351.js"
+      src="/_next/static/chunks/main-33e91156e136ce2b.js"
       defer=""
     ></script>
     <script
@@ -27,7 +27,7 @@
       defer=""
     ></script>
     <script
-      src="/_next/static/chunks/pages/link-27386a147a7100fa.js"
+      src="/_next/static/chunks/pages/link-9b89bf672b4775b4.js"
       defer=""
     ></script>
     <script src="/_next/static/BUILD_ID/_buildManifest.js" defer=""></script>
Diff for withRouter.html
@@ -19,7 +19,7 @@
       defer=""
     ></script>
     <script
-      src="/_next/static/chunks/main-148a2d7bd7ba1351.js"
+      src="/_next/static/chunks/main-33e91156e136ce2b.js"
       defer=""
     ></script>
     <script
Commit: 0d574a3

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 for the PR!

@ijjk ijjk merged commit b49717f into vercel:canary Feb 7, 2022
@wouterraateland wouterraateland deleted the patch-1 branch February 8, 2022 09:35
natew pushed a commit to natew/next.js that referenced this pull request Feb 16, 2022
* Allow scroll prevention on hash change

Currently, `scrollToHash` is performed on every hash change, even when this change is caused by `<Link scroll={false} {...props} />`.
This change prevents scrolling in this case and allows users to specify the desired scrolling behavior in the router's `hashChangeComplete` event.

* Add test case and apply fixes

Co-authored-by: JJ Kasper <jj@jjsweb.site>
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 10, 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.

None yet

2 participants