T
TanStack3mo ago
extended-salmon

How can i return a custom header from a loader?

In remix, we have data util that allows to return data with custom headers. Do we have the same thing in tanstack start?
7 Replies
conscious-sapphire
conscious-sapphire3mo ago
yes a route's head property is a function which receives loader data as a parameter so your loader returns data, and you can access it in head
extended-salmon
extended-salmonOP3mo ago
I mean the headers like set-cookie, max-age, etc.
stormy-gold
stormy-gold3mo ago
a loader runs on server initially and then on the client so no you cant set a header for a loader what do you want to do here?
extended-salmon
extended-salmonOP3mo ago
In the loader, i call an API and it returns data with the set-cookie header. I want to set that cookie in the user's browser. If the loaders run on the client, it should automatically set the cookie because it fetches on the client. But on the server, it doesnt work that way. In remix, they have the function "data" so they can return stuff like this in the loader
data(data,
headers: {
Cache-control: ....
}
)
data(data,
headers: {
Cache-control: ....
}
)
extended-salmon
extended-salmonOP3mo ago
Here is the reference
stormy-gold
stormy-gold3mo ago
put the logic into a server function and then call that server function from the loader. within the server function you can call setResponseHeader

Did you find this page helpful?