Skip to content

Conversation

@brophdawg11
Copy link
Contributor

@MichaelDeBoey MichaelDeBoey linked an issue Oct 23, 2025 that may be closed by this pull request
@brophdawg11 brophdawg11 force-pushed the brophdawg11/session branch 2 times, most recently from 9dc0e74 to d15afe1 Compare October 23, 2025 14:16
Copy link
Member

@mjackson mjackson left a 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.

@brophdawg11 brophdawg11 force-pushed the brophdawg11/session branch 2 times, most recently from 0e06933 to 62f95fa Compare October 29, 2025 18:57
@@ -0,0 +1 @@
export { createFileSessionStorage } from './lib/storage/file-storage.ts'
Copy link
Contributor Author

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

Comment on lines +15 to +16
// Store carts by cartId, cartId will be stored in the session
let nextCartId = 1
Copy link
Contributor Author

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)
Copy link
Contributor Author

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

Comment on lines +4 to +7
declare module '@remix-run/session' {
interface SessionData {
cartId?: string
userId?: string
Copy link
Contributor Author

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],
Copy link
Contributor Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add session-middleware to fetch-router Add built-in session support in fetch-router Add session package

4 participants