rails/request.js

Support for responseKind js/script

PedroAugustoRamalhoDuarte opened this issue · 5 comments

In my project we use ajax to handle request in stimulus and we are trying to update to request.js:

Example:

$.ajax({
  beforeSend: function (xhr) {
    xhr.setRequestHeader('X-CSRF-Token', $('meta[name="csrf-token"]').attr('content'));
  },
  url: url,
  remote: true,
  dataType: 'script',
  type: 'get',
});

For this we need this dataType script to execute js files, but in the request.js docs there are only 3 options for responseKind (html, turbo-stream, json). A new script option would be interesting

Could you maybe return a turbo-stream that contains a script tag that will be executed once the stream is inserted into the DOM? I know Turbo supports this hotwired/turbo#192

I'm just not a huge fan of executing JS returned by the server this way, so I'm a bit hesitant about adding support for it hotwired/turbo#186 (comment)

@marcelolx Maybe is a good replacement, what are the advantages and disadvantages for each options?