Search code examples
blazorblazor-server-side

Is PersistentComponentState tied to the SignalR circuit or global to the app?


For Blazor render mode InteractiveServer Microsoft says the way to avoid reading in data twice for a page (pre-render and again post-render) is to save it the first time using PersistentComponentState and then the second time just use the data saved off to it.

Is this persisted data global to the app or is it scope level data tied to the session/circuit? Reading the docs it sounds like it's global, but it suggest as the key nameof({VARIABLE}) and for multiple users hitting the same page, they would have the same key.

That will work if the data is per session/circuit. That will have key name conflicts if it is global.

And follow up question, if it's global, why not use MemoryCache? Which solves the re-read time hit not only for pre/post render, but for multiple people hitting the same page who should be shown the same data (like Events today).


Solution

  • I've tested this and the PersistentComponentState is tied to the SignalR circuit (i.e. session) according to my tests. I'd prefer there was a categorical statement to that effect from Microsoft so I know we could depend on this going forward, but assuming I didn't screw up in my testing, it's tied to the circuit.

    Also, remember that is is serializing to/from JSON and so if what you save is more than a couple of strings and/or numbers, there's a performance hit.