router.push with params
Answered
Scottish Deerhound posted this in #help-forum
Scottish DeerhoundOP
So im trying to make a search bar that redirects to ?q=(search value) and i have implemented it on the page itself, but whenever i use router.push it always goes to correct page initially as i see it in console, but then it goes back to /? with no params, i made it print the same string its redirecting to and pasting it manually and that works fine... Here is page code:
Here is the server output if i type something random:
export default async function Home({ searchParams }: Props) {
console.log(searchParams);
return (
<main><div>{searchParams.q}</div></main>
)
}(This is for testing) And here is the search code but i doubt its the issue (ill be adding more params) const handleSearch = (e: React.FormEvent<HTMLFormElement>) => {
const params = new URLSearchParams(queryParams);
params.set("q", searchQuery)
const route = "?" + params.toString();
console.log("Pushing to " + route);
router.push(route.toString());
};Here is console output, there is an error but i dont know what it means:Pushing to ?q=dsa
Failed to fetch RSC payload. Falling back to browser navigation. TypeError: Failed to fetch: [...]
Navigated to http://localhost:3000/?The navigated part is chrome feature, not my codeHere is the server output if i type something random:
✓ Ready in 4.1s
✓ Compiled /page in 2.1s (449 modules)
{}
✓ Compiled in 244ms (220 modules)
{ q: 'fsdgh' }Just a bit confused, it tried following tutorials with similar goals but they get the same result, so i kept the one i could understand the bestAnswered by Scottish Deerhound
idk how this makes sense but it was because of the handleSearch and the event, all i needed was
e.preventDefault(); and now it works (https://stackoverflow.com/questions/76989456/error-failed-to-fetch-rsc-payload-falling-back-to-browser-navigation)32 Replies
mmm… i recall this being an issue on root page for some reason
try prefixing your push with /
@DirtyCajunRice | AppDir mmm… i recall this being an issue on root page for some reason
Scottish DeerhoundOP
Like this?
"/?" + params.toString(); it has same result... also i tried that on a 404 page since the navbar search thingy is on every page, and it still has the same resultScottish DeerhoundOP
my bad i got rid of pathname while testing
it works yea
is there a workaround?
not familiar with react stuff but maybe a way to send all users of / to /page or something?
@DirtyCajunRice | AppDir it 100% will work
Scottish DeerhoundOP
ok its broken again im trying on another page, but its still just sending me to ? look;
Pushing to /auctions/?q=f
Navigated to http://localhost:3000/auctions?and if you do
/auctions?q=f, how does that go?@riský and if you do `/auctions?q=f`, how does that go?
Scottish DeerhoundOP
as its meant to, its back to just displaying the param values and it displays them correctly
i also have a thing to put it into the search box and its there
would this be relevant?
it only happens if it does router.push
@riský and if you do `/auctions?q=f`, how does that go?
Scottish DeerhoundOP
wait did you mean manually typeing it or in code
either, i just want to know if nextjs found the traiing slash pain
Scottish DeerhoundOP
doesnt work with either
i hardcoded the /auctions?q=f into router.push it still doesnt do it
Scottish DeerhoundOP
if it helps then if i go to any page (still hardcoded) it always leads to
(nonexistantpage)?@Scottish Deerhound would this be relevant?
Scottish DeerhoundOP
this error only happens when the page is in cache
i tested a bit more,
const route = pathName + "?" + params.toString(); only works when used on invalid pathsif the path is valid, even if the page does not require query params, it does not work
@riský either, i just want to know if nextjs found the traiing slash pain
Scottish DeerhoundOP
trailing slash always gets removed for some reason
wait ill try update to canary
Scottish DeerhoundOP
Whats RSC payload?
with canary it specifies it fails to fetch it for the site with the query params in use, then redirects to the site with no params
Also this:
Scottish DeerhoundOP
idk how this makes sense but it was because of the handleSearch and the event, all i needed was
e.preventDefault(); and now it works (https://stackoverflow.com/questions/76989456/error-failed-to-fetch-rsc-payload-falling-back-to-browser-navigation)Answer