Skip to content

Instantly share code, notes, and snippets.

@felangel
Created December 14, 2022 16:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save felangel/5d86c44e39ecb22bed73da78795da769 to your computer and use it in GitHub Desktop.
Save felangel/5d86c44e39ecb22bed73da78795da769 to your computer and use it in GitHub Desktop.
Dart Frog Trackable Static Files
import 'dart:io';
import 'package:dart_frog/dart_frog.dart';
Future<HttpServer> run(Handler handler, InternetAddress ip, int port) {
final cascade = Cascade().add(trackableStaticFileHandler).add(handler);
return serve(cascade.handler, ip, port);
}
Future<Response> trackableStaticFileHandler(RequestContext context) async {
const customStaticFilePath = 'api/static';
final handler = createStaticFileHandler(path: customStaticFilePath);
final response = await handler(context);
final assetPath = context.request.url.path.split(customStaticFilePath).last;
if (response.statusCode == HttpStatus.ok) {
// A static asset was requested!
print('requested $assetPath');
}
return response;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment