( fetchMetric: FetchMetric, loggingConfig: LoggingConfig | undefined )
| 146 | } |
| 147 | |
| 148 | function logFetchMetric( |
| 149 | fetchMetric: FetchMetric, |
| 150 | loggingConfig: LoggingConfig | undefined |
| 151 | ): void { |
| 152 | let { |
| 153 | cacheReason, |
| 154 | cacheStatus, |
| 155 | cacheWarning, |
| 156 | end, |
| 157 | method, |
| 158 | start, |
| 159 | status, |
| 160 | url, |
| 161 | } = fetchMetric |
| 162 | |
| 163 | if (cacheStatus === 'hmr' && !loggingConfig?.fetches?.hmrRefreshes) { |
| 164 | // Cache hits during HMR refreshes are intentionally not logged, unless |
| 165 | // explicitly enabled in the logging config. |
| 166 | return |
| 167 | } |
| 168 | |
| 169 | if (loggingConfig?.fetches) { |
| 170 | if (url.length > 48 && !loggingConfig.fetches.fullUrl) { |
| 171 | url = truncateUrl(url) |
| 172 | } |
| 173 | |
| 174 | writeLine( |
| 175 | white( |
| 176 | `${method} ${url} ${status} in ${Math.round(end - start)}ms ${formatCacheStatus(cacheStatus)}` |
| 177 | ), |
| 178 | 1 |
| 179 | ) |
| 180 | |
| 181 | if (cacheStatus === 'skip' || cacheStatus === 'miss') { |
| 182 | writeLine( |
| 183 | gray( |
| 184 | `Cache ${cacheStatus === 'skip' ? 'skipped' : 'missed'} reason: (${white(cacheReason)})` |
| 185 | ), |
| 186 | 2 |
| 187 | ) |
| 188 | } |
| 189 | } else if (cacheWarning) { |
| 190 | // When logging for fetches is not enabled, we still want to print any |
| 191 | // associated warnings, so we print the request first to provide context. |
| 192 | writeLine(white(`${method} ${url}`), 1) |
| 193 | } |
| 194 | |
| 195 | if (cacheWarning) { |
| 196 | writeLine(`${yellow(bold('⚠'))} ${white(cacheWarning)}`, 2) |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | function writeLine(text: string, indentationLevel = 0): void { |
| 201 | process.stdout.write(` ${'│ '.repeat(indentationLevel)}${text}\n`) |
no test coverage detected