Replies: 3 comments 6 replies
-
|
Implementation: |
Beta Was this translation helpful? Give feedback.
-
|
Creating a session when the user has no intention of logging in is a waste of resources. |
Beta Was this translation helpful? Give feedback.
-
|
I think what you're probably running into is calling You can check |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
getSessionalways runscreateSessionregardless of whether the user has a corresponding entry in the cookie.It always creates, wraps, and returns a JS's Map even when it will not be used in the caller (e.g. when you check the user is a member registered in a database):
react-router/packages/react-router/lib/server-runtime/sessions.ts
Lines 93 to 140 in 255ac96
Also, a beginner who does not read the document will not know to determine whether the user has a session. The correct way:
However, the JSDoc string of
SessionStorage.idis just:Who will learn that they can use this
.idto determine whether the user has a session onlyfrom this description?To make matters worth, this way cannot be applied to a storage created by
createCookieSessionStorage.idis always""there. You have no way to tell whether the session is missing or tampered/corrupted without the help ofcreateCookie(Cookie).createCookiehas no way to leave the default recommended cookie name ("__session") for a session to React Router unlikecreateCookieSessionStorageorcreateSessionStorage.A nullish value (
nullorundefined) will be more intuitive and versatile if you do not touch the session when the user does not have a cookie:API:
vs
react-router/packages/react-router/lib/server-runtime/sessions.ts
Lines 266 to 270 in 255ac96
The current
getSessionwill perform a wasteful operationcreateSession, i.e. creating an extra, wasteful, and unused object andMap, even if an user has no merit to receive a session data.vs
As you might notice, it returns just
nullwithout creating new one when no entry.This will not be a breaking change unless you use third party session libraries. However, an author of a third party session library have to modify their library to implement this method. Unfortunately JavaScript/TypeScript cannot provide default method implementations in an interface unlike Java or C#.
Beta Was this translation helpful? Give feedback.
All reactions