Skip to content

Commit 7f0f1b4

Browse files
Load from dist instead of using http
1 parent 04201f1 commit 7f0f1b4

3 files changed

Lines changed: 9 additions & 55 deletions

File tree

addon/services/docs-store.js

Lines changed: 7 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -58,54 +58,13 @@ export default class DocsStoreService extends Service {
5858

5959
let fastboot = getOwner(this).lookup('service:fastboot');
6060
if (fastboot?.isFastBoot) {
61-
// In FastBoot, use Node's http module to fetch from the local server
62-
// that prember/fastboot is running
63-
let http = FastBoot.require('http');
64-
let request = fastboot.request;
65-
// Derive host and protocol from the FastBoot/Node request in a standards-based way
66-
let host =
67-
(request &&
68-
request.headers &&
69-
(request.headers.host || request.headers.Host)) ||
70-
request.host;
71-
let protocol =
72-
(request && request.protocol) ||
73-
(request &&
74-
request.headers &&
75-
(request.headers['x-forwarded-proto'] ||
76-
request.headers['X-Forwarded-Proto'])) ||
77-
'http';
78-
let url = `${protocol}://${host}/docs/${id}.json`;
79-
80-
let data = await new Promise((resolve, reject) => {
81-
let req = http.get(url, (res) => {
82-
res.setEncoding('utf8');
83-
let body = '';
84-
res.on('data', (chunk) => {
85-
body += chunk;
86-
});
87-
res.on('end', () => {
88-
let statusCode = res.statusCode || 0;
89-
if (statusCode >= 200 && statusCode < 300) {
90-
resolve(body);
91-
} else {
92-
reject(
93-
new Error(`Request to ${url} failed with status ${statusCode}`),
94-
);
95-
}
96-
});
97-
res.on('error', reject);
98-
});
99-
100-
req.on('error', reject);
101-
// Basic timeout so prember/fastboot failures don't hang indefinitely
102-
req.setTimeout(10000, () => {
103-
req.abort();
104-
reject(new Error(`Request to ${url} timed out`));
105-
});
106-
});
107-
108-
payload = JSON.parse(data);
61+
// In FastBoot, read the docs JSON directly from the dist directory
62+
// using FastBoot.distPath. This works during both prember builds and
63+
// ember serve with fastboot, without needing an HTTP request.
64+
let fs = FastBoot.require('fs');
65+
let path = FastBoot.require('path');
66+
let filePath = path.join(FastBoot.distPath, 'docs', `${id}.json`);
67+
payload = JSON.parse(fs.readFileSync(filePath, 'utf8'));
10968
} else {
11069
let namespace = `${getRootURL(this).replace(/\/$/, '')}/docs`;
11170
let url = `${namespace}/${id}.json`;

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"ember-cli-deploy-plugin"
88
],
99
"fastbootDependencies": [
10-
"http"
10+
"fs",
11+
"path"
1112
],
1213
"repository": "https://github.com/ember-learn/ember-cli-addon-docs",
1314
"license": "MIT",

tests/dummy/config/environment.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,5 @@ module.exports = function (environment) {
5151
// plugin rewrites it to the versioned path at deploy time.
5252
}
5353

54-
ENV.fastboot = {
55-
// Restrict FastBoot host allowlist to known hosts instead of allowing all.
56-
// Add any additional production hostnames here, e.g. 'docs.example.com'.
57-
hostWhitelist: ['localhost', /^localhost:\d+$/, '127.0.0.1', '::1'],
58-
};
59-
6054
return ENV;
6155
};

0 commit comments

Comments
 (0)