Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add preload link headers for downstream Cloudflare early hints (assets download before the page is rendered) #171

Conversation

MadLittleMods
Copy link
Contributor

@MadLittleMods MadLittleMods commented Apr 19, 2023

Add preload link headers for downstream Cloudflare early hints.

Because it takes us at best several seconds to request information from a homeserver and then server-side render the page, the browser has to wait for the response before it can even try loading the necessary assets. With this change that facilitates early hints, the browser can preload all of the assets necessary before we are done generating the response and will be ready to go by the time we're all done on the server.

Fix #32

Part of #132

Tracking actually enabling early hints in Cloudflare in the deployment issue: https://github.com/vector-im/sre-internal/issues/2079


To demonstrate the problem, see the waterfall

Before After
I don't have an after visual because we haven't deployed with Cloudflare yet. But it should move that chunk of asset loading after the page loads to before page starts loading so there is no delay in the page being responsive once we have serve a response.

Dev notes

Cloudflare:

@MadLittleMods MadLittleMods added T-Enhancement New feature or request A-performance What is slow, what made it faster and more efficient? A-room-directory Room directory landing page where you can explore the list of rooms A-archive-room-view The view to look at a room day by day in the archive A-backend Related to the backend Node.js server pieces and removed A-room-directory Room directory landing page where you can explore the list of rooms A-archive-room-view The view to look at a room day by day in the archive labels Apr 19, 2023
@MadLittleMods MadLittleMods marked this pull request as ready for review April 19, 2023 19:15
@MadLittleMods MadLittleMods merged commit 17a39ab into main Apr 19, 2023
MadLittleMods added a commit that referenced this pull request Apr 26, 2023
Follow-up to #171 and #175 where they broke because we went from scripts to modules.

Part of #132

Before this PR, we were seeing these warning in the Chrome devtools console:

```
A preload for 'foo' is found, but is not used because the request credentials mode does not match. Consider taking a look at crossorigin attribute.
```

This is caused by a credentials mode mismatch between the `<script type="module">` tag and the `Link` header. A `<script type="module">` with no `crossorigin` attribute indicates a credentials mode of `omit` and a naive `Link: </foo-url>; rel=preload; as=script;` has a  default credentials mode of `same-origin`, hence the mismatch and warning we're seeing.

We could set the credentials mode to match using `Link: </foo-url>; rel=preload; as=script; omit` but there is an even better option! We can use the dedicated `Link: </foo-url>; rel=modulepreload` link type which not only downloads and puts the the file in the cache like a normal preload but the browser also knows it's a JavaScript module now and can parse/compile it so it's ready to go.

---

Future consideration: Adding `nopush` to preload link headers. 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)

---

References for preload `Link` headers:

  - https://medium.com/reloading/preload-prefetch-and-priorities-in-chrome-776165961bbf#6f54
  - https://html.spec.whatwg.org/multipage/links.html#link-type-preload
  - https://www.smashingmagazine.com/2016/02/preload-what-is-it-good-for/#headers
 - https://developer.chrome.com/blog/modulepreload/#ok-so-why-doesnt-link-relpreload-work-for-modules
@MadLittleMods MadLittleMods deleted the madlittlemods/32-add-preload-link-headers-for-cf-early-hints branch April 26, 2023 09:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-backend Related to the backend Node.js server pieces A-performance What is slow, what made it faster and more efficient? T-Enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use HTTP 103 to preload assets while we wait for Matrix requests to respond
1 participant