Next.js: get pathname, query data inside routeChangeStart event

Created on 26 May 2019  路  3Comments  路  Source: vercel/next.js

Feature request

Is your feature request related to a problem? Please describe.

I want to get the query and pathname in routeChangeStart event
If I push, or Link with as path, I can only get as in url parameter of routeChangeStart.

Describe the solution you'd like

function(url: string) {}
becomes
function(as: string, {query, pathname, ...})

Describe alternatives you've considered

A clear and concise description of any alternative solutions or features you've considered.

Additional context

Actually, I want route cancellation like #2476 described. I can achieve it by throwing an error here to block the destination URL and display a custom modal to prompt user. If the user choose to continue, then navigate to the destination Url.

The problem is that I can't use Router.push(url) because the url is as url, not the exact pathname. The only user-unfriendly workaround is to force reload by window.location.href = url and it will be handled by the node.js express server.

P.S. I can't use confirm() to do this because we need a custom modal with 3 buttons on it.

Most helpful comment

I agree with this feature request. Three is 0.000000 value of providing action listeners with only one URL parameter. Do we need to implement URL parsers, get path names and so on?

This can only be used as a console logger as described at your example.

All 3 comments

We're not planning to increase the router size for this at this point.

I agree with this feature request. Three is 0.000000 value of providing action listeners with only one URL parameter. Do we need to implement URL parsers, get path names and so on?

This can only be used as a console logger as described at your example.

agree that at least the route information should be exposed in the router events. An URL string is too basic information to use, like for example advanced analytics, etc.
With i18n support in the routing coming soon, it will be even more important, to be able to make decisions based on the language.. would you need to regex the URL string?

Was this page helpful?
0 / 5 - 0 ratings