admin管理员组文章数量:1323715
I have a dynamic route:
test/[id].js
When user clicks on a link pointing to /test/1
Next.js ends up rendering the proper page as intended.
The funny business starts when I want to have /test/1
url masked with anything else.
<Link href="/test/1" as="/any/thing/here">
As far as I understand the above code should:
- move user to test/1 and render test/[id].js,
- hide the domain/test/1 url behind domain/any/thing/here.
What happens is it does not point to test/[id].js at all, just returns a 404.
Sandbox link with broken code:
=/pages/index.js
What am I doing wrong here? I have a really long and plex urls with lots of slashes I have to use with Next.js dynamic routing, and I don't see any other solution than using "as"... I'm pretty much sure I used to use it a few years back and it just worked! Seems like it worked for this guy as well: Linking to dynamic routes in Next.js
If Next.js changed something then how I recreate this functionality easily?
I have a dynamic route:
test/[id].js
When user clicks on a link pointing to /test/1
Next.js ends up rendering the proper page as intended.
The funny business starts when I want to have /test/1
url masked with anything else.
<Link href="/test/1" as="/any/thing/here">
As far as I understand the above code should:
- move user to test/1 and render test/[id].js,
- hide the domain./test/1 url behind domain./any/thing/here.
What happens is it does not point to test/[id].js at all, just returns a 404.
Sandbox link with broken code:
https://codesandbox.io/s/nervous-silence-z62s1?file=/pages/index.js
What am I doing wrong here? I have a really long and plex urls with lots of slashes I have to use with Next.js dynamic routing, and I don't see any other solution than using "as"... I'm pretty much sure I used to use it a few years back and it just worked! Seems like it worked for this guy as well: Linking to dynamic routes in Next.js
If Next.js changed something then how I recreate this functionality easily?
Share Improve this question asked Jul 1, 2021 at 8:55 WordpressorWordpressor 7,55326 gold badges74 silver badges115 bronze badges1 Answer
Reset to default 6I think you need to write href
like that:
<Link
href="/test/[id]?id=1"
// Or like that
href={{
pathname: '/test/[id]',
query: { id: '1' }
}}
as="/any/thing/here"
>
<a>Link to test/1 - 'as' decorator is broken?</a>
</Link>
I am not sure why it works like that, but I saw it somewhere some time ago and I use it like that since then. I think there is no information about it in the docs.
And be sure that /any/thing/here
page actually exists too, because otherwise if user refresh browser after client side navigation there will be 404 anyway.
本文标签: javascriptNextjs Link quotasquot decorator stopped working for dynamic routesStack Overflow
版权声明:本文标题:javascript - Next.js Link "as" decorator stopped working for dynamic routes? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742125046a2421899.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论