admin管理员组文章数量:1335356
I need to do a simple client side redirect to an external URL from a page on my Gatsby site hosted on Netlify. It works fine locally, but nothing seems to work when deployed. There also seems to be a bug with Gatsby's native createRedirect-functionality so that it doesn't allow redirecting to external URLs.
I've tried the three methods below in ponentDidMount
, in render
as well as in callbacks from a recurring (setInterval
) function call. I've also tried hijacking clicks on the links that leads to the redirect page and doing window.open(url)
, but nothing involving window
seems to be working when deployed. Any ideas?
window.location.replace(url)
window.location.href = url
window.location = url
I need to do a simple client side redirect to an external URL from a page on my Gatsby site hosted on Netlify. It works fine locally, but nothing seems to work when deployed. There also seems to be a bug with Gatsby's native createRedirect-functionality so that it doesn't allow redirecting to external URLs.
I've tried the three methods below in ponentDidMount
, in render
as well as in callbacks from a recurring (setInterval
) function call. I've also tried hijacking clicks on the links that leads to the redirect page and doing window.open(url)
, but nothing involving window
seems to be working when deployed. Any ideas?
window.location.replace(url)
window.location.href = url
window.location = url
Share
asked Aug 1, 2018 at 15:02
oskareoskare
1,07114 silver badges25 bronze badges
1 Answer
Reset to default 8You want to redirect from a url like /foo
on your site to some external URL like domain.tld/bar
, is that correct? If so, and you're using Netlify, then you probably want to use the Netlify _redirects
file.
However, if you really want to do this inside of Gatsby, you should be able to use the ponentDidMount()
hook to call window.location.replace()
. I've just tested on our v2 site and this works.
class About extends React.Component {
ponentDidMount() {
window.location.replace("https://www.gatsbycentral./");
}
There are some quirks during development, but this should work in production. I tested with a preview branch on Netlify and it worked for me.
If you have a specific error message you could post that as a new question.
本文标签: javascriptGatsby client side redirects to external URL not working on NetlifyStack Overflow
版权声明:本文标题:javascript - Gatsby client side redirects to external URL not working on Netlify - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742265598a2443330.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论