Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Fastboot sandbox globals to be defined. #25

Closed
wants to merge 3 commits into from

Conversation

Ravenstine
Copy link

I've been working on a simple static blog that uses Prember and PouchDB(ember-pouch). Unfortunately, PouchDB requires the presence of fetch() and Headers in the global scope, making it incompatible with Fastboot out of the box.

I added a globals option to Prember so that global variables can be defined for the Fastboot sandbox. That way, for my purposes, fetch and Headers will be available for PouchDB in Node, thereby allowing my application to receive blog entry JSON from my CouchDB database before rendering the HTML. I imagine this option will be useful to others.

A test is included, but it's looking for the presence of a string in the DOM passed through the globals option. I couldn't think of a better way to test this feature, which is probably due to my laziness in testing with Ember, and would be open to a suggestion on how to better test the globals option.

@ef4
Copy link
Owner

ef4 commented Aug 24, 2018

Thanks for working on this.

But I think it should be possible to solve your problem without needing a feature at this layer.

For example, to polyfill fetch in fastboot (and therefore in Prember):

  1. npm install --save-dev node-fetch

  2. Edit your package.json to put node-fetch in the fastboot whitelist:

     "fastbootDependencies": [
       "node-fetch"
     ]
    
  3. In app.js, use the polyfill only in Fastboot:

    if (typeof FastBoot !== 'undefined') {
      window.fetch = FastBoot.require('node-fetch');
    }
    

@Ravenstine Ravenstine closed this Aug 24, 2018
@Ravenstine Ravenstine reopened this Aug 24, 2018
@Ravenstine
Copy link
Author

@ef4 I thought that was working initially, but it isn't. It looks more like PouchDB(through ember-pouch) is looking for fetch during the Broccoli build, perhaps?

Here's my error summary:

ERROR Summary:

  - broccoliBuilderErrorStack: /Users/Admin/workspace/blog-test/tmp/prember-input_base_path-xyQVjWuu.tmp/assets/vendor.js:81164
var f$1 = fetch;
          ^

ReferenceError: fetch is not defined
    at Object.<anonymous> (/Users/Admin/workspace/blog-test/tmp/prember-input_base_path-xyQVjWuu.tmp/assets/vendor/pouchdb/pouchdb.js:4838:1)
    at Object.13.1 (/Users/Admin/workspace/blog-test/tmp/prember-input_base_path-xyQVjWuu.tmp/assets/vendor/pouchdb/pouchdb.js:12189:1)
    at o (/Users/Admin/workspace/blog-test/tmp/prember-input_base_path-xyQVjWuu.tmp/assets/vendor/pouchdb/pouchdb.js:7:1)
    at r (/Users/Admin/workspace/blog-test/tmp/prember-input_base_path-xyQVjWuu.tmp/assets/vendor/pouchdb/pouchdb.js:7:1)
    at /Users/Admin/workspace/blog-test/tmp/prember-input_base_path-xyQVjWuu.tmp/assets/vendor/pouchdb/pouchdb.js:7:1
    at /Users/Admin/workspace/blog-test/tmp/prember-input_base_path-xyQVjWuu.tmp/assets/vendor/pouchdb/pouchdb.js:7:1
    at /Users/Admin/workspace/blog-test/tmp/prember-input_base_path-xyQVjWuu.tmp/assets/vendor/pouchdb/pouchdb.js:7:1
    at Script.runInContext (vm.js:102:20)
    at VMSandbox.eval (/Users/Admin/workspace/prember/node_modules/fastboot/src/vm-sandbox.js:14:16)
    at /Users/Admin/workspace/prember/node_modules/fastboot/src/ember-app.js:156:15
    at Array.forEach (<anonymous>)
    at EmberApp.loadAppFiles (/Users/Admin/workspace/prember/node_modules/fastboot/src/ember-app.js:153:21)
    at EmberApp.retrieveSandboxedApp (/Users/Admin/workspace/prember/node_modules/fastboot/src/ember-app.js:201:10)
    at new EmberApp (/Users/Admin/workspace/prember/node_modules/fastboot/src/ember-app.js:59:21)
    at FastBoot._buildEmberApp (/Users/Admin/workspace/prember/node_modules/fastboot/src/index.js:114:17)
    at new FastBoot (/Users/Admin/workspace/prember/node_modules/fastboot/src/index.js:52:10)
    at Prerender.build (/Users/Admin/workspace/prember/lib/prerender.js:88:16)
  - codeFrame: fetch is not defined
  - errorMessage: Build Canceled: Broccoli Builder ran into an error with `prember` plugin. 💥
fetch is not defined
  - errorType: Build Error
  - location:
    - column: [undefined]
    - file: [undefined]
    - line: [undefined]
    - treeDir: [undefined]
  - message: Build Canceled: Broccoli Builder ran into an error with `prember` plugin. 💥
fetch is not defined
  - name: Error
  - nodeAnnotation: prember
  - nodeName: prember
  - originalErrorMessage: fetch is not defined
  - stack: /Users/Admin/workspace/blog-test/tmp/prember-input_base_path-xyQVjWuu.tmp/assets/vendor.js:81164
var f$1 = fetch;
          ^

ReferenceError: fetch is not defined
    at Object.<anonymous> (/Users/Admin/workspace/blog-test/tmp/prember-input_base_path-xyQVjWuu.tmp/assets/vendor/pouchdb/pouchdb.js:4838:1)
    at Object.13.1 (/Users/Admin/workspace/blog-test/tmp/prember-input_base_path-xyQVjWuu.tmp/assets/vendor/pouchdb/pouchdb.js:12189:1)
    at o (/Users/Admin/workspace/blog-test/tmp/prember-input_base_path-xyQVjWuu.tmp/assets/vendor/pouchdb/pouchdb.js:7:1)
    at r (/Users/Admin/workspace/blog-test/tmp/prember-input_base_path-xyQVjWuu.tmp/assets/vendor/pouchdb/pouchdb.js:7:1)
    at /Users/Admin/workspace/blog-test/tmp/prember-input_base_path-xyQVjWuu.tmp/assets/vendor/pouchdb/pouchdb.js:7:1
    at /Users/Admin/workspace/blog-test/tmp/prember-input_base_path-xyQVjWuu.tmp/assets/vendor/pouchdb/pouchdb.js:7:1
    at /Users/Admin/workspace/blog-test/tmp/prember-input_base_path-xyQVjWuu.tmp/assets/vendor/pouchdb/pouchdb.js:7:1
    at Script.runInContext (vm.js:102:20)
    at VMSandbox.eval (/Users/Admin/workspace/prember/node_modules/fastboot/src/vm-sandbox.js:14:16)
    at /Users/Admin/workspace/prember/node_modules/fastboot/src/ember-app.js:156:15
    at Array.forEach (<anonymous>)
    at EmberApp.loadAppFiles (/Users/Admin/workspace/prember/node_modules/fastboot/src/ember-app.js:153:21)
    at EmberApp.retrieveSandboxedApp (/Users/Admin/workspace/prember/node_modules/fastboot/src/ember-app.js:201:10)
    at new EmberApp (/Users/Admin/workspace/prember/node_modules/fastboot/src/ember-app.js:59:21)
    at FastBoot._buildEmberApp (/Users/Admin/workspace/prember/node_modules/fastboot/src/index.js:114:17)
    at new FastBoot (/Users/Admin/workspace/prember/node_modules/fastboot/src/index.js:52:10)
    at Prerender.build (/Users/Admin/workspace/prember/lib/prerender.js:88:16)

=================================================================================

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants