Static files and SPA
Serve a folder of files, or a single page app (React, Vue, Flutter web), from the Dox app, with the right cache headers.
With the front-end in the same app as your API, you have one server to run and no CORS setup.
What you need
- A Dox app.
- A
publicfolder with the built front-end (index.html,assets/...). - For the storage disk and the controller below: the
dox_storagepackage, with its provider. See File storage.
Serve a folder: one line
Route.staticFiles serves the files of a folder. Add it in register() of WebRouter:
Now /app.js sends public/app.js. A route wins over a file with the same path. For a Cache-Control header, add maxAge. To send the .br and .gz files of your build, add precompressed: true. See Routing: serve static files.
For a single page app, add a fallback route that sends index.html.
Or serve a storage disk
Storage.route serves every file of a disk. First add a public disk for the folder:
StorageConfig storageConfig = StorageConfig(
defaultDisk: env.storageDisk,
disks: <String, Map<String, dynamic>>{
'local': <String, dynamic>{'driver': 'local', 'root': 'storage'},
/// the front-end files
'public': <String, dynamic>{'driver': 'local', 'root': 'public'},
},
);
Then add the route in register() of WebRouter:
Now /static/app.js sends public/app.js. This route sends no Cache-Control header.
Serve an SPA
A single page app (SPA) needs two things:
index.htmlfor every URL the SPA router handles, with no cache.- The built assets with a long cache. Build tools put a hash in the file name (
app.3f9c2b.js), so a new build has new names.
1. Write the controller
Write one method for the assets and one for index.html:
import 'dart:io';
import 'package:dox_core/dox_core.dart';
import 'package:dox_storage/dox_storage.dart';
@Inject()
class StaticController {
/// GET /assets/{path*}
/// Built files have a hash in the name, so the browser may keep them
/// for one year. The `public` disk rejects `..` in the path.
Future<DoxResponse> asset(String path) async {
StreamFile file = await Storage().disk('public').stream(path);
return response(file).cache(const Duration(days: 365));
}
/// GET / and GET /{path*}
/// Every other URL gets index.html, so the router of the SPA can
/// handle it. Never cache it, so a new deploy shows at once.
/// An unknown API URL stays a 404, it does not get the page.
DoxResponse index(DoxRequest req) {
if (req.uri.path.startsWith('/api/')) {
throw NotFoundHttpException();
}
return response(File('public/index.html')).cache(Duration.zero);
}
}
cache(duration) sets Cache-Control: max-age=<seconds>.
2. Add the routes
The catch-all route matches every URL, so it must come last. Put the API router before the web router in AppConfig:
import 'package:dox_core/dox_core.dart';
import 'package:my_app/app/http/controllers/static.controller.dart';
class WebRouter extends Router {
@override
List<dynamic> get middleware => <dynamic>[];
@override
void register() {
// the SPA: assets with a long cache, every other URL gets index.html
Route.get('/assets/{path*}', (StaticController c) => c.asset);
Route.get('/', (StaticController c) => c.index);
Route.get('/{path*}', (StaticController c) => c.index);
}
}
Set the order of the routers:
AppConfig appConfig = AppConfig(
/// the SPA catch-all route is in WebRouter, so it must come last.
/// Listed routers come first, in this order.
routers: <Router>[ApiRouter(), WebRouter()],
// ...
);
Keep the route for /, because /{path*} does not match the start page.
3. Put the build in public
Build the front-end and copy the files to public:
# React (Vite)
npm run build && cp -r dist/. public/
# Flutter web
flutter build web && cp -r build/web/. public/
Flutter web puts the files next to index.html, not in assets/. So for Flutter, change the asset route to match.
Check it
The start page comes with no cache:
Any other page also gets index.html, and an asset gets a cache of one year:
curl -i http://localhost:3000/settings/profile # same answer: index.html
curl -i http://localhost:3000/assets/app.js
/assets/nope.js, /assets/..%2Fpubspec.yaml and /api/nothing answer 404.
In production
Put a proxy in front of Dox, and let it do gzip. For a big site, serve public from the proxy or a CDN. To read more files from the disk at once, raise fileThreads.
Next steps
- Routing: serve storage files: all the options of
Storage.route. - Response: files and big responses: send a file.
- File storage: disks and drivers.