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

Fix next/link can't jump to non-latin anchors #36430

Merged
merged 6 commits into from
May 22, 2022
Merged

Conversation

hulufei
Copy link
Contributor

@hulufei hulufei commented Apr 25, 2022

fixes #11109

Bug

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

Feature

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

Documentation / Examples

  • Make sure the linting passes by running yarn lint

@hulufei
Copy link
Contributor Author

hulufei commented Apr 25, 2022

I have made it works for anchors in the same page, but got stuck with links from another page. Could anyone help me to work it out? Thanks.

@hulufei hulufei changed the title Fix next/link can't jump to non-latin anchors [Help needed] Fix next/link can't jump to non-latin anchors Apr 25, 2022
@hulufei hulufei changed the title [Help needed] Fix next/link can't jump to non-latin anchors [Need help] Fix next/link can't jump to non-latin anchors Apr 25, 2022
@hulufei
Copy link
Contributor Author

hulufei commented Apr 25, 2022

I think I figured it out.

@hulufei hulufei changed the title [Need help] Fix next/link can't jump to non-latin anchors Fix next/link can't jump to non-latin anchors Apr 25, 2022
@hulufei hulufei marked this pull request as ready for review April 25, 2022 09:57
@ijjk
Copy link
Member

ijjk commented Apr 25, 2022

Failing test suites

Commit: 2900d7e

yarn testheadless test/integration/middleware/core/test/index.test.js

  • Middleware base tests > production mode > should override with rewrite externally correctly
  • Middleware base tests > production mode > should rewrite to Vercel
Expand output

● Middleware base tests › production mode › should override with rewrite externally correctly

elementHandle.click: Timeout 30000ms exceeded.
=========================== logs ===========================
attempting click action
  waiting for element to be visible, enabled and stable
  element is visible, enabled and stable
  scrolling into view if needed
  done scrolling
  checking that element receives pointer events at (94.19,148.47)
  element does receive pointer events
  performing click action
  click action done
  waiting for scheduled navigations to finish
============================================================

  281 |   click() {
  282 |     return this.chain((el) => {
> 283 |       return el.click().then(() => el)
      |                 ^
  284 |     })
  285 |   }
  286 |

  at lib/browsers/playwright.ts:283:17
  at Object.<anonymous> (integration/middleware/core/test/index.test.js:266:5)

● Middleware base tests › production mode › should rewrite to Vercel

expect(received).toBe(expected) // Object.is equality

Expected: "Develop. Preview. Ship. For the best frontend teams – Vercel"
Received: ""

  478 |     // const browser = await webdriver(context.appPort, '/rewrite-me-to-vercel')
  479 |     // TODO: running this to chech the window.location.pathname hangs for some reason;
> 480 |     expect($('head > title').text()).toBe(
      |                                      ^
  481 |       'Develop. Preview. Ship. For the best frontend teams – Vercel'
  482 |     )
  483 |   })

  at Object.<anonymous> (integration/middleware/core/test/index.test.js:480:38)

Read more about building and testing Next.js in contributing.md.

@ijjk
Copy link
Member

ijjk commented Apr 25, 2022

Stats from current PR

Default Build (Increase detected ⚠️)
General Overall increase ⚠️
vercel/next.js canary hulufei/next.js fix-11109 Change
buildDuration 19.3s 19.4s ⚠️ +148ms
buildDurationCached 7.6s 7.6s ⚠️ +15ms
nodeModulesSize 479 MB 479 MB ⚠️ +593 B
Page Load Tests Overall increase ✓
vercel/next.js canary hulufei/next.js fix-11109 Change
/ failed reqs 0 0
/ total time (seconds) 5.172 5.107 -0.06
/ avg req/sec 483.34 489.48 +6.14
/error-in-render failed reqs 0 0
/error-in-render total time (seconds) 2 2.01 ⚠️ +0.01
/error-in-render avg req/sec 1249.76 1243.78 ⚠️ -5.98
Client Bundles (main, webpack) Overall increase ⚠️
vercel/next.js canary hulufei/next.js fix-11109 Change
925.HASH.js gzip 179 B 179 B
framework-HASH.js gzip 42 kB 42 kB
main-HASH.js gzip 29 kB 29 kB ⚠️ +32 B
webpack-HASH.js gzip 1.54 kB 1.54 kB
Overall change 72.7 kB 72.8 kB ⚠️ +32 B
Legacy Client Bundles (polyfills)
vercel/next.js canary hulufei/next.js fix-11109 Change
polyfills-HASH.js gzip 31 kB 31 kB
Overall change 31 kB 31 kB
Client Pages
vercel/next.js canary hulufei/next.js fix-11109 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.73 kB 5.73 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 hulufei/next.js fix-11109 Change
_buildManifest.js gzip 458 B 458 B
Overall change 458 B 458 B
Rendered Page Sizes Overall decrease ✓
vercel/next.js canary hulufei/next.js fix-11109 Change
index.html gzip 533 B 532 B -1 B
link.html gzip 547 B 546 B -1 B
withRouter.html gzip 528 B 527 B -1 B
Overall change 1.61 kB 1.6 kB -3 B

