Hey folks!
I'm trying to get a feature working where a query param from the URL is passed down to a Link. I'm grabbing the location prop from my page and passing it down to the component correctly. (this all works fine) When I try and append that query param to the Link, it works fine in development mode, but when I run gatsby build and gatsby serve, the query param vanishes. Here's an example of what I'm doing:
const { myQueryParam } = parse((location || {}).search);
const params = stringify({ myQueryParam });
const q = params ? `?${params}` : "";
return (
<Link to={`https://www.google.com?${q}`}>My Link</Link>
);
If my current url is: https://localhost:8000?myQueryParam=abc1234, then I'm expecting the Link to navigate to https://www.google.com?myQueryParam=abc1234. Again, this works properly in gatsby develop, but not during the build. I (very na茂vely) suspect that somehow the Link is being compiled to a normal anchor tag at build time and since the query param is empty at that point, nothing happens.
I can temporarily work around this by making a window.location call in an onClick function on the link, but that seems super hacky and not something I want to introduce into the codebase long term.
Does anybody have recommendations on how I can make this work?
Gatsby version: 2.0.42 (latest)
I was unable to reproduce your issue.
But before that, one thing, you have a problem with your code namely in here:
const q = params ? `?${params}` : "";
This will append a second ? to your link url.
Now onto the issue at hand.
The actual code i used was this.
The page code:
import React from 'react'
import { Link} from 'gatsby'
import TestqueryParams from '../components/TestqueryParams';
import Layout from '../components/layout'
const SecondPage = ({location}) => (
<Layout>
<h1>Hi from the second page</h1>
<p>Welcome to page 2</p>
<TestqueryParams {...location}/>
<Link to="/">Go back to the homepage</Link>
</Layout>
)
export default SecondPage
The component with the code provided and a query parameter like so myQueryParam=abc1234
import React from 'react';
import { Link } from 'gatsby'
import { parse,stringify } from 'query-string';
const TestqueryParams=location=>{
const { myQueryParam } = parse((location || {}).search);
const params = stringify({ myQueryParam });
const q = params ? `${params}` : "";
return (
<div>
<h4>Hello from TestqueryParams</h4>
<Link to={`https://www.google.com?${q}`}>My Link</Link>
</div>
)
}
export default TestqueryParams
tested with gatsby dev and it was getting the query param passed like you said.
With gatsby build and gatsby serve i got the same exact output. I got the parameter added to the link.
Deleted the .cache folder and public re issued build and serve, exact same output.
Also don't forget to adjust your code because the Link url will be localhost:9000/https://www.google.com?myQueryParam=whatever is recieved here
Thanks so much for taking the time to respond in such detail! This is so weird.
I ran through your example with the following setup (to try and make a proof of concept):
gatsby new hello-world https://github.com/gatsbyjs/gatsby-starter-hello-world)TestqueryParams component to /src/pages/index.js.Here are my results:
gatsby developgatsby build && gatsby serve:<Link to={`https://www.google.com?${q}`}>My Link</Link><a href={`https://www.google.com?${q}`}>My Link</a>This would also explain my original issue, which I probably should have mentioned above. My Link is the same abstraction from the docs that switches out an anchor for the Link when it isn't prefixed by /.
So I guess in conclusion, I'm still encountering the issue, but only for anchor tags. Again, thanks for the super detailed write-up.
No need to thank...Here to help out others as much as they have helped me. I'm leaving the issue open and see if anyone more knowledgeable can provide you with a solution to the issue
Old issues will be closed after 30 days of inactivity. This issue has been quiet for 20 days and is being marked as stale. Reply here or add the label "not stale" to keep this issue open!
Hey again!
It鈥檚 been 30 days since anything happened on this issue, so our friendly neighborhood robot (that鈥檚 me!) is going to close it.
Please keep in mind that I鈥檓 only a robot, so if I鈥檝e closed this issue in error, I鈥檓 HUMAN_EMOTION_SORRY. Please feel free to reopen this issue or create a new one if you need anything else.
Thanks again for being part of the Gatsby community!
I have faced the same issue today :(
Can this issue be re-opened? Running into the same problem described by @Vpr99,
"My Link is the same abstraction from the docs that switches out an anchor for the Link when it isn't prefixed by /."
Most helpful comment
I have faced the same issue today :(