0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-03-17 23:11:29 -05:00

Remove session.flash for now (#12745)

This commit is contained in:
Matt Kane 2024-12-16 15:37:31 +00:00 committed by GitHub
parent 658d7f274f
commit bbfe1d10ab
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 2 additions and 57 deletions

View file

@ -23,7 +23,7 @@ const VALID_COOKIE_REGEX = /^[\w-]+$/;
interface SessionEntry {
data: any;
expires?: number | 'flash';
expires?: number;
}
export class AstroSession<TDriver extends SessionDriverName = any> {
@ -122,7 +122,7 @@ export class AstroSession<TDriver extends SessionDriverName = any> {
* Sets a session value. The session is created if it does not exist.
*/
set<T = any>(key: string, value: T, { ttl }: { ttl?: number | 'flash' } = {}) {
set<T = any>(key: string, value: T, { ttl }: { ttl?: number } = {}) {
if (!key) {
throw new AstroError({
...SessionStorageSaveError,
@ -157,10 +157,6 @@ export class AstroSession<TDriver extends SessionDriverName = any> {
this.#dirty = true;
}
flash<T = any>(key: string, value: T) {
this.set(key, value, { ttl: 'flash' });
}
/**
* Destroys the session, clearing the cookie and storage if it exists.
*/
@ -320,10 +316,6 @@ export class AstroSession<TDriver extends SessionDriverName = any> {
const expired = typeof value.expires === 'number' && value.expires < now;
if (!this.#data.has(key) && !this.#toDelete.has(key) && !expired) {
this.#data.set(key, value);
if (value?.expires === 'flash') {
this.#toDelete.add(key);
this.#dirty = true;
}
}
}

View file

@ -7,20 +7,6 @@ export const onRequest = defineMiddleware(async (context, next) => {
// Skip requests for prerendered pages
if (context.isPrerendered) return next();
if(context.url.searchParams.has('setFlash') && context.url.pathname === '/') {
context.session.flash('middleware-flash', `Flashed message at ${new Date().toISOString()}`);
}
if(context.url.pathname === '/next-rewrite-middleware') {
context.session.flash('middleware-flash', `Flashed rewrite message at ${new Date().toISOString()}`);
return next('/');
}
if(context.url.pathname === '/ctx-rewrite-middleware') {
context.session.flash('middleware-flash', `Flashed rewrite message at ${new Date().toISOString()}`);
return context.rewrite(new Request(new URL('/', context.url)));
}
const { action, setActionResult, serializeActionResult } =
getActionContext(context);

View file

@ -1,4 +0,0 @@
---
Astro.session.flash('flash-value', `Flashed value at ${new Date().toISOString()}`);
return Astro.rewrite('/');
---

View file

@ -1,4 +0,0 @@
---
Astro.session.flash('flash-value', `Flashed value at ${new Date().toISOString()}`);
return Astro.redirect('/');
---

View file

@ -1,7 +1,5 @@
---
const value = await Astro.session.get('value');
const flash = await Astro.session.get('flash-value');
const middlewareFlash = await Astro.session.get('middleware-flash');
---
<html lang="en">
<head>
@ -11,8 +9,5 @@ const middlewareFlash = await Astro.session.get('middleware-flash');
<h1>Hi</h1>
<p>{value}</p>
<p>Flash: {flash}</p>
<p>middlewareFlash: {middlewareFlash}</p>
<p><a href="/flash">Set flash</a></p>
<a href="/cart" style="font-size: 36px">🛒</a>
</html>

View file

@ -167,26 +167,6 @@ test('AstroSession - Data Persistence', async (t) => {
});
await t.test('should not write flash session data to storage a second time', async () => {
let storedData;
const mockStorage = {
get: async () => stringify(new Map([['key', { data: 'value', expires: "flash" }]])),
setItem: async (_key, value) => {
storedData = value;
},
};
const session = createSession(defaultConfig, defaultMockCookies, mockStorage);
const value = await session.get('key');
assert.equal(value, 'value');
await session[PERSIST_SYMBOL]();
const emptyMap = devalueStringify(new Map());
assert.equal(storedData, emptyMap);
})
});