Skip to content

Commit

Permalink
Remove session.flash for now (#12745)
Browse files Browse the repository at this point in the history
  • Loading branch information
ascorbic authored Dec 16, 2024
1 parent 658d7f2 commit bbfe1d1
Show file tree
Hide file tree
Showing 6 changed files with 2 additions and 57 deletions.
12 changes: 2 additions & 10 deletions packages/astro/src/core/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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;
}
}
}

Expand Down
14 changes: 0 additions & 14 deletions packages/astro/test/fixtures/sessions/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

This file was deleted.

4 changes: 0 additions & 4 deletions packages/astro/test/fixtures/sessions/src/pages/flash.astro

This file was deleted.

5 changes: 0 additions & 5 deletions packages/astro/test/fixtures/sessions/src/pages/index.astro
Original file line number Diff line number Diff line change
@@ -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>
Expand All @@ -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>
20 changes: 0 additions & 20 deletions packages/astro/test/units/sessions/astro-session.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
})


});

Expand Down

0 comments on commit bbfe1d1

Please sign in to comment.