Replies: 3 comments 3 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 runscreateSessionwhenreadDatacannot find a corresponding entry.Another method that does not run
createSessionin such a case is required when you develop a member-only application and expose its login page to the public.If
createSessionis called too many times (e.g. by DoS attack), the performance of a database could be degraded by helpless and meaningless read queries to the session table/storage (e.g.SELECT * FROM session WHERE id = ''). You have to addif (!id /* or id === "" */) return nullat the beginning ofreadDatato prevent invoking such extra helpless queries. Also, you have to add.id()to the predicate in theifstatement to determine whether a session definitely exists.It should be if you do not touch the session when the user does not have a cookie:
We want to prevent extra execution of
tryRead(""):We want make
if (!id) return null;unnecessary by preventing extra execution ofreadDatawhen 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 operation and force adding the wasteful check 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