Diffs

Diff for main-HASH.js
@@ -5220,7 +5220,8 @@
                     isValidShallowRoute,
                     _scroll1,
                     shouldScroll,
-                    resetScroll;
+                    resetScroll,
+                    hashRegex;
                   return _runtimeJs.default.wrap(
                     function _callee$(_ctx) {
                       while (1)
@@ -5871,9 +5872,13 @@
                               as,
                               routeProps
                             );
+                            hashRegex = /#.+$/;
+                            if (shouldScroll && hashRegex.test(as)) {
+                              _this.scrollToHash(as);
+                            }
                             return _ctx.abrupt("return", true);
-                          case 164:
-                            _ctx.prev = 164;
+                          case 166:
+                            _ctx.prev = 166;
                             _ctx.t4 = _ctx["catch"](113);
                             if (
                               !(
@@ -5881,13 +5886,13 @@
                                 _ctx.t4.cancelled
                               )
                             ) {
-                              _ctx.next = 168;
+                              _ctx.next = 170;
                               break;
                             }
                             return _ctx.abrupt("return", false);
-                          case 168:
+                          case 170:
                             throw _ctx.t4;
-                          case 169:
+                          case 171:
                           case "end":
                             return _ctx.stop();
                         }
@@ -5896,7 +5901,7 @@
                     null,
                     [
                       [39, 51],
-                      [113, 164],
+                      [113, 166],
                       [135, 141]
                     ]
                   );
@@ -6326,15 +6331,17 @@
                 window.scrollTo(0, 0);
                 return;
               }
+              // Decode hash to make non-latin anchor works.
+              var rawHash = decodeURIComponent(hash);
               // First we check if the element by id is found
-              var idEl = document.getElementById(hash);
+              var idEl = document.getElementById(rawHash);
               if (idEl) {
                 idEl.scrollIntoView();
                 return;
               }
               // If there's no element with the id, we check the `name` property
               // To mirror browsers
-              var nameEl = document.getElementsByName(hash)[0];
+              var nameEl = document.getElementsByName(rawHash)[0];
               if (nameEl) {
                 nameEl.scrollIntoView();
               }
Diff for index.html
@@ -19,7 +19,7 @@
       defer=""
     ></script>
     <script
-      src="/_next/static/chunks/main-d39b128be27dbb18.js"
+      src="/_next/static/chunks/main-7c48d7922fb4ac24.js"
       defer=""
     ></script>
     <script
Diff for link.html
@@ -19,7 +19,7 @@
       defer=""
     ></script>
     <script
-      src="/_next/static/chunks/main-d39b128be27dbb18.js"
+      src="/_next/static/chunks/main-7c48d7922fb4ac24.js"
       defer=""
     ></script>
     <script
Diff for withRouter.html
@@ -19,7 +19,7 @@
       defer=""
     ></script>
     <script
-      src="/_next/static/chunks/main-d39b128be27dbb18.js"
+      src="/_next/static/chunks/main-7c48d7922fb4ac24.js"
       defer=""
     ></script>
     <script

Default Build with SWC (Increase detected ⚠️)
General Overall increase ⚠️
vercel/next.js canary hulufei/next.js fix-11109 Change
buildDuration 23.3s 23.1s -205ms
buildDurationCached 7.9s 8s ⚠️ +119ms
nodeModulesSize 479 MB 479 MB ⚠️ +593 B
Page Load Tests Overall increase ✓
vercel/next.js canary hulufei/next.js fix-11109 Change
/ failed reqs 0 0
/ total time (seconds) 5.418 5.399 -0.02
/ avg req/sec 461.39 463.04 +1.65
/error-in-render failed reqs 0 0
/error-in-render total time (seconds) 2.225 2.137 -0.09
/error-in-render avg req/sec 1123.69 1169.92 +46.23
Client Bundles (main, webpack) Overall increase ⚠️
vercel/next.js canary hulufei/next.js fix-11109 Change
925.HASH.js gzip 178 B 178 B
framework-HASH.js gzip 42.7 kB 42.7 kB
main-HASH.js gzip 29.5 kB 29.5 kB ⚠️ +34 B
webpack-HASH.js gzip 1.54 kB 1.54 kB
Overall change 73.9 kB 73.9 kB ⚠️ +34 B
Legacy Client Bundles (polyfills)
vercel/next.js canary hulufei/next.js fix-11109 Change
polyfills-HASH.js gzip 31 kB 31 kB
Overall change 31 kB 31 kB
Client Pages
vercel/next.js canary hulufei/next.js fix-11109 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.89 kB 2.89 kB
head-HASH.js gzip 357 B 357 B
hooks-HASH.js gzip 920 B 920 B
image-HASH.js gzip 5.82 kB 5.82 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.3 kB 16.3 kB
Client Build Manifests
vercel/next.js canary hulufei/next.js fix-11109 Change
_buildManifest.js gzip 457 B 457 B
Overall change 457 B 457 B
Rendered Page Sizes Overall increase ⚠️
vercel/next.js canary hulufei/next.js fix-11109 Change
index.html gzip 532 B 533 B ⚠️ +1 B
link.html gzip 546 B 547 B ⚠️ +1 B
withRouter.html gzip 528 B 529 B ⚠️ +1 B
Overall change 1.61 kB 1.61 kB ⚠️ +3 B

