-
-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable relative URL in Fastboot mode
define fetch module at instance-initializers to get request info
- Loading branch information
Showing
8 changed files
with
1,193 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import setupFetch from 'fetch/setup'; | ||
|
||
/** | ||
* To allow relative URLs for Fastboot mode, we need the per request information | ||
* from the fastboot service. Then we re-define the `fetch` amd module. | ||
*/ | ||
function patchFetchForRelativeURLs(instance) { | ||
const fastboot = instance.lookup('service:fastboot'); | ||
const request = fastboot.get('request'); | ||
// host is cp | ||
setupFetch(request.protocol, request.get('host'))(); | ||
} | ||
|
||
export default { | ||
name: 'fetch', | ||
initialize: patchFetchForRelativeURLs | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,51 @@ | ||
/* globals define FastBoot */ | ||
(function() { | ||
define('fetch', ['exports'], function(self) { | ||
var AbortControllerPolyfill = FastBoot.require('abortcontroller-polyfill/dist/cjs-ponyfill'); | ||
var nodeFetch = FastBoot.require('node-fetch'); | ||
var abortableFetch = AbortControllerPolyfill.abortableFetch({ | ||
fetch: nodeFetch, | ||
Request: nodeFetch.Request | ||
}); | ||
define('fetch/setup', ['exports'], function(self) { | ||
var httpRegex = /^https?:\/\//; | ||
var protocolRelativeRegex = /^\/\//; | ||
|
||
self['default'] = abortableFetch.fetch; | ||
self['Request'] = abortableFetch.Request; | ||
|
||
self['Headers'] = nodeFetch.Headers; | ||
self['Response'] = nodeFetch.Response; | ||
|
||
self['AbortController'] = AbortControllerPolyfill.AbortController; | ||
var AbortControllerPolyfill = FastBoot.require( | ||
'abortcontroller-polyfill/dist/cjs-ponyfill' | ||
); | ||
var nodeFetch = FastBoot.require('node-fetch'); | ||
var abortableFetch = AbortControllerPolyfill.abortableFetch({ | ||
fetch: nodeFetch, | ||
Request: nodeFetch.Request | ||
}); | ||
|
||
define('fetch/ajax', ['exports'], function() { | ||
throw new Error('You included `fetch/ajax` but it was renamed to `ember-fetch/ajax`'); | ||
}); | ||
})(); | ||
self['default'] = function(protocol, host) { | ||
return function() { | ||
define('fetch', ['exports'], function(exports) { | ||
/** | ||
* Setup the exported fetch for a given origin so it can handle: | ||
* - protocol-relative URL (//can-be-http-or-https.com/) | ||
* - path-relative URL (/file/under/root) | ||
* @param {String} url | ||
* @param {Object} [options] | ||
*/ | ||
exports['default'] = function fetch(url, options) { | ||
if (protocolRelativeRegex.test(url)) { | ||
url = host + url; | ||
} else if (!httpRegex.test(url)) { | ||
if (!host) { | ||
throw new Error( | ||
'You are using using fetch with a path-relative URL, but host is missing from Fastboot request. Please set the hostWhitelist property in your environment.js.' | ||
); | ||
} | ||
url = protocol + '//' + host + url; | ||
} | ||
return abortableFetch.fetch(url, options); | ||
}; | ||
exports['Request'] = abortableFetch.Request; | ||
exports['Headers'] = nodeFetch.Headers; | ||
exports['Response'] = nodeFetch.Response; | ||
exports['AbortController'] = AbortControllerPolyfill.AbortController; | ||
}); | ||
}; | ||
}; | ||
}); | ||
|
||
define('fetch/ajax', ['exports'], function() { | ||
throw new Error( | ||
'You included `fetch/ajax` but it was renamed to `ember-fetch/ajax`' | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.