enhanced icon indicating copy to clipboard operation
enhanced copied to clipboard

Middleware - on error for async load

Open schirrel opened this issue 3 years ago • 2 comments

Like you've said "promise new Promise is handy, but it would be great if one could compose"

We can have async remotes with promise new Promise like:

promise new Promise((resolve) => {
    const script = document.createElement("script");
    script.src = "remote-url";
    script.onload = () => {
      const module = {
        get: (request) => window.appRemote.get(request),
        init: (arg) => {
          try {
            return window.appRemote.init(arg);
          } catch (e) {
            console.log("Problem loading appRemote", E);
          }
        },
      };
      resolve(module);
    };
    script.onerror = () => {
      const module = {
        get: () => () => {},
        init: () => () => {},
      };
      resolve(module);
    };
    document.head.appendChild(script);
  }
)

The script.onerror = () => { there ,help te application to don't throw error when tried to load the remote, with this we can have the app shell to load even when the remote is offline. Usually when returned a 404 this could be a lot of pain.

And when try to load then module itself, like `import myModule from "myRemote/myModule", it returns null, and so we can assume the remote is offline and do validations.

But all these script could be part of the middleware also, what you think?

 remotes: {
    app1: {
      async: true // this perform the whole promise promise new Promise

We can also provide a onError if the dev intent to throw an Exception or handle it any other wayt:

 remotes: {
    app1: {
      async: true // this perform the whole promise promise new Promise
      onError: () => {
        // here i can do whatever i want
        } 
     

Bellow a couple of prints with this approach working with regular development

  • Webpack config (i am using vue-cli but not a big deal) Captura de Tela 2022-07-19 às 22 42 40

  • Using on the Component Captura de Tela 2022-07-19 às 22 42 24

  • The browser Captura de Tela 2022-07-19 às 22 42 49

schirrel avatar Jul 20 '22 01:07 schirrel

So I think we can use this. Also if you use an array of remote values. You can have one be promise new promise and if you reject the promise. Webpack tries the next remote in the array.

ScriptedAlchemy avatar Jul 31 '22 23:07 ScriptedAlchemy

Or we might be able to create new module factories for failures. But let me propose new code losing tools then let's see what we can do about it

ScriptedAlchemy avatar Aug 01 '22 07:08 ScriptedAlchemy