Replies: 9 comments 11 replies
-
We don't expose If |
Beta Was this translation helpful? Give feedback.
-
I haven't found a way to get the real pathname. If the url is "/en/something" then both |
Beta Was this translation helpful? Give feedback.
-
has anybody able to find solution to this problem ? |
Beta Was this translation helpful? Give feedback.
-
We have a similar problem. When using |
Beta Was this translation helpful? Give feedback.
-
Maybe i'm missing the point here, but this may work?
|
Beta Was this translation helpful? Give feedback.
-
For extra context here, another compelling use case of pathname is for logging. We have a wrapper around all of our getServersideProps which does some common logic including logging the route / some performance numbers. Currently we have to manually pass in a unique name for each route. Instead, we could have something like "/blogs/[blog_id]/" (we don't want "/blogs/a" and "/blogs/b" to be separated, as they are the same route with different params). This works great in our getInitialProps / clientside logging, but for some reason isn't exposed here |
Beta Was this translation helpful? Give feedback.
-
In my getServerSideProps I'm deciding whether a user have the right to access a page and having a reliable source of information without doing additional parsing would be great. Next.js has pathname on client side, why wouldn't it have pathname on server side? req.url in my case not even close resembles a pathname.
Right now I'm doing something like that:
Which is meh... |
Beta Was this translation helpful? Give feedback.
-
I wrote a small snippet with help of export const getServerSideProps: GetServerSideProps = async (ctx) => {
let resolvedUrl = ctx.resolvedUrl;
// if we have params in URL
if (ctx.params != null) {
// cut params values from resolvedUrl
resolvedUrl = Object.keys(ctx.params).reduce<string>((memo, param) => {
const value = ctx.params![param];
if (typeof value === 'string') {
const paramPositionInResolvedUrl = memo.indexOf(value);
return `${memo.substring(0, paramPositionInResolvedUrl)}[${param}]`;
}
return memo;
}, ctx.resolvedUrl);
}
const resourceUrl = new URL(`${process.env.WEBSITE_URL}${resolvedUrl}`);
console.log(resourceUrl.pathname)
return { props: {} };
} If I have a request like |
Beta Was this translation helpful? Give feedback.
-
I am using the approach suggested by @srigi , I just did a quick update to use Ex:
|
Beta Was this translation helpful? Give feedback.
-
Hello,
Our project has quite complex i18n requirements. In short, these are some of our requirements:
example.es
(locale = es),example.co.uk
(locale = en).This is now covered by Next.js 10 i18n routing.
pages/search.js
should be accessible onexample.es/buscar
(es) andexample.co.uk/search
(en).This is not directly covered by Next.js but it's possible using rewrites.
example.es/productos/product-1
andexample.co.uk/items/item-1
.<Link href=/search">
to produceexample.es/buscar
orexample.co.uk/search
according to the current locale.We've solved this by creating our own wrapper of the official component, which uses
useRouter()
hook.example.co.uk/buscar
andexample.es/search
should either not work (404) or redirect (301) to the proper domain + route combination.In order to achieve all of this we've created our own routing manifest, a json file that looks like this:
This file is read by next.config.js to produce the rewrites:
The routing manifest is also used by our
<Link>
component to build routes for the current locale.In order to achieve 5. (404 or 301 on invalid domain-route combinations), we're using
getServerSideProps
to try to validate the route. There we can access thecontext
object which containsparams
,req
,query
,locale
andresolvedUrl
among other things. However it does not containpathname
, which we would need in order to find the relevant route in our routing manifest.Interestingly, the context object for getInitialProps does contain
pathname
.Is there any reason why
getInitialProps
has access topathname
as part ofcontext
butgetServerSideProps
doesn't? Would you guys accept a PR addingpathname
togetServerSideProps
?Also any suggestion on how to cover the requirements exposed above with a different approach are more than welcome.
Beta Was this translation helpful? Give feedback.
All reactions