Reading url from headers gives me previous url
Answered
Waterman posted this in #help-forum
WatermanOP
So in this code:
I am trying to get the current header, and I am, but its basically storing the provious url and therefore I am fetching the wrong data, code:
I don't really know the issue my self so therefore I might have asked the questions a bit weird but if somebody knows a solution I would appreciate it
for example how does this image make sense?
const headersList = headers();
const fullUrl = headersList.get("referer") || "";
const parts = fullUrl.split("/");
const id = parts[parts.length - 1];I am trying to get the current header, and I am, but its basically storing the provious url and therefore I am fetching the wrong data, code:
const res = await fetch(`${process.env.HOST}/api/basproduct?id=` + id);I don't really know the issue my self so therefore I might have asked the questions a bit weird but if somebody knows a solution I would appreciate it
for example how does this image make sense?
Answered by DirtyCajunRice | AppDir
right. just print out all the keys and values of h.forEach to check
26 Replies
WatermanOP
I just need the path and the query
but I wanna keep it as server component so I don't wanna use usePathname
or the only requirment I have is that I am able to fetch data live from a database and I guess I can't really do that with client components
WatermanOP
oh ok haha
import 'server-only';
import { headers } from "next/headers";
export const getFullPath = () => {
const h = headers();
const path = h.get("next-url") || h.get("x-invoke-path") || "/";
console.warn("Using path of:", path);
const queryString = decodeURIComponent(h.get("x-invoke-query") || "%7B%7D");
let searchParamsJson = "{}";
try {
searchParamsJson = JSON.parse(queryString);
} catch (e) {
console.error("Error parsing search params:", queryString);
}
const searchParams = new URLSearchParams(searchParamsJson);
return {
path,
searchParams,
}
}WatermanOP
do I need all of that code to get the url?
and before you ask, yes you need to check both of those headers for path because the header sent is different in dev than in prod 😂
WatermanOP
or just a part of it?
oh ok haha, but with this code it will work in both prod and dev?
you may not care about the search params now… but you will in the future so just leave it in there
WatermanOP
oh ok, I have to go now but I will try that later, thank you so much, I have been struggling with it for so long
np 🙂
@DirtyCajunRice | AppDir np 🙂
WatermanOP
Hey I just tried it out and it is actually working really well, but I have another problem but I don't think it is related to your code,
but the problem I am experiencing is that I get
but the problem I am experiencing is that I get
Using path of: /butik when I click on one of the products, therfore I have to do a page refresh in order to get the correct url: Using path of: /kollektioner/artistic-rose do you have any idea how I can fix this?@Waterman Hey I just tried it out and it is actually working really well, but I have another problem but I don't think it is related to your code,
but the problem I am experiencing is that I get Using path of: /butik when I click on one of the products, therfore I have to do a page refresh in order to get the correct url:
Using path of: /kollektioner/artistic-rose do you have any idea how I can fix this?
there are other headers you may prefer to use
console.warn log out all the headers in prod and see which you want
it may be a totally different header for your use case
WatermanOP
oh ok, if I understand you right, I can use different properties of variable
So I can modify this line of code?
h?So I can modify this line of code?
const path = h.get("next-url") || h.get("x-invoke-path") || "/";because I think the actual problem comes because when I hover over the link of the product, it starts loading and then using the current url that is "/butik"
@Waterman oh ok, if I understand you right, I can use different properties of variable h?
So I can modify this line of code?
javascript
const path = h.get("next-url") || h.get("x-invoke-path") || "/";
right. just print out all the keys and values of h.forEach to check
Answer
WatermanOP
oh thanks I used the x-invoke-path and it seems to be working fully now, thank you so much for making me understand the code better too.