How to run code when fastboot is done rendering?


#1

I think I need the opposite of an initializer…

When fastboot is close to done rendering I want to run some code that inspects the Ember Data store and tells me which models are in there.

I’m wondering if there’s some API I can hook for this? Is ApplicationInstance#willDestroy the best place?


#2

Hmm, I think I would go at it a bit differently. Instead of doing work during destroy, I would probably implement a custom visit and do the work after calling _super.

// app/app.js
export default Application.extend({
  // ...snip...
  visit() {
    return this._super(...arguments).finally(results => {
      // visit has finished, results is the final HTML to be rendered by fastboot
    });
  }
});

#3

Also, it sounds like maybe you are looking for something similar to the Shoebox API’s? There is fairly deep integration (between ember-cli-fastboot and fastboot itself) that enables this to work well…


#4

Yup, similar to shoebox, but I don’t want to serialize it to the HTML page.

I think overriding visit is a cool idea. I tried it, but visit()from app/app.js doesn’t seem to be getting called.

I’ll keep playing with it, I like this approach.


#5

Oh, I think I made a mistake in my snippet, I think visit is a static method on Ember.Application subclasses.

You’d need to tweak to:

const App = Application.extend({ /* normal stuff here */ });

App.reopenClass({
  visit() {
    return this._super(...arguments).finally(results => {
      // visit has finished, results is the final HTML to be rendered by fastboot
    });
  }
});

Good luck, please let us know how it goes…


#6

Ok I think I got something… but I don’t understand why it works :slight_smile:

It only works if I import and reopen ApplicationInstance. It has no effect on Application.

import ApplicationInstance from '@ember/application/instance';

ApplicationInstance.reopen({
  visit() {
    console.log('running application instance reopen visit');
    return this._super(...arguments);
  }
});