question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Make Cookies available on SSR.

See original GitHub issue

Describe the problem

While coding on the server part of the project, you have access to an object of type Cookies in the RequestEvent. However, when coding on the SSR part, this object does not exist plus you can’t access the document object in the browser.

Describe the proposed solution

I believe while the request is being SSR, there should still be access to the request’s headers so, my proposal is to also allow the Cookies object to exist in the load functions.

Obviosuly this will break the load functions when they are CSR so the solution should be some sort of polyfill that transparently integrates with either the request’s headers or the document.cookie in the browser.

Alternatives considered

I’m afraid I can’t think of an alternative at this moment as in the SSR zone, there is no access to neither the Cookies object nor the document object.

Perhaps the only “solution” would be to not use cookies and return the data directly instead? But this would require the client code to manually send it back on every request.

Importance

would make my life easier

Additional Information

No response

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
carlosV2commented, Nov 15, 2022

Oh! That’s very interesting! I didn’t know I could force the run of the layouts by using url.pathname. From the documentation, I though that could only be accomplished with depends and invalidate.

I will revert from using a cookie and I’ll use a variable passed from the server.

Imho, I still think allowing for some degree of cookie access at SSR would be beneficial (it could just be allowing to access the request headers, for example) but, for my case, it is no longer required.

Thanks so much for your help @dummdidumm!

0reactions
dummdidummcommented, Nov 14, 2022

I’m not sure I follow - so your problem is not actually the cookie, but using the cookie to achieve something specific? Poking-in-the-dark-suggestions:

  • you can attach something to locals in hooks.server.ts which all +layout/page.server.ts files can access
  • you can add a dependency on url in your root +layout.server.ts so it runs on every page change: export function load ({ url }) { url.pathname; ... }
Read more comments on GitHub >

github_iconTop Results From Across the Web

Accessing cookies from React SSR - Stack Overflow
If it is on server-side, use the new prop (here: serverCookie) containing cookie otherwise use the cookie.get from client-side.
Read more >
How do you send Cookies, if you use server side rendering?
Cookie handling is not related to how you render views. In case of SSR, the browser will still send an HTTP request to...
Read more >
Access COOKIES in NextJS from server side. | Reactjs | SSR
Accessing cookies in NextJS from server side could be tricky for newbies. ... I can create full stack applications social media, ...
Read more >
Cookies vs Local Storage // for UX on Client and Server (CSR ...
Cookies vs Local Storage // for UX on Client and Server (CSR && SSR ).
Read more >
pass on cookies from server-side API calls on SSR response
In a web browser, any of many requests can set cookies for the browser. However, making requests on the server results in different...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found