diff --git a/packages/ember-cli-fastboot/index.js b/packages/ember-cli-fastboot/index.js index 10ee7b75b..fb1bbdd71 100644 --- a/packages/ember-cli-fastboot/index.js +++ b/packages/ember-cli-fastboot/index.js @@ -336,8 +336,13 @@ module.exports = { this.fastboot = new FastBoot(fastbootOptions); } + let appConfig = this._getHostAppConfig(); + let fastbootMiddleware = FastBootExpressMiddleware({ - fastboot: this.fastboot + fastboot: this.fastboot, + visitOptions: { + renderMode: appConfig.fastboot.renderMode + } }); fastbootMiddleware(req, resp, next); diff --git a/packages/ember-cli-fastboot/lib/broccoli/fastboot-config.js b/packages/ember-cli-fastboot/lib/broccoli/fastboot-config.js index 0f7e5b659..099d6aadc 100644 --- a/packages/ember-cli-fastboot/lib/broccoli/fastboot-config.js +++ b/packages/ember-cli-fastboot/lib/broccoli/fastboot-config.js @@ -32,6 +32,10 @@ module.exports = class FastBootConfig extends Plugin { this.fastbootConfig[appConfigModule] = options.appConfig; this._fileToChecksumMap = {}; + if (options.appConfig.fastboot) { + this.renderMode = options.appConfig.fastboot.renderMode; + } + if (this.fastbootAppConfig && this.fastbootAppConfig.htmlFile) { this.htmlFile = this.fastbootAppConfig.htmlFile; } else { @@ -174,6 +178,7 @@ module.exports = class FastBootConfig extends Plugin { schemaVersion: LATEST_SCHEMA_VERSION, manifest: this.manifest, hostWhitelist: this.normalizeHostWhitelist(), + renderMode: this.renderMode, config: this.fastbootConfig, appName: this.appName, } diff --git a/packages/ember-cli-fastboot/test/fastboot-rendermode-config-test.js b/packages/ember-cli-fastboot/test/fastboot-rendermode-config-test.js new file mode 100644 index 000000000..8574c778e --- /dev/null +++ b/packages/ember-cli-fastboot/test/fastboot-rendermode-config-test.js @@ -0,0 +1,42 @@ +'use strict'; + +const expect = require('chai').use(require('chai-string')).expect; +const RSVP = require('rsvp'); +const request = RSVP.denodeify(require('request')); + +const AddonTestApp = require('ember-cli-addon-tests').AddonTestApp; + +describe('FastBoot renderMode config', function() { + this.timeout(400000); + + let app; + + before(function() { + app = new AddonTestApp(); + + return app.create('fastboot-rendermode-config', { emberVersion: 'latest'}) + .then(function() { + return app.startServer({ + command: 'serve' + }); + }); + }); + + after(function() { + return app.stopServer(); + }); + + it('uses rehydration when rendermode is serialize', function() { + return request({ + url: 'http://localhost:49741/dynamic', + headers: { + 'Accept': 'text/html' + } + }) + .then(function(response) { + expect(response.statusCode).to.equal(200); + expect(response.headers['content-type']).to.equalIgnoreCase('text/html; charset=utf-8'); + expect(response.body).to.contain('magic'); + }); + }); +}); diff --git a/packages/ember-cli-fastboot/test/fixtures/fastboot-rendermode-config/app/router.js b/packages/ember-cli-fastboot/test/fixtures/fastboot-rendermode-config/app/router.js new file mode 100644 index 000000000..119742add --- /dev/null +++ b/packages/ember-cli-fastboot/test/fixtures/fastboot-rendermode-config/app/router.js @@ -0,0 +1,9 @@ +import Ember from 'ember'; + +let Router = Ember.Router; + +Router.map(function() { + this.route('dynamic'); +}); + +export default Router; diff --git a/packages/ember-cli-fastboot/test/fixtures/fastboot-rendermode-config/app/routes/dynamic.js b/packages/ember-cli-fastboot/test/fixtures/fastboot-rendermode-config/app/routes/dynamic.js new file mode 100644 index 000000000..62166fb1f --- /dev/null +++ b/packages/ember-cli-fastboot/test/fixtures/fastboot-rendermode-config/app/routes/dynamic.js @@ -0,0 +1,7 @@ +import Ember from 'ember'; + +export default Ember.Route.extend({ + model() { + return 'magic'; + } +}); diff --git a/packages/ember-cli-fastboot/test/fixtures/fastboot-rendermode-config/app/templates/dynamic.hbs b/packages/ember-cli-fastboot/test/fixtures/fastboot-rendermode-config/app/templates/dynamic.hbs new file mode 100644 index 000000000..cd4d5d026 --- /dev/null +++ b/packages/ember-cli-fastboot/test/fixtures/fastboot-rendermode-config/app/templates/dynamic.hbs @@ -0,0 +1,3 @@ +

This should be a dynamic page!

+ +

And the most dynamic word is: {{@model}}

\ No newline at end of file diff --git a/packages/ember-cli-fastboot/test/fixtures/fastboot-rendermode-config/config/environment.js b/packages/ember-cli-fastboot/test/fixtures/fastboot-rendermode-config/config/environment.js new file mode 100644 index 000000000..af2e2491a --- /dev/null +++ b/packages/ember-cli-fastboot/test/fixtures/fastboot-rendermode-config/config/environment.js @@ -0,0 +1,16 @@ +'use strict'; + +module.exports = function(environment) { + var ENV = { + rootURL: '/', + locationType: 'auto', + environment: environment, + modulePrefix: 'fastboot-rendermode-config', + fastboot: { + hostWhitelist: [/localhost:\d+/], + renderMode: 'serialize', + } + }; + + return ENV; +}; diff --git a/packages/fastboot/src/ember-app.js b/packages/fastboot/src/ember-app.js index 3299a229a..2716c9886 100644 --- a/packages/fastboot/src/ember-app.js +++ b/packages/fastboot/src/ember-app.js @@ -40,6 +40,7 @@ class EmberApp { this.appName = config.appName; this.html = config.html; this.sandboxRequire = config.sandboxRequire; + this.renderMode = config.renderMode; if (process.env.APP_CONFIG) { let appConfig = JSON.parse(process.env.APP_CONFIG); @@ -298,7 +299,10 @@ class EmberApp { let buildSandboxPerVisit = options.buildSandboxPerVisit || false; let shouldRender = options.shouldRender !== undefined ? options.shouldRender : true; - let bootOptions = buildBootOptions(shouldRender); + let renderMode = process.env.EXPERIMENTAL_RENDER_MODE_SERIALIZE + ? 'serialize' + : options.renderMode || this.renderMode; + let bootOptions = buildBootOptions(shouldRender, renderMode); let fastbootInfo = new FastBootInfo(req, res, { hostWhitelist: this.hostWhitelist, metadata: options.metadata, @@ -355,17 +359,16 @@ class EmberApp { * Builds an object with the options required to boot an ApplicationInstance in * FastBoot mode. */ -function buildBootOptions(shouldRender) { +function buildBootOptions(shouldRender, renderMode) { let doc = new SimpleDOM.Document(); let rootElement = doc.body; - let _renderMode = process.env.EXPERIMENTAL_RENDER_MODE_SERIALIZE ? 'serialize' : undefined; return { isBrowser: false, document: doc, rootElement, shouldRender, - _renderMode, + _renderMode: renderMode, }; } diff --git a/packages/fastboot/src/fastboot-schema.js b/packages/fastboot/src/fastboot-schema.js index 91fcede9c..bff19d809 100644 --- a/packages/fastboot/src/fastboot-schema.js +++ b/packages/fastboot/src/fastboot-schema.js @@ -92,6 +92,7 @@ function loadConfig(distPath) { scripts, html, hostWhitelist: pkg.fastboot.hostWhitelist, + renderMode: pkg.fastboot.renderMode, config, appName, sandboxRequire,