(options: RenderUrlConfig)
| 27 | |
| 28 | // helper method that generates html of an url |
| 29 | export const renderUrl = async (options: RenderUrlConfig): Promise<string> => { |
| 30 | const { |
| 31 | req, |
| 32 | res, |
| 33 | url, |
| 34 | indexHtml, |
| 35 | providers, |
| 36 | commonEngine, |
| 37 | angularAppEngine, |
| 38 | bootstrap, |
| 39 | browserDistFolder, |
| 40 | inlineCriticalCss, |
| 41 | } = options; |
| 42 | |
| 43 | // we need to override url of req with the one we have in parameters, |
| 44 | // because during invalidate process, the url is not from the request |
| 45 | req.url = url; |
| 46 | req.originalUrl = url; |
| 47 | |
| 48 | const { protocol, originalUrl, baseUrl, headers } = req; |
| 49 | const BASE_URL_PROVIDER: Provider = { |
| 50 | provide: APP_BASE_HREF, |
| 51 | useValue: baseUrl, |
| 52 | }; |
| 53 | |
| 54 | return new Promise((resolve, reject) => { |
| 55 | const allProviders = providers |
| 56 | ? [...providers, ...EXTRA_PROVIDERS] // if providers are provided, we add them to the list |
| 57 | : [...EXTRA_PROVIDERS, BASE_URL_PROVIDER]; // if not, we add the default providers |
| 58 | |
| 59 | if (angularAppEngine) { |
| 60 | angularAppEngine |
| 61 | .handle(req) |
| 62 | .then((response) => { |
| 63 | if (response) { |
| 64 | return response.text(); |
| 65 | } |
| 66 | throw new Error('No response from Angular App Engine'); |
| 67 | }) |
| 68 | .then((html) => { |
| 69 | resolve(html); |
| 70 | }) |
| 71 | .catch((err) => { |
| 72 | reject(err); |
| 73 | }); |
| 74 | } else if (commonEngine) { |
| 75 | commonEngine |
| 76 | .render({ |
| 77 | bootstrap, |
| 78 | documentFilePath: indexHtml, |
| 79 | url: `${protocol}://${headers.host}${originalUrl}`, |
| 80 | publicPath: browserDistFolder, |
| 81 | inlineCriticalCss: inlineCriticalCss ?? true, |
| 82 | providers: [...allProviders] as StaticProvider[], // we need to cast to StaticProvider[] because of a bug in the types |
| 83 | }) |
| 84 | .then((html) => { |
| 85 | resolve(html); |
| 86 | }) |
no test coverage detected