@@ -15,6 +15,7 @@ export type FlashOptions = Partial<{
1515type FlashContext = {
1616 store : Writable < App . PageData [ 'flash' ] > ;
1717 options : FlashOptions ;
18+ optionHash : string ;
1819} ;
1920
2021const flashStores = new WeakMap < Readable < Page > , FlashContext > ( ) ;
@@ -41,22 +42,7 @@ export function initFlash(
4142
4243function _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`.
0 commit comments