Skip to content

Commit 99f2933

Browse files
committed
Disable Next.js scroll restoration
1 parent ee96afd commit 99f2933

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed

beta/next.config.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ const nextConfig = {
1313
pageExtensions: ['jsx', 'js', 'ts', 'tsx', 'mdx', 'md'],
1414
experimental: {
1515
plugins: true,
16-
scrollRestoration: true,
1716
legacyBrowsers: false,
1817
browsersListForSwc: true,
1918
},
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
diff --git a/node_modules/next/dist/client/index.js b/node_modules/next/dist/client/index.js
2+
index 8541301..e020783 100644
3+
--- a/node_modules/next/dist/client/index.js
4+
+++ b/node_modules/next/dist/client/index.js
5+
@@ -471,7 +471,11 @@ function renderError(renderErrorProps) {
6+
let reactRoot = null;
7+
// On initial render a hydrate should always happen
8+
let shouldHydrate = true;
9+
-function renderReactElement(domEl, fn) {
10+
+function renderReactElement(domEl, fn
11+
+// ~~~~~~~~~ MANUAL EDIT FOR REACT DOCS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
12+
+, REACT_DOCS_IS_POPSTATE // Added this argument
13+
+// ~~~~~~~~~ /MANUAL EDIT FOR REACT DOCS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
14+
+) {
15+
// mark start of hydrate/render
16+
if (_utils.ST) {
17+
performance.mark('beforeRender');
18+
@@ -484,10 +488,16 @@ function renderReactElement(domEl, fn) {
19+
// TODO: Remove shouldHydrate variable when React 18 is stable as it can depend on `reactRoot` existing
20+
shouldHydrate = false;
21+
} else {
22+
- const startTransition = _react.default.startTransition;
23+
- startTransition(()=>{
24+
+ // ~~~~~~~~~ MANUAL EDIT FOR REACT DOCS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25+
+ if (REACT_DOCS_IS_POPSTATE) { // Added this branch
26+
reactRoot.render(reactEl);
27+
- });
28+
+ } else { // The else block contents is the original code
29+
+ const startTransition = _react.default.startTransition;
30+
+ startTransition(()=>{
31+
+ reactRoot.render(reactEl);
32+
+ });
33+
+ }
34+
+ // ~~~~~~~~~ /MANUAL EDIT FOR REACT DOCS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
35+
}
36+
} else {
37+
// The check for `.hydrate` is there to support React alternatives like preact
38+
@@ -796,7 +806,11 @@ function doRender(input) {
39+
});
40+
}
41+
if (input.scroll) {
42+
- window.scrollTo(input.scroll.x, input.scroll.y);
43+
+ // ~~~~~~~~~ MANUAL EDIT FOR REACT DOCS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
44+
+ if (!input.scroll.REACT_DOCS_IS_POPSTATE) { // Added this condition
45+
+ window.scrollTo(input.scroll.x, input.scroll.y);
46+
+ }
47+
+ // ~~~~~~~~~ /MANUAL EDIT FOR REACT DOCS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
48+
}
49+
}
50+
function onRootCommit() {
51+
@@ -815,6 +829,9 @@ function doRender(input) {
52+
onRootCommit
53+
]
54+
}, process.env.__NEXT_STRICT_MODE ? /*#__PURE__*/ _react.default.createElement(_react.default.StrictMode, null, elem) : elem)
55+
+ // ~~~~~~~~~ MANUAL EDIT FOR REACT DOCS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
56+
+ , input.scroll && input.scroll.REACT_DOCS_IS_POPSTATE // Added this argument
57+
+ // ~~~~~~~~~ /MANUAL EDIT FOR REACT DOCS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
58+
);
59+
return renderPromise;
60+
}
61+
diff --git a/node_modules/next/dist/shared/lib/router/router.js b/node_modules/next/dist/shared/lib/router/router.js
62+
index 572e21b..538091f 100644
63+
--- a/node_modules/next/dist/shared/lib/router/router.js
64+
+++ b/node_modules/next/dist/shared/lib/router/router.js
65+
@@ -416,6 +416,15 @@ class Router {
66+
}
67+
}
68+
}
69+
+ // ~~~~~~~~~ MANUAL EDIT FOR REACT DOCS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
70+
+ // I don't want Next.js to do anythnig on scroll for Back/Forward buttons.
71+
+ // Instead I want to let the browser take care of it with auto scroll restoration.
72+
+ // Unfortunately, Next.js still tries to scroll to top on Back, so let's not do that.
73+
+ forcedScroll = {
74+
+ REACT_DOCS_IS_POPSTATE: true
75+
+ };
76+
+ // ~~~~~~~~~ /MANUAL EDIT FOR REACT DOCS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
77+
+
78+
this._key = key;
79+
const { pathname } = (0, _parseRelativeUrl).parseRelativeUrl(url);
80+
// Make sure we don't re-render on initial load,

0 commit comments

Comments
 (0)