async parallel request - partial render

后端 未结 2 1339
暗喜
暗喜 2021-02-10 17:23

What is the proper way to partially render a view following an async parallel request?

Currently I am doing the following

// an example using an object          


        
相关标签:
2条回答
  • 2021-02-10 17:49

    I think you can't do it just on the backend.

    To minimise users' delay you need to send the minimal page to the browser and then to request the rest of the information from the browser via AJAX. Another approach to minimising delays is to send all templates to the browser on the first page load, together with the rendered page, and render all the pages in browser based on the data you request from the server. That's the way I do it. The beauty of nodejs is that you can use the same templating engine both in the backend and frontend and also share the modules.

    If your page is composed in such a way that the slow information is further in HTML than the fast information, you can write response partially without using res.render (that renders complete page) and use res.write instead. I don't think though that this approach deserves serious attention as you would stuck with it sooner than you notice...

    0 讨论(0)
  • 2021-02-10 17:51

    what you want is facebook's bigpipe: https://www.facebook.com/note.php?note_id=389414033919. fortunately, this is easy with nodejs because streaming is built in. unfortunately, template systems are bad at this because async templates are a pain in the butt. however, this is much better than doing any additional AJAX requests.

    basic idea is you first send a layout:

    res.render('layout.ejs', function (err, html) {
      if (err) return next(err)
    
      res.setHeader('Content-Type', 'text/html; charset=utf-8')
      res.write(html.replace('</body></html>', ''))
    
      // Ends the response. 
      // `writePartials` should not return anything in the callback!
      writePartials(res.end.bind(res, '</body></html>'))
    })
    

    you can't send </body></html> because your document isn't finished. then writePartials would be a bunch of async functions (partials or pagelets) executed in parallel.

    function writePartials(callback) {
      async.parallel([partial1, partial2, partial3], callback)
    })
    

    Note: since you've already written a response, there's not much you can do with errors except log them.

    What each partial will do is send inline javascript to the client. For example, the layout can have .stream, and the pagelet will replace .stream's innerHTML upon arrival, or when "the callback finishes".

    function partialStream(callback) {
      res.render('stream.partial.ejs', function (err, html) {
        // Don't return the error in the callback
        // You may want to display an error message or something instead
        if (err) {
          console.error(err.stack)
          callback()
          return
        }
    
        res.write('<script>document.querySelector(".stream").innerHTML = ' + 
          JSON.stringify(html) + ';</script>')
        callback()
      })
    })
    

    Personally, I have .stream.placeholder and replace it with a new .stream element. The reason is I basically do .placeholder, .placeholder ~ * {display: none} so things don't jump around the page. However, this requires a DIY front-end framework since suddenly the JS gets more complciated.

    There, your response is now streaming. Only requirement is that the client supports Javascript.

    0 讨论(0)
提交回复
热议问题