|
1 | 1 | import { defineNitroPlugin, useAppConfig } from 'nitropack/runtime' |
2 | 2 | import { parseUserAgent } from 'detect-browser-es' |
3 | | -import type { HttpClientHintsState, ResolvedHttpClientHintsOptions } from '../shared-types/types' |
| 3 | +import type { |
| 4 | + HttpClientHintsState, |
| 5 | + ResolvedHttpClientHintsOptions, |
| 6 | + ServerHttpClientHintsOptions, |
| 7 | +} from '../shared-types/types' |
4 | 8 | import { extractBrowser } from '../utils/detect' |
5 | 9 | import { extractCriticalHints } from '../utils/critical' |
6 | 10 | import { extractDeviceHints } from '../utils/device' |
7 | 11 | import { extractNetworkHints } from '../utils/network' |
8 | 12 |
|
9 | | -interface ServerRuntimeConfig extends Omit<ResolvedHttpClientHintsOptions, 'serverImages'> { |
10 | | - serverImages: string[] |
11 | | -} |
12 | | - |
13 | 13 | export default defineNitroPlugin((nitroApp) => { |
14 | | - const { serverImages, ...rest } = useAppConfig().httpClientHints as ServerRuntimeConfig |
| 14 | + const { serverImages, ...rest } = useAppConfig().httpClientHints as ServerHttpClientHintsOptions |
15 | 15 | const options: ResolvedHttpClientHintsOptions = { |
16 | 16 | ...rest, |
17 | 17 | serverImages: serverImages.map(r => new RegExp(r)), |
18 | 18 | } |
19 | | - console.log(options) |
20 | | - nitroApp.hooks.hook('afterResponse', async (event) => { |
21 | | - // we should add the vary header to the response |
| 19 | + nitroApp.hooks.hook('afterResponse', async (_event) => { |
| 20 | + // we should add the Vary header to the response: is there a way to check if the response has been committed? |
22 | 21 | }) |
23 | 22 | nitroApp.hooks.hook('request', async (event) => { |
24 | 23 | // expose the client hints in the context |
@@ -48,15 +47,15 @@ export default defineNitroPlugin((nitroApp) => { |
48 | 47 | if (detect) { |
49 | 48 | clientHints.browser = await extractBrowser(options, requestHeaders as Record<string, string>, userAgentHeader ?? undefined) |
50 | 49 | } |
51 | | - if (critical) { |
52 | | - clientHints.critical = extractCriticalHints(options, requestHeaders, userAgent) |
53 | | - } |
54 | 50 | if (device) { |
55 | 51 | clientHints.device = extractDeviceHints(options, requestHeaders, userAgent) |
56 | 52 | } |
57 | 53 | if (network) { |
58 | 54 | clientHints.network = extractNetworkHints(options, requestHeaders, userAgent) |
59 | 55 | } |
| 56 | + if (critical) { |
| 57 | + clientHints.critical = extractCriticalHints(options, requestHeaders, userAgent) |
| 58 | + } |
60 | 59 | event.context.httpClientHints = clientHints |
61 | 60 | } |
62 | 61 | } |
|
0 commit comments