1+ /* global FastBoot */
12import Service from '@ember/service' ;
3+ import { getOwner } from '@ember/application' ;
24import { tracked } from '@glimmer/tracking' ;
35import { getRootURL } from 'ember-cli-addon-docs/-private/config' ;
46
@@ -52,11 +54,35 @@ export default class DocsStoreService extends Service {
5254 }
5355
5456 async _fetchProject ( id ) {
55- let namespace = `${ getRootURL ( this ) . replace ( / \/ $ / , '' ) } /docs` ;
56- let url = `${ namespace } /${ id } .json` ;
57-
58- let response = await fetch ( url ) ;
59- let payload = await response . json ( ) ;
57+ let payload ;
58+
59+ let fastboot = getOwner ( this ) . lookup ( 'service:fastboot' ) ;
60+ if ( fastboot ?. isFastBoot ) {
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' ) ) ;
68+ } else {
69+ let namespace = `${ getRootURL ( this ) . replace ( / \/ $ / , '' ) } /docs` ;
70+ let url = `${ namespace } /${ id } .json` ;
71+ let response ;
72+ try {
73+ response = await fetch ( url ) ;
74+ } catch ( e ) {
75+ throw new Error (
76+ `Network error while fetching ${ url } : ${ e && e . message } ` ,
77+ ) ;
78+ }
79+ if ( ! response . ok ) {
80+ throw new Error (
81+ `Request to ${ url } failed with status ${ response . status } ` ,
82+ ) ;
83+ }
84+ payload = await response . json ( ) ;
85+ }
6086
6187 this . _loadPayload ( payload ) ;
6288
@@ -67,7 +93,11 @@ export default class DocsStoreService extends Service {
6793 let allRecords = [ ] ;
6894
6995 // Collect data (can be single or array)
70- let dataItems = Array . isArray ( payload . data ) ? payload . data : [ payload . data ] ;
96+ let dataItems = Array . isArray ( payload . data )
97+ ? payload . data
98+ : payload . data
99+ ? [ payload . data ]
100+ : [ ] ;
71101 allRecords . push ( ...dataItems ) ;
72102
73103 // Collect included
0 commit comments