Created
December 14, 2022 16:00
-
-
Save felangel/5d86c44e39ecb22bed73da78795da769 to your computer and use it in GitHub Desktop.
Dart Frog Trackable Static Files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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