Skip to content

Commit 901a988

Browse files
committed
Fixed option comparison.
1 parent 2776cfb commit 901a988

File tree

4 files changed

+22
-29
lines changed

4 files changed

+22
-29
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ Headlines: Added, Changed, Deprecated, Removed, Fixed, Security
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [2.3.1] - 2024-01-07
9+
10+
### Fixed
11+
12+
- Options weren't compared properly, causing problems when navigating.
13+
814
## [2.3.0] - 2023-12-15
915

1016
### Added

src/lib/client.ts

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export type FlashOptions = Partial<{
1515
type FlashContext = {
1616
store: Writable<App.PageData['flash']>;
1717
options: FlashOptions;
18+
optionHash: string;
1819
};
1920

2021
const flashStores = new WeakMap<Readable<Page>, FlashContext>();
@@ -41,22 +42,7 @@ export function initFlash(
4142

4243
function _initFlash(page: Readable<Page>, options?: FlashOptions): Writable<App.PageData['flash']> {
4344
const flashStore = flashStores.get(page);
44-
45-
if (flashStore && !options) {
46-
/*
47-
console.log(
48-
{
49-
hasStore: true,
50-
route: get(page).route.id,
51-
customOptions: flashStore.options !== defaultOptions
52-
},
53-
flashStore.options
54-
);
55-
*/
56-
return flashStore.store;
57-
} else if (flashStore && options && flashStore.options !== options) {
58-
throw new Error('getFlash options can only be set once, at a top-level component.');
59-
}
45+
if (flashStore && !options) return flashStore.store;
6046

6147
const currentOptions: FlashOptions = options
6248
? {
@@ -69,19 +55,12 @@ function _initFlash(page: Readable<Page>, options?: FlashOptions): Writable<App.
6955
}
7056
: defaultOptions;
7157

58+
if (flashStore && options && serializeOptions(currentOptions) !== flashStore.optionHash) {
59+
throw new Error('getFlash options can only be set once, at a top-level component.');
60+
}
61+
7262
const store = writable<App.PageData['flash']>();
73-
const context = { store, options: currentOptions };
74-
75-
/*
76-
console.log(
77-
{
78-
hasStore: false,
79-
route: get(page).route.id,
80-
customOptions: context.options !== defaultOptions
81-
},
82-
context.options
83-
);
84-
*/
63+
const context = { store, options: currentOptions, optionHash: serializeOptions(currentOptions) };
8564

8665
flashStores.set(page, context);
8766
clearCookieAndUpdateIfNewData(context, get(page).data.flash);
@@ -145,6 +124,10 @@ function _initFlash(page: Readable<Page>, options?: FlashOptions): Writable<App.
145124
return store;
146125
}
147126

127+
function serializeOptions(opts: FlashOptions) {
128+
return JSON.stringify(opts);
129+
}
130+
148131
/**
149132
* Retrieves the flash message store for display or modification.
150133
* @param page Page store, imported from `$app/stores`.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<script>
2+
import {error} from '@sveltejs/kit';
3+
throw error(500);
4+
</script>

src/routes/(app)/multi-init/+page.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
let error = '';
66
77
try {
8-
getFlash(page, { clearOnNavigate: true });
8+
getFlash(page, { clearOnNavigate: false });
99
} catch (e) {
1010
error = String(e);
1111
}

0 commit comments

Comments
 (0)