Replies: 5 comments 13 replies
-
Love everything proposed here! I agree that sessions should be pluggable. I'd personally reach for Upstash + a serverless deploy platform to implement sessions. Should it be possible for adapters to inject a default session handler that can be overridden in the config? Or should it be an explicit |
Beta Was this translation helpful? Give feedback.
-
Looks great, would love to see this as a primitive in Astro. We're bringing back PHP. |
Beta Was this translation helpful? Give feedback.
-
I might be missing something, but this seems pretty trivial to implement in userland with |
Beta Was this translation helpful? Give feedback.
-
Cool! Would this also be useable for sharing data between server and client (JS frameworks)? |
Beta Was this translation helpful? Give feedback.
-
This has been accepted to stage 2. Continue any discussion here: #1050 |
Beta Was this translation helpful? Give feedback.
-
Summary
I am proposing a first class
Astro.session
primitive, with pluggable storage backends (possibly based on unstorage). Inspired by PHP sessions and Rails sessions, this will allow on-demand rendered pages and API endpoints to use a newAstro.session
object to access arbitrary data, scoped to that browser session. A session ID cookie is used to associate the browser with the session, but the actual data is stored on the backend.Background and motivation
HTTP is a stateless protocol, so sharing data between requests is a problem that most server-rendered web apps need to solve. The standard way is using cookies, but these have limitations. Firstly, they are limited to 4kB in size, so can't be used for anything complex. The full data also needs to be sent along with every request and response, increasing their size. Finally, even when encrypted they can be vulnerable to replay attacks. We have encountered the limitations of cookie storage when building Astro Actions, where we need response data to persist between page redirects. It is easy to reach the 4kB limit in these cases.
For this reasons, most non-trivial apps need to implement some kind of server-side session handling. In most cases these work by storing a random session id in a cookie, and using that to retrieve the full session data on the server. Monolithic frameworks such as Rails and Laravel normally implement sessions at the framework or server level, and PHP has built-in session handling which heavily influenced this proposal. However it isn't a common primitive for modern JS frameworks, in part due to the fact that these need to work in serverless environments, so simple default backends such as filesystem storage cannot be relied upon. We propose addressing this by allowing adapters to provide default implementations, relying on the primitives that they have available.
Goals
Astro.cookies
(Astro.session.get()
,Astro.session.set()
)Astro.session.regenerateId()
andAstro.session.destroy()
session
config object.Non-goals
Example
References
Beta Was this translation helpful? Give feedback.
All reactions