Jaguar-dart / jaguar

Jaguar, a server framework built for speed, simplicity and extensible. ORM, Session, Authentication & Authorization, OAuth
http://jaguar-dart.github.io
463 stars 34 forks source link

responseProcessor not called for staticFiles #140

Closed PiN73 closed 3 years ago

PiN73 commented 3 years ago

I want to override response mime type when serving static files.

Tried to use responseProcessor

final server = Jaguar();
server.staticFiles(
  '/static/*',
  'static',
  responseProcessor: (Context context, dynamic result) {
    print('INSIDE responseProcessor');
  },
);
await server.serve();

but when the files are requested, my callback isn't called.

Is it a bug in Jaguar or am I doing something wrong?

PiN73 commented 3 years ago

Discovering https://github.com/Jaguar-dart/jaguar/pull/137 I found that overriding mime type is possible this way for now

final server = Jaguar();
server.staticFiles('/static/*', 'static');
server.after.add((ctx) {
  if (ctx.path.startsWith('/static/') && ctx.path.endsWith('.txt')) {
    // for example, when serving .txt files, set mime type to text/html
    ctx.response.headers.mimeType = MimeTypes.html;
  }
});
await server.serve();
PiN73 commented 3 years ago

Anyway it would be good to know if responseProcessor should be called by Jaguar in my case

tejainece commented 3 years ago

@PiN73 responseProcessor is only called when you return a result from the route handler. Static file handler does not return result, it instead updates the response object.

Using after is the right way to go for this.

PiN73 commented 3 years ago

@tejainece but then responseProcessor parameter inside Jaguar@staticFiles isn't consistent and shouldn't be present, should it?