'use strict'; const assert = require('assert'); const getDependenciesForEntryPointName = require('../lib/get-dependencies-for-entry-point-name'); // Set some preload link headers which we can use with Cloudflare to turn into 103 early // hints, https://developers.cloudflare.com/cache/about/early-hints/ // // This will turn into a nice speed-up since the server side render can take some time // while we fetch all the information from the homeserver and the page can have all of // the assets loaded and ready to go by that time. This way there is no extra delay // after the page gets served. function setHeadersToPreloadAssets(res, pageOptions) { assert(res); assert(pageOptions); assert(pageOptions.entryPoint); const { styles, preloadScripts } = getDependenciesForEntryPointName(pageOptions.entryPoint); // Note: Any docs for the `` element apply to the `Link` header. "The `Link` // header contains parameters [that] are equivalent to attributes of the `` // element." // (https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Link#parameters) // XXX: Should we add `nopush` to the `Link` headers here? Many servers initiate an // HTTP/2 Server Push when they encounter a preload link in HTTP header form // otherwise. Do we want/care about that (or maybe we don't)? (mentioned in // https://medium.com/reloading/preload-prefetch-and-priorities-in-chrome-776165961bbf#6f54) const styleLinks = styles.map((styleUrl) => { return `<${styleUrl}>; rel=preload; as=style`; }); // TODO: We should preload fonts as well. // // We use `crossorigin` because fonts are fetched with anonymous mode "cors" and // "same-origin" credentials mode (see // https://drafts.csswg.org/css-fonts/#font-fetching-requirements). `crossorigin` is // just short-hand for `crossorigin=anonymous` (see // https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/crossorigin and // https://html.spec.whatwg.org/multipage/infrastructure.html#cors-settings-attribute). // // `Link: ; rel=preload; as=font; crossorigin` // We use `rel=modulepreload` instead of `rel=preload` for the JavaScript modules // because it's a nice dedicated thing to handle ESM modules that not only downloads // and puts it in the cache like a normal `rel=preload` but the browser also knows // it's a JavaScript module now and can parse/compile it so it's ready to go. // // Also as a note: `