Initialization
Before callingrunApp() on the server, you must initialize Jaspr with options:
The options file is automatically generated when you use features like
@client components or @css styles. The file is typically named <entrypoint>.server.options.dart.Jaspr.initializeApp()
Initializes the Jaspr framework on the server with the provided configuration.Signature
The server options configuration. Usually the auto-generated
defaultServerOptions.Whether to use isolates for handling requests. This can improve performance for CPU-intensive operations.
The file extensions that are allowed to be server-rendered. Used for static site generation.
Example
ServerOptions
TheServerOptions class contains the configuration for server-side rendering.
Signature
An identifier for the client bundle. Auto-generated based on your project.
A map of component types to their client targets. This is populated from
@client annotated components.A function that returns global style rules. This is populated from
@css annotated styles.ClientTarget
Defines the target configuration for a@client component.
Signature
The name/identifier of the client component.
Optional function to extract parameters from the component for serialization.
Generated Options File
When you use@client components, Jaspr automatically generates an options file alongside your entry point.
File Location
For a filelib/main.dart, the generated files are:
lib/main.server.options.dart- Server-side optionslib/main.client.options.dart- Client-side options (if applicable)
Example Generated File
Manual Configuration
While not recommended, you can manually create server options:Checking Initialization
You can check if Jaspr has been initialized:Options Access
Access the current options (read-only):Build Configuration
To generate the options file, ensure yourbuild.yaml includes the Jaspr builder:
Migration Notes
From v0.14 and Earlier
In older versions of Jaspr, a singlejaspr_options.dart file was generated. Starting from v0.22.0:
- The options file is now generated as
<file>.server.options.dartalongside the entry point - An additional
<file>.client.options.dartis generated for client-side code - You must update your imports from
jaspr_options.darttomain.server.options.dart
Common Issues
”Jaspr was not initialized”
This error occurs when you callrunApp() without first calling Jaspr.initializeApp().
Solution:
Options File Not Generated
If the options file is not being generated:- Ensure you have
@clientcomponents or other features that require code generation - Run
dart run build_runner buildorjaspr serve - Check that
jaspr_builderis in yourdev_dependencies
Import Errors
If you see import errors for the generated file:- Make sure you’ve run the build process
- Check that the path matches your entry point file name
- Clean and rebuild:
dart run build_runner clean && dart run build_runner build
See Also
- runApp() - Running your Jaspr application
- @client Components - Client component annotation
- @css Styles - CSS-in-Dart styling
- Jaspr Lints - Linting configuration