- 
                Notifications
    You must be signed in to change notification settings 
- Fork 2.7k
Bring over RR session code into @remix-run/session package #10797
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
base: brophdawg11/cookies
Are you sure you want to change the base?
Conversation
003723b    to
    80537fe      
    Compare
  
    9dc0e74    to
    d15afe1      
    Compare
  
    There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall we are headed in the right direction, thanks for the effort here this week! I hope the feedback helps.
0e06933    to
    62f95fa      
    Compare
  
    …t of fetch-router in the browser bundle
62f95fa    to
    7350dde      
    Compare
  
    | @@ -0,0 +1 @@ | |||
| export { createFileSessionStorage } from './lib/storage/file-storage.ts' | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I needed to move this to a sub-export so that it didn't get picked up when a client-side API imported @remix-run/fetch-router and @remix-run/fetch-router importing the agnostic session stuff from @remix-run/sesson.
This happened from the bookstore routes.ts:
import { route, formAction, resources } from '@remix-run/fetch-router'
export let routes = route({ ... })When we tried to run pnpm dev:browser, it would error on the file-storage imports of node:fs
| // Store carts by cartId, cartId will be stored in the session | ||
| let nextCartId = 1 | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No longer storing by session id because that's not something app code can always directly access (sesson.id isn't populated on initial session creation - only when we eventually commit the session).  We don't want to commit a session until they interact with it, so there's no good way to key a cart by sessionId the first time we interact with the session.  Instead, we use a cartId that we store in the session (which "activates" the session and triggers the cookie), and we key the cart lookup by that.
| * To be used on any route that mutates the cart | ||
| */ | ||
| export const ensureCart: Middleware = async ({ session }) => { | ||
| createCartIfNotExists(session) | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Middleware used by the cart API routes to ensure we always have a cart available if we're trying to mutate the cart
| declare module '@remix-run/session' { | ||
| interface SessionData { | ||
| cartId?: string | ||
| userId?: string | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Module augmentation to strongly type our session
| async add({ storage, formData }) { | ||
| // Simulate network latency | ||
| await new Promise((resolve) => setTimeout(resolve, 1000)) | ||
| use: [ensureCart], | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cart middleware that runs before all cart API handlers
Stacked on #10796
Adds a new
@remix-run/sessionpackage - brought over from https://github.com/remix-run/react-router/blob/main/packages/react-router/lib/server-runtime/sessions.ts