VeryGoodOpenSource / dart_frog

A fast, minimalistic backend framework for Dart 🎯
https://dartfrog.vgv.dev
MIT License
1.83k stars 148 forks source link

feat: Serve index.html by default #445

Open Tienisto opened 1 year ago

Tienisto commented 1 year ago

Description

I have a single page application written in Vue which gets compiled into public/

I think it is pragmatic to serve index.html in public by default for / if there is no / controller in dart code

enquestor commented 1 year ago

Second this. Just found out this package and willing to try it out. Surprised that this isn't a thing. Is there currently any workaround? Worse case is to serve static files separately behind a reverse proxy I guess.

Tienisto commented 1 year ago

There is a workaround without a proxy:

https://github.com/VeryGoodOpenSource/dart_frog/issues/90#issuecomment-1244807334

routes/
  - index.dart
import 'dart:io';

import 'package:dart_frog/dart_frog.dart';
import 'package:path/path.dart' as path;

Response onRequest(RequestContext context) {
  final file = File(path.join(Directory.current.path, 'public', 'index.html'));
  if (!file.existsSync()) {
    return Response(body: 'Index Not found');
  }
  final indexHtml = file.readAsStringSync();
  return Response(body: indexHtml, headers: {'Content-Type': 'text/html'});
}
enquestor commented 1 year ago

Thanks for the heads-up!