Example to show firebase auth used on the client and accessed via a Svelte Store.
Updated to show lazy-loading the Firebase 9.0 SDK for faster startup / higher lighthouse score.
Updated to show syncing the client-side auth state with the server to set a cookie and provide a SvelteKit "session"
Updated to use the new Firebase 9.0 SDK, so lazy-loading firebase from the CDN has been removed.
Firebase SDK Packages are lazy-loaded from the CDN. This ensures that the application starts up quickly (rather than waiting for the large firebase bundles to load) and also auth can load and initialize before other packages (e.g. firestore isn't loaded unless the user is signed in and on a route that requests data).
The auth store ensures that the firebase code is only loaded and called when the page is running in the browser and when the auth status has been referenced. Comment out the <Auth />
import in $layout.svelte
and notice that no firebase libs are requested.
A simpler example could just include the firebase CDN scripts in the app.html
file but I find this approach keeps the page zippy while also avoiding re-bundling the firebase SDK into the app so it has the benefit of potentially loading faster via the CDN.
Some additional pieces I'll try to add: