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/link router 24075 take asPath instead of pathName in router #24199

Merged
merged 9 commits into from
Apr 30, 2021

Conversation

ishank-s
Copy link
Contributor

@ishank-s ishank-s commented Apr 18, 2021

Fixes: #24075

Bug

  • Related issues linked using fixes #number
  • Integration tests added

Documentation / Examples

  • Make sure the linting passes

@ishank-s ishank-s changed the title Fix/link router 24075 Fix/link router 24075 take asPath instead of pathName in router Apr 18, 2021
@ijjk

This comment has been minimized.

@ijjk

This comment has been minimized.

@ishank-s
Copy link
Contributor Author

hi @timneutkens , requesting review, the issue breaks next/links in dynamic pages

@ijjk

This comment has been minimized.

ijjk
ijjk previously approved these changes Apr 30, 2021
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
Copy link
Member

ijjk commented Apr 30, 2021

Stats from current PR

Default Server Mode (Increase detected ⚠️)
General Overall increase ⚠️
vercel/next.js canary prophet1996/next.js fix/link-router-24075 Change
buildDuration 14.5s 14.5s -68ms
buildDurationCached 4.8s 4.5s -268ms
nodeModulesSize 46.6 MB 46.6 MB ⚠️ +350 B
Page Load Tests Overall increase ✓
vercel/next.js canary prophet1996/next.js fix/link-router-24075 Change
/ failed reqs 0 0
/ total time (seconds) 2.44 2.436 0
/ avg req/sec 1024.44 1026.34 +1.9
/error-in-render failed reqs 0 0
/error-in-render total time (seconds) 1.323 1.294 -0.03
/error-in-render avg req/sec 1889.58 1932.62 +43.04
Client Bundles (main, webpack, commons) Overall increase ⚠️
vercel/next.js canary prophet1996/next.js fix/link-router-24075 Change
597-HASH.js gzip 13.2 kB 13.2 kB ⚠️ +9 B
778-HASH.js gzip 7.12 kB 7.12 kB
framework-HASH.js gzip 39.3 kB 39.3 kB
main-HASH.js gzip 151 B 151 B
webpack-HASH.js gzip 993 B 993 B
Overall change 60.7 kB 60.8 kB ⚠️ +9 B
Legacy Client Bundles (polyfills)
vercel/next.js canary prophet1996/next.js fix/link-router-24075 Change
polyfills-HASH.js gzip 31.1 kB 31.1 kB
Overall change 31.1 kB 31.1 kB
Client Pages Overall decrease ✓
vercel/next.js canary prophet1996/next.js fix/link-router-24075 Change
_app-HASH.js gzip 1.3 kB 1.3 kB
_error-HASH.js gzip 3.68 kB 3.68 kB
amp-HASH.js gzip 558 B 558 B
css-HASH.js gzip 363 B 363 B
hooks-HASH.js gzip 924 B 924 B
index-HASH.js gzip 243 B 243 B
link-HASH.js gzip 1.66 kB 1.66 kB -1 B
routerDirect..HASH.js gzip 336 B 336 B
withRouter-HASH.js gzip 334 B 334 B
99a142a5cfae..804.css gzip 125 B 125 B
Overall change 9.52 kB 9.52 kB -1 B
Client Build Manifests Overall increase ⚠️
vercel/next.js canary prophet1996/next.js fix/link-router-24075 Change
_buildManifest.js gzip 390 B 395 B ⚠️ +5 B
Overall change 390 B 395 B ⚠️ +5 B
Rendered Page Sizes Overall decrease ✓
vercel/next.js canary prophet1996/next.js fix/link-router-24075 Change
index.html gzip 611 B 609 B -2 B
link.html gzip 616 B 614 B -2 B
withRouter.html gzip 605 B 605 B
Overall change 1.83 kB 1.83 kB -4 B

Diffs

