jaspr-pre-rendering-and-hydration
Installation
SKILL.md
This skill is relevant for static and server mode Jaspr projects. You can check the mode of a Jaspr project in pubspec.yaml under jaspr: mode: <mode>.
Core Rules for Pre-rendering (SSR/SSG)
Jaspr pre-renders components at request/build-time on the server. Interactive browser features require hydration.
- Dual Entrypoints:
- Server:
lib/main.server.dart-> CallsrunApp(Document(body: MyApp())) - Client:
lib/main.client.dart-> CallsrunApp(ClientApp())
- Server:
- Library Constraints:
- Importing: You MUST NOT import
dart:js_interoporpackage:webin any code imported by the server entrypoint. - Importing: You MUST NOT import
dart:ioordart:ffiin any code imported by the client entrypoint. - Using: You can import
package:universal_webin shared code, but you MUST wrap any access to its browser APIs inside anif (kIsWeb)check to prevent crashing during server-side rendering.
- Importing: You MUST NOT import
- Interactivity / Hydration: Components are server-rendered by default. To add client interactivity (e.g., event listeners, state, using browser APIs), you MUST annotate a component with
@client.
The @client Annotation and ClientApp
When you annotate a component with @client, Jaspr renders its initial HTML on the server and "hydrates" it in the browser.