Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ember Fastboot Jquery

I just added ember-cli-fastboot (github) on my project in order to have a pre-render server for crawlers bots.

After following installation steps, I get an error message when starting the server (ember fastboot --serve-assets):

jQuery is not defined
ReferenceError: jQuery is not defined
at <anonymous>:66304:16 [...]

Is there something in the cache to clean? Am I missing something obvious?

like image 492
Nicorr Avatar asked Dec 08 '15 17:12

Nicorr


People also ask

Does Ember use jQuery?

Ember has been historically coupled to jQuery. As part of RFC294, jQuery has been made optional. This addon makes jQuery available in an Ember project. It also provides the mechanism that implements jQuery integration when that feature is enabled.

What is FastBoot ember?

FastBoot allows you to render and serve Ember. js apps on the server. Using FastBoot, you can serve rendered HTML to browsers and other clients without requiring them to download JavaScript assets.

Is Ember JS frontend or backend?

AngularJS and Ember. js are designed to be backend agnostic. It's a good rule to keep the back-end independent since the front-end is seeing a lot more growth. This decision will save you some trouble in the future when you want to modify your front-end a year or two down the line.


1 Answers

FastBoot renders in Node, specifically in a V8 virtual machine, which means it does not have access to the DOM, which is required for running jQuery. Also, being a sandboxed vm, it doesn’t have access to the same global namespace that the FastBoot code is running in (unless you explicitly pass it into the sandbox, see: "Using Whitelisted Node Dependencies" on https://www.ember-fastboot.com/docs/user-guide).

If you were to pass jQuery, via the jQuery node module, and expose in your app code, you still wouldn’t be able to run much of jQuery’s API in FastBoot mode, because it depends heavily on DOM APIs that simply don’t exist in this environment.

But some times you need to do some DOM manipulation within the browser sometimes, to handle complex animations or user interactions. The recommended solution for this is to keep all of your code that requires DOM manipulation in the didInsertElement or willInsertElement component lifecycle event handlers, which explicitly do not run in FastBoot mode.

Another option, which is not recommended, is to wrap calls to jQuery that are erring out with a check to make sure you’re not in FastBoot (see: "The FastBoot Service" on the user guide above)

You do not need to whitelist the module in this case, as FastBoot should never execute the code dependent on jQuery.

A side note: Ember clearly renders HTML, and it needs some way to construct a DOM-like structure. The FastBoot authors created SimpleDOM which is passed to the rendering engine at Application Instance init time. It is a very small subset of DOM methods used to construct the hierarchy, and then serialize to an HTML string.

like image 142
Kyle Olson Avatar answered Nov 13 '22 19:11

Kyle Olson