Diff for _buildManifest.js
@@ -12,7 +12,7 @@ self.__BUILD_MANIFEST = {
   "/hooks": [
     "static\u002Fchunks\u002Fpages\u002Fhooks-8c2e74bb2b6bffaebe37.js"
   ],
-  "/link": ["static\u002Fchunks\u002Fpages\u002Flink-dd34d9b14dfb3c040ade.js"],
+  "/link": ["static\u002Fchunks\u002Fpages\u002Flink-e010d772d10ae2f9a50c.js"],
   "/routerDirect": [
     "static\u002Fchunks\u002Fpages\u002FrouterDirect-86b413d0659e50665759.js"
   ],
Diff for link-HASH.js
@@ -102,7 +102,7 @@
 
         var p = props.prefetch !== false;
         var router = (0, _router2.useRouter)();
-        var pathname = (router && router.pathname) || "/";
+        var pathname = (router && router.asPath) || "/";
 
         var _react$default$useMem = _react["default"].useMemo(
             function() {
Diff for 597-HASH.js
@@ -1529,7 +1529,15 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
 
       function resolveHref(currentPath, href, resolveAs) {
         // we use a dummy base url for relative urls
-        var base = new URL(currentPath, "http://n");
+        var base;
+
+        try {
+          base = new URL(currentPath, "http://n");
+        } catch (_) {
+          // fallback to / for invalid asPath values e.g. //
+          base = new URL("/", "http://n");
+        }
+
         var urlAsString =
           typeof href === "string"
             ? href
@@ -1593,7 +1601,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
       function prepareUrlAs(router, url, as) {
         // If url and as provided as an object representation,
         // we'll format them into the string version here.
-        var _resolveHref = resolveHref(router.pathname, url, true),
+        var _resolveHref = resolveHref(router.asPath, url, true),
           _resolveHref2 = _slicedToArray(_resolveHref, 2),
           resolvedHref = _resolveHref2[0],
           resolvedAs = _resolveHref2[1];
@@ -1607,7 +1615,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
           ? resolvedHref
           : addBasePath(resolvedHref);
         var preparedAs = as
-          ? stripOrigin(resolveHref(router.pathname, as))
+          ? stripOrigin(resolveHref(router.asPath, as))
           : resolvedAs || resolvedHref;
         return {
           url: preparedUrl,
Diff for index.html
@@ -7,7 +7,7 @@
     <noscript data-n-css=""></noscript>
     <link
       rel="preload"
-      href="/_next/static/chunks/webpack-6a4dc1e801bd0b7ccbdd.js"
+      href="/_next/static/chunks/webpack-ef955a69f11ea96b4a60.js"
       as="script"
     />
     <link
@@ -17,7 +17,7 @@
     />
     <link
       rel="preload"
-      href="/_next/static/chunks/597-ae440a7286b9272ad989.js"
+      href="/_next/static/chunks/597-1b9c2a3fa8cca5953f4e.js"
       as="script"
     />
     <link
@@ -58,7 +58,7 @@
       src="/_next/static/chunks/polyfills-8683bd742a84c1edd48c.js"
     ></script>
     <script
-      src="/_next/static/chunks/webpack-6a4dc1e801bd0b7ccbdd.js"
+      src="/_next/static/chunks/webpack-ef955a69f11ea96b4a60.js"
       async=""
     ></script>
     <script
@@ -66,7 +66,7 @@
       async=""
     ></script>
     <script
-      src="/_next/static/chunks/597-ae440a7286b9272ad989.js"
+      src="/_next/static/chunks/597-1b9c2a3fa8cca5953f4e.js"
       async=""
     ></script>
     <script
Diff for link.html
@@ -7,7 +7,7 @@
     <noscript data-n-css=""></noscript>
     <link
       rel="preload"
-      href="/_next/static/chunks/webpack-6a4dc1e801bd0b7ccbdd.js"
+      href="/_next/static/chunks/webpack-ef955a69f11ea96b4a60.js"
       as="script"
     />
     <link
@@ -17,7 +17,7 @@
     />
     <link
       rel="preload"
-      href="/_next/static/chunks/597-ae440a7286b9272ad989.js"
+      href="/_next/static/chunks/597-1b9c2a3fa8cca5953f4e.js"
       as="script"
     />
     <link
@@ -37,7 +37,7 @@
     />
     <link
       rel="preload"
-      href="/_next/static/chunks/pages/link-dd34d9b14dfb3c040ade.js"
+      href="/_next/static/chunks/pages/link-e010d772d10ae2f9a50c.js"
       as="script"
     />
   </head>
@@ -63,7 +63,7 @@
       src="/_next/static/chunks/polyfills-8683bd742a84c1edd48c.js"
     ></script>
     <script
-      src="/_next/static/chunks/webpack-6a4dc1e801bd0b7ccbdd.js"
+      src="/_next/static/chunks/webpack-ef955a69f11ea96b4a60.js"
       async=""
     ></script>
     <script
@@ -71,7 +71,7 @@
       async=""
     ></script>
     <script
-      src="/_next/static/chunks/597-ae440a7286b9272ad989.js"
+      src="/_next/static/chunks/597-1b9c2a3fa8cca5953f4e.js"
       async=""
     ></script>
     <script
@@ -87,7 +87,7 @@
       async=""
     ></script>
     <script
-      src="/_next/static/chunks/pages/link-dd34d9b14dfb3c040ade.js"
+      src="/_next/static/chunks/pages/link-e010d772d10ae2f9a50c.js"
       async=""
     ></script>
     <script src="/_next/static/BUILD_ID/_buildManifest.js" async=""></script>
Diff for withRouter.html
@@ -7,7 +7,7 @@
     <noscript data-n-css=""></noscript>
     <link
       rel="preload"
-      href="/_next/static/chunks/webpack-6a4dc1e801bd0b7ccbdd.js"
+      href="/_next/static/chunks/webpack-ef955a69f11ea96b4a60.js"
       as="script"
     />
     <link
@@ -17,7 +17,7 @@
     />
     <link
       rel="preload"
-      href="/_next/static/chunks/597-ae440a7286b9272ad989.js"
+      href="/_next/static/chunks/597-1b9c2a3fa8cca5953f4e.js"
       as="script"
     />
     <link
@@ -58,7 +58,7 @@
       src="/_next/static/chunks/polyfills-8683bd742a84c1edd48c.js"
     ></script>
     <script
-      src="/_next/static/chunks/webpack-6a4dc1e801bd0b7ccbdd.js"
+      src="/_next/static/chunks/webpack-ef955a69f11ea96b4a60.js"
       async=""
     ></script>
     <script
@@ -66,7 +66,7 @@
       async=""
     ></script>
     <script
-      src="/_next/static/chunks/597-ae440a7286b9272ad989.js"
+      src="/_next/static/chunks/597-1b9c2a3fa8cca5953f4e.js"
       async=""
     ></script>
     <script

Serverless Mode (Increase detected ⚠️)
General Overall increase ⚠️
vercel/next.js canary prophet1996/next.js fix/link-router-24075 Change
buildDuration 17.8s 17.8s -30ms
buildDurationCached 6.7s 6.7s -15ms
nodeModulesSize 46.6 MB 46.6 MB ⚠️ +350 B
Client Bundles (main, webpack, commons) Overall increase ⚠️
vercel/next.js canary prophet1996/next.js fix/link-router-24075 Change
597-HASH.js gzip 13.2 kB 13.2 kB ⚠️ +9 B
778-HASH.js gzip 7.12 kB 7.12 kB
framework-HASH.js gzip 39.3 kB 39.3 kB
main-HASH.js gzip 151 B 151 B
webpack-HASH.js gzip 993 B 993 B
Overall change 60.7 kB 60.8 kB ⚠️ +9 B
Legacy Client Bundles (polyfills)
vercel/next.js canary prophet1996/next.js fix/link-router-24075 Change
polyfills-HASH.js gzip 31.1 kB 31.1 kB
Overall change 31.1 kB 31.1 kB
Client Pages Overall decrease ✓
vercel/next.js canary prophet1996/next.js fix/link-router-24075 Change
_app-HASH.js gzip 1.3 kB 1.3 kB
_error-HASH.js gzip 3.68 kB 3.68 kB
amp-HASH.js gzip 558 B 558 B
css-HASH.js gzip 363 B 363 B
hooks-HASH.js gzip 924 B 924 B
index-HASH.js gzip 243 B 243 B
link-HASH.js gzip 1.66 kB 1.66 kB -1 B
routerDirect..HASH.js gzip 336 B 336 B
withRouter-HASH.js gzip 334 B 334 B
99a142a5cfae..804.css gzip 125 B 125 B
Overall change 9.52 kB 9.52 kB -1 B
Client Build Manifests Overall increase ⚠️
vercel/next.js canary prophet1996/next.js fix/link-router-24075 Change
_buildManifest.js gzip 390 B 395 B ⚠️ +5 B
Overall change 390 B 395 B ⚠️ +5 B
Serverless bundles Overall increase ⚠️
vercel/next.js canary prophet1996/next.js fix/link-router-24075 Change
_error.js 1.35 MB 1.35 MB
404.html 2.76 kB 2.76 kB
500.html 2.75 kB 2.75 kB
amp.amp.html 10.8 kB 10.8 kB
amp.html 1.96 kB 1.96 kB
css.html 2.13 kB 2.13 kB
hooks.html 2.01 kB 2.01 kB
index.js 1.35 MB 1.35 MB
link.js 1.4 MB 1.4 MB ⚠️ +124 B
routerDirect.js 1.4 MB 1.4 MB ⚠️ +128 B
withRouter.js 1.4 MB 1.4 MB ⚠️ +126 B
Overall change 6.91 MB 6.91 MB ⚠️ +378 B

Webpack 4 Mode (Increase detected ⚠️)
General Overall increase ⚠️
vercel/next.js canary prophet1996/next.js fix/link-router-24075 Change
buildDuration 12.4s 12.2s -192ms
buildDurationCached 5.2s 5s -189ms
nodeModulesSize 46.6 MB 46.6 MB ⚠️ +350 B
Page Load Tests Overall increase ✓
vercel/next.js canary prophet1996/next.js fix/link-router-24075 Change
/ failed reqs 0 0
/ total time (seconds) 2.432 2.442 ⚠️ +0.01
/ avg req/sec 1027.78 1023.68 ⚠️ -4.1
/error-in-render failed reqs 0 0
/error-in-render total time (seconds) 1.304 1.274 -0.03
/error-in-render avg req/sec 1917.67 1962.38 +44.71
Client Bundles (main, webpack, commons) Overall increase ⚠️
vercel/next.js canary prophet1996/next.js fix/link-router-24075 Change
677f882d2ed8..HASH.js gzip 13.3 kB 13.3 kB ⚠️ +11 B
framework.HASH.js gzip 39 kB 39 kB
main-HASH.js gzip 7.19 kB 7.19 kB
webpack-HASH.js gzip 751 B 751 B
Overall change 60.2 kB 60.2 kB ⚠️ +11 B
Legacy Client Bundles (polyfills)
vercel/next.js canary prophet1996/next.js fix/link-router-24075 Change
polyfills-HASH.js gzip 31.3 kB 31.3 kB
Overall change 31.3 kB 31.3 kB
Client Pages Overall decrease ✓
vercel/next.js canary prophet1996/next.js fix/link-router-24075 Change
_app-HASH.js gzip 1.28 kB 1.28 kB
_error-HASH.js gzip 3.71 kB 3.71 kB
amp-HASH.js gzip 536 B 536 B
css-HASH.js gzip 339 B 339 B
hooks-HASH.js gzip 887 B 887 B
index-HASH.js gzip 227 B 227 B
link-HASH.js gzip 1.65 kB 1.64 kB -3 B
routerDirect..HASH.js gzip 303 B 303 B
withRouter-HASH.js gzip 302 B 302 B
21c68fa65a48..217.css gzip 125 B 125 B
Overall change 9.36 kB 9.36 kB -3 B
Client Build Manifests Overall increase ⚠️
vercel/next.js canary prophet1996/next.js fix/link-router-24075 Change
_buildManifest.js gzip 419 B 420 B ⚠️ +1 B
Overall change 419 B 420 B ⚠️ +1 B
Rendered Page Sizes
vercel/next.js canary prophet1996/next.js fix/link-router-24075 Change
index.html gzip 612 B 613 B ⚠️ +1 B
link.html gzip 620 B 620 B
withRouter.html gzip 607 B 606 B -1 B
Overall change 1.84 kB 1.84 kB

Diffs

Diff for _buildManifest.js
@@ -12,7 +12,7 @@ self.__BUILD_MANIFEST = {
   "/hooks": [
     "static\u002Fchunks\u002Fpages\u002Fhooks-f1cc32851f5e866a7e78.js"
   ],
-  "/link": ["static\u002Fchunks\u002Fpages\u002Flink-a9bdb47b832b5a01f298.js"],
+  "/link": ["static\u002Fchunks\u002Fpages\u002Flink-121d9306ed12436945d4.js"],
   "/routerDirect": [
     "static\u002Fchunks\u002Fpages\u002FrouterDirect-e9bdf4dc22ca5dd5d885.js"
   ],
Diff for link-HASH.js
@@ -160,7 +160,7 @@ _N_E = (window["webpackJsonp_N_E"] = window["webpackJsonp_N_E"] || []).push([
 
         var p = props.prefetch !== false;
         var router = (0, _router2.useRouter)();
-        var pathname = (router && router.pathname) || "/";
+        var pathname = (router && router.asPath) || "/";
 
         var _react$default$useMem = _react["default"].useMemo(
             function() {
Diff for 677f882d2ed8..c4df.HASH.js
@@ -1366,7 +1366,15 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
 
       function resolveHref(currentPath, href, resolveAs) {
         // we use a dummy base url for relative urls
-        var base = new URL(currentPath, "http://n");
+        var base;
+
+        try {
+          base = new URL(currentPath, "http://n");
+        } catch (_) {
+          // fallback to / for invalid asPath values e.g. //
+          base = new URL("/", "http://n");
+        }
+
         var urlAsString =
           typeof href === "string"
             ? href
@@ -1430,7 +1438,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
       function prepareUrlAs(router, url, as) {
         // If url and as provided as an object representation,
         // we'll format them into the string version here.
-        var _resolveHref = resolveHref(router.pathname, url, true),
+        var _resolveHref = resolveHref(router.asPath, url, true),
           _resolveHref2 = _slicedToArray(_resolveHref, 2),
           resolvedHref = _resolveHref2[0],
           resolvedAs = _resolveHref2[1];
@@ -1444,7 +1452,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
           ? resolvedHref
           : addBasePath(resolvedHref);
         var preparedAs = as
-          ? stripOrigin(resolveHref(router.pathname, as))
+          ? stripOrigin(resolveHref(router.asPath, as))
           : resolvedAs || resolvedHref;
         return {
           url: preparedUrl,
Diff for index.html
@@ -17,7 +17,7 @@
     />
     <link
       rel="preload"
-      href="/_next/static/chunks/677f882d2ed86fa3467b8979053c1a4c3f8bc4df.93bc6152317c318ec255.js"
+      href="/_next/static/chunks/677f882d2ed86fa3467b8979053c1a4c3f8bc4df.034e4b1503019c911a82.js"
       as="script"
     />
     <link
@@ -61,7 +61,7 @@
       async=""
     ></script>
     <script
-      src="/_next/static/chunks/677f882d2ed86fa3467b8979053c1a4c3f8bc4df.93bc6152317c318ec255.js"
+      src="/_next/static/chunks/677f882d2ed86fa3467b8979053c1a4c3f8bc4df.034e4b1503019c911a82.js"
       async=""
     ></script>
     <script
Diff for link.html
@@ -17,7 +17,7 @@
     />
     <link
       rel="preload"
-      href="/_next/static/chunks/677f882d2ed86fa3467b8979053c1a4c3f8bc4df.93bc6152317c318ec255.js"
+      href="/_next/static/chunks/677f882d2ed86fa3467b8979053c1a4c3f8bc4df.034e4b1503019c911a82.js"
       as="script"
     />
     <link
@@ -32,7 +32,7 @@
     />
     <link
       rel="preload"
-      href="/_next/static/chunks/pages/link-a9bdb47b832b5a01f298.js"
+      href="/_next/static/chunks/pages/link-121d9306ed12436945d4.js"
       as="script"
     />
   </head>
@@ -66,7 +66,7 @@
       async=""
     ></script>
     <script
-      src="/_next/static/chunks/677f882d2ed86fa3467b8979053c1a4c3f8bc4df.93bc6152317c318ec255.js"
+      src="/_next/static/chunks/677f882d2ed86fa3467b8979053c1a4c3f8bc4df.034e4b1503019c911a82.js"
       async=""
     ></script>
     <script
@@ -78,7 +78,7 @@
       async=""
     ></script>
     <script
-      src="/_next/static/chunks/pages/link-a9bdb47b832b5a01f298.js"
+      src="/_next/static/chunks/pages/link-121d9306ed12436945d4.js"
       async=""
     ></script>
     <script src="/_next/static/BUILD_ID/_buildManifest.js" async=""></script>
Diff for withRouter.html
@@ -17,7 +17,7 @@
     />
     <link
       rel="preload"
-      href="/_next/static/chunks/677f882d2ed86fa3467b8979053c1a4c3f8bc4df.93bc6152317c318ec255.js"
+      href="/_next/static/chunks/677f882d2ed86fa3467b8979053c1a4c3f8bc4df.034e4b1503019c911a82.js"
       as="script"
     />
     <link
@@ -61,7 +61,7 @@
       async=""
     ></script>
     <script
-      src="/_next/static/chunks/677f882d2ed86fa3467b8979053c1a4c3f8bc4df.93bc6152317c318ec255.js"
+      src="/_next/static/chunks/677f882d2ed86fa3467b8979053c1a4c3f8bc4df.034e4b1503019c911a82.js"
       async=""
     ></script>
     <script
Commit: 4801507

@kodiakhq kodiakhq bot merged commit 9bbb968 into vercel:canary Apr 30, 2021
flybayer pushed a commit to blitz-js/next.js that referenced this pull request Jun 1, 2021
…el#24199)

Fixes: vercel#24075

## Bug
 
- [X] Related issues linked using `fixes #number`
- [X] Integration tests added


## Documentation / Examples

- [x] Make sure the linting passes
@vercel vercel locked as resolved and limited conversation to collaborators Jan 28, 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.

next/link constructs incorrect hash links and throws on dynamic pages
2 participants