|
| 1 | +# Flutter web apps |
| 2 | + |
| 3 | +Serverpod can serve your Flutter web application directly, allowing you to host both your API and web frontend from the same server. `FlutterRoute` handles the specifics of serving Flutter web apps, including WASM multi-threading headers and SPA-style routing. |
| 4 | + |
| 5 | +## Basic setup |
| 6 | + |
| 7 | +Use `FlutterRoute` to serve your Flutter web build: |
| 8 | + |
| 9 | +```dart |
| 10 | +pod.webServer.addRoute( |
| 11 | + FlutterRoute(Directory('web/app')), |
| 12 | + '/**', |
| 13 | +); |
| 14 | +``` |
| 15 | + |
| 16 | +This configuration: |
| 17 | + |
| 18 | +- Serves all static files from the Flutter build |
| 19 | +- Falls back to `index.html` for client-side routing |
| 20 | +- Adds WASM multi-threading headers automatically |
| 21 | + |
| 22 | +## Building Flutter for web |
| 23 | + |
| 24 | +Build your Flutter app for web with WASM support for improved performance and multi-threading: |
| 25 | + |
| 26 | +```bash |
| 27 | +cd my_project_flutter |
| 28 | +flutter build web --wasm |
| 29 | +``` |
| 30 | + |
| 31 | +:::info |
| 32 | + |
| 33 | +WASM builds automatically fall back to JavaScript in browsers that don't support WebAssembly Garbage Collection (WasmGC). Your app works everywhere while taking advantage of WASM performance where available. |
| 34 | + |
| 35 | +::: |
| 36 | + |
| 37 | +## Project structure |
| 38 | + |
| 39 | +Copy your Flutter build output to the server's `web` directory: |
| 40 | + |
| 41 | +```text |
| 42 | +my_project/ |
| 43 | +├── my_project_server/ |
| 44 | +│ ├── lib/ |
| 45 | +│ │ └── server.dart |
| 46 | +│ └── web/ |
| 47 | +│ └── app/ # Flutter web build output |
| 48 | +│ ├── index.html |
| 49 | +│ ├── main.dart.js |
| 50 | +│ ├── flutter.js |
| 51 | +│ └── ... |
| 52 | +├── my_project_flutter/ |
| 53 | +│ └── build/ |
| 54 | +│ └── web/ # Flutter build output (source) |
| 55 | +└── my_project_client/ |
| 56 | +``` |
| 57 | + |
| 58 | +## WASM multi-threading |
| 59 | + |
| 60 | +Flutter WASM builds can use multi-threaded rendering for improved performance. This requires `SharedArrayBuffer`, which browsers only enable with specific security headers. |
| 61 | + |
| 62 | +`FlutterRoute` automatically adds these headers: |
| 63 | + |
| 64 | +- `Cross-Origin-Opener-Policy: same-origin` |
| 65 | +- `Cross-Origin-Embedder-Policy: require-corp` |
| 66 | + |
| 67 | +### Using WasmHeadersMiddleware directly |
| 68 | + |
| 69 | +If you're using `SpaRoute` or custom routes instead of `FlutterRoute`, add the headers manually with `WasmHeadersMiddleware`: |
| 70 | + |
| 71 | +```dart |
| 72 | +pod.webServer.addMiddleware(const WasmHeadersMiddleware(), '/**'); |
| 73 | +
|
| 74 | +pod.webServer.addRoute( |
| 75 | + SpaRoute( |
| 76 | + Directory('web/app'), |
| 77 | + fallback: File('web/app/index.html'), |
| 78 | + ), |
| 79 | + '/**', |
| 80 | +); |
| 81 | +``` |
| 82 | + |
| 83 | +## Cache control |
| 84 | + |
| 85 | +Configure caching for Flutter's static assets: |
| 86 | + |
| 87 | +```dart |
| 88 | +pod.webServer.addRoute( |
| 89 | + FlutterRoute( |
| 90 | + Directory('web/app'), |
| 91 | + cacheControlFactory: StaticRoute.publicImmutable( |
| 92 | + maxAge: const Duration(minutes: 5), |
| 93 | + ), |
| 94 | + ), |
| 95 | + '/**', |
| 96 | +); |
| 97 | +``` |
| 98 | + |
| 99 | +See [Static Files](static-files#cache-control) for more on cache control. |
| 100 | + |
| 101 | +## Cache busting |
| 102 | + |
| 103 | +Enable cache-busted URLs: |
| 104 | + |
| 105 | +```dart |
| 106 | +final webDir = Directory('web/app'); |
| 107 | +
|
| 108 | +final cacheBustingConfig = CacheBustingConfig( |
| 109 | + mountPrefix: '/', |
| 110 | + fileSystemRoot: webDir, |
| 111 | +); |
| 112 | +
|
| 113 | +pod.webServer.addRoute( |
| 114 | + FlutterRoute( |
| 115 | + webDir, |
| 116 | + cacheBustingConfig: cacheBustingConfig, |
| 117 | + cacheControlFactory: StaticRoute.publicImmutable( |
| 118 | + maxAge: const Duration(minutes: 5), |
| 119 | + ), |
| 120 | + ), |
| 121 | + '/**', |
| 122 | +); |
| 123 | +``` |
| 124 | + |
| 125 | +See [Static Files](static-files#static-file-cache-busting) for more on cache busting. |
| 126 | + |
| 127 | +## Complete example |
| 128 | + |
| 129 | +Here's a complete `server.dart` serving a Flutter web app: |
| 130 | + |
| 131 | +```dart |
| 132 | +import 'dart:io'; |
| 133 | +
|
| 134 | +import 'package:serverpod/serverpod.dart'; |
| 135 | +
|
| 136 | +import 'src/generated/protocol.dart'; |
| 137 | +import 'src/generated/endpoints.dart'; |
| 138 | +
|
| 139 | +void run(List<String> args) async { |
| 140 | + final pod = Serverpod(args, Protocol(), Endpoints()); |
| 141 | +
|
| 142 | + final flutterAppDir = Directory('web/app'); |
| 143 | +
|
| 144 | + if (!flutterAppDir.existsSync()) { |
| 145 | + print('Warning: Flutter web app not found at ${flutterAppDir.path}'); |
| 146 | + print('Build your Flutter app and copy it to web/app/'); |
| 147 | + } else { |
| 148 | + pod.webServer.addRoute(FlutterRoute(flutterAppDir), '/**'); |
| 149 | + } |
| 150 | +
|
| 151 | + await pod.start(); |
| 152 | +} |
| 153 | +``` |
| 154 | + |
| 155 | +With this configuration, your Flutter web app is served at the root URL of your web server (typically `http://localhost:8082` in development). |
0 commit comments