request.js icon indicating copy to clipboard operation
request.js copied to clipboard

Support for responseKind js/script

Open PedroAugustoRamalhoDuarte opened this issue 2 years ago • 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 https://github.com/hotwired/turbo/pull/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 https://github.com/hotwired/turbo/issues/186#issuecomment-959814926

marcelolx avatar Nov 12 '22 11:11 marcelolx

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

while moving to turbo-stream may be the ultimate destination, i second the addition of JS support at the mean time because old applications, like the one that i support, still runs on the old JS partials architecture. having the JS support allows me to keep the old code and make the migration more easily.

shufeilei avatar Apr 02 '24 21:04 shufeilei

If someone is willing to put a PR together, I am happy to review and consider it (I don't have the bandwidth to work on it right now)

marcelolx avatar Apr 03 '24 12:04 marcelolx

I opened a PR https://github.com/rails/request.js/pull/72/files

songjiz avatar Apr 08 '24 16:04 songjiz