Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Cookies are missing in request for loaders and actions in CSR #10819

Closed
rmedaer opened this issue Aug 27, 2023 · 1 comment
Closed

[Bug]: Cookies are missing in request for loaders and actions in CSR #10819

rmedaer opened this issue Aug 27, 2023 · 1 comment
Labels

Comments

@rmedaer
Copy link

rmedaer commented Aug 27, 2023

What version of React Router are you using?

6.15.0

Steps to Reproduce

Create a Route with a loader (in CSR) and get Cookie header from the request.

function loader({ request }) {
  const cookie = request.headers.get('cookie');
  console.log(cookie);

  // (...) business logic using cookies
}

const router = createBrowserRouter({
    path: '/',
    element: <HelloWorld/>,
    loader: loader,
});

// (...)

Expected Behavior

I expect cookies to be available through the Cookie header (as they are might be in SSR) so I can use the same piece of code for both CSR and SSR.

Actual Behavior

The cookies are not available in the Cookie header.

Analyzing the createClientSideRequest function in react-router/packages/router/router.ts, it looks like the cookies (from document.cookie) are not included in the Request created:

function createClientSideRequest(
  history: History,
  location: string | Location,
  signal: AbortSignal,
  submission?: Submission
): Request {
  let url = history.createURL(stripHashFromPath(location)).toString();
  let init: RequestInit = { signal };

  // (...) 

  return new Request(url, init);
}

Notes

It might be a feature request but since it's available in SSR, I expect the same behavior in CSR.

@rmedaer rmedaer added the bug label Aug 27, 2023
@brophdawg11
Copy link
Contributor

I'm going to move this to a feature request discussion.

IMO, Cookies are inherently different in CSR and SSR due to cookie paths, origins, http only, etc., so I think it's probably better/safer to read from the Request in SSR and document.cookie in CSR. It feels like it would be error-prone to try to fill in the request cookies from document.cookie and share the same code, but we can open this as a proposal and see what other's think.

@remix-run remix-run locked and limited conversation to collaborators Aug 28, 2023
@brophdawg11 brophdawg11 converted this issue into discussion #10825 Aug 28, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
Projects
None yet
Development

No branches or pull requests

2 participants