krazkidd / lineup

Softball lineup editor made with Nuxt 3 (Vue.js 3)
https://lineup.app.xericode.net
GNU Affero General Public License v3.0
3 stars 2 forks source link

Turn SSR off and use Pinia store as recommended #3

Closed krazkidd closed 10 months ago

krazkidd commented 10 months ago

After further experimentation, it is clear there is an issue with Pinia and (client side) hydration.

During SSR, our store returns default values because local storage state is local to the server, and these values are serialized and rendered and sent to the client. The client is not hydrating with store values derived from its own local storage but is rather using the server-provided values.

While Pinia supports SSR, it is not guaranteed to work well when server and client state differ, e.g. with local storage. This scenario may be better supported in the future. (See "Advanced SSR" here.)

To fix the hydration issue, we turn SSR off in this PR (ssr: false). Though SSR is one of Nuxt's primary selling points, not all modules support universal rendering, and using local storage complicates that even more.

The fortunate news is ssr: false allows us to implement the Pinia app settings store as recommended by the main Pinia dev. That is, we use computed() state props so we can control writes and persist changes to the local storage. This simplification makes two-way binding easier to set up in Vue templates and components are refactored as necessary.

We should also still be able to statically render the app in a client-side render only mode with this change (and make continued use free static site hosting).