Diffs

Diff for main-HASH.js
@@ -5220,7 +5220,8 @@
                     isValidShallowRoute,
                     _scroll1,
                     shouldScroll,
-                    resetScroll;
+                    resetScroll,
+                    hashRegex;
                   return _runtimeJs.default.wrap(
                     function _callee$(_ctx) {
                       while (1)
@@ -5871,9 +5872,13 @@
                               as,
                               routeProps
                             );
+                            hashRegex = /#.+$/;
+                            if (shouldScroll && hashRegex.test(as)) {
+                              _this.scrollToHash(as);
+                            }
                             return _ctx.abrupt("return", true);
-                          case 164:
-                            _ctx.prev = 164;
+                          case 166:
+                            _ctx.prev = 166;
                             _ctx.t4 = _ctx["catch"](113);
                             if (
                               !(
@@ -5881,13 +5886,13 @@
                                 _ctx.t4.cancelled
                               )
                             ) {
-                              _ctx.next = 168;
+                              _ctx.next = 170;
                               break;
                             }
                             return _ctx.abrupt("return", false);
-                          case 168:
+                          case 170:
                             throw _ctx.t4;
-                          case 169:
+                          case 171:
                           case "end":
                             return _ctx.stop();
                         }
@@ -5896,7 +5901,7 @@
                     null,
                     [
                       [39, 51],
-                      [113, 164],
+                      [113, 166],
                       [135, 141]
                     ]
                   );
@@ -6326,15 +6331,17 @@
                 window.scrollTo(0, 0);
                 return;
               }
+              // Decode hash to make non-latin anchor works.
+              var rawHash = decodeURIComponent(hash);
               // First we check if the element by id is found
-              var idEl = document.getElementById(hash);
+              var idEl = document.getElementById(rawHash);
               if (idEl) {
                 idEl.scrollIntoView();
                 return;
               }
               // If there's no element with the id, we check the `name` property
               // To mirror browsers
-              var nameEl = document.getElementsByName(hash)[0];
+              var nameEl = document.getElementsByName(rawHash)[0];
               if (nameEl) {
                 nameEl.scrollIntoView();
               }
Diff for index.html
@@ -19,7 +19,7 @@
       defer=""
     ></script>
     <script
-      src="/_next/static/chunks/main-d39b128be27dbb18.js"
+      src="/_next/static/chunks/main-7c48d7922fb4ac24.js"
       defer=""
     ></script>
     <script
Diff for link.html
@@ -19,7 +19,7 @@
       defer=""
     ></script>
     <script
-      src="/_next/static/chunks/main-d39b128be27dbb18.js"
+      src="/_next/static/chunks/main-7c48d7922fb4ac24.js"
       defer=""
     ></script>
     <script
Diff for withRouter.html
@@ -19,7 +19,7 @@
       defer=""
     ></script>
     <script
-      src="/_next/static/chunks/main-d39b128be27dbb18.js"
+      src="/_next/static/chunks/main-7c48d7922fb4ac24.js"
       defer=""
     ></script>
     <script
Commit: 8e0787c

@hulufei
Copy link
Contributor Author

hulufei commented Apr 27, 2022

@ijjk Could you check it again? Thanks.

@hulufei
Copy link
Contributor Author

hulufei commented Apr 28, 2022

It's weird, I checked the failing log, and didn't find any assert failures. I had run yarn testheadless --testPathPattern "client-navigation" -t 'Client Navigation' on local with 2900d7e, all tests passed.

Anything I can do to continue?

@hulufei hulufei requested a review from ijjk May 2, 2022 04:38
@ijjk
Copy link
Member

ijjk commented May 22, 2022

Thanks for the PR!

@kodiakhq kodiakhq bot merged commit 11cb49b into vercel:canary May 22, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 21, 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.

Jump link anchors dont work
3 participants