-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add dynamic LCP priority worker logic
- Loading branch information
1 parent
780ead3
commit a5a02f5
Showing
5 changed files
with
214 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import {strict as assert} from 'assert'; | ||
import fetch from 'node-fetch'; | ||
|
||
const urlWithLCPImage = | ||
'/articles/my-challenge-to-the-web-performance-community/'; | ||
|
||
const urlWithoutLCPImage = '/articles/cascading-cache-invalidation/'; | ||
|
||
describe('worker', function () { | ||
describe('priority hints', function () { | ||
beforeEach(async () => { | ||
// Delete the cache by passing an empty selector. | ||
// TODO: figure out a better way to do this. There doesn't seem to | ||
// currently be a way to clear KV store data locally via wrangler. | ||
for (const url of [urlWithLCPImage, urlWithLCPImage]) { | ||
await fetch(`http://127.0.0.1:3000/hint`, { | ||
method: 'POST', | ||
body: JSON.stringify({ | ||
path: url, | ||
selector: '', | ||
}), | ||
}); | ||
} | ||
}); | ||
|
||
it('should apply priority hints to prior LCP images', async () => { | ||
await browser.url(urlWithLCPImage); | ||
|
||
const fp1 = await browser.execute(() => { | ||
return document.querySelector('img').getAttribute('fetchpriority'); | ||
}); | ||
|
||
// Wait until the hint has been sent. | ||
await browser.executeAsync((done) => { | ||
new PerformanceObserver((list) => { | ||
for (const entry of list.getEntries()) { | ||
if (entry.name.endsWith('/hint')) { | ||
done(); | ||
} | ||
} | ||
}).observe({type: 'resource', buffered: true}); | ||
}); | ||
|
||
await browser.url('/__reset__'); | ||
|
||
// Wait until the SW has unregistered. | ||
await browser.waitUntil(async () => { | ||
return await browser.execute(() => { | ||
return window.__ready__ === true; | ||
}); | ||
}); | ||
|
||
await browser.url(urlWithLCPImage); | ||
|
||
const fp2 = await browser.execute(() => { | ||
return document.querySelector('img').getAttribute('fetchpriority'); | ||
}); | ||
|
||
assert.strictEqual(fp1, null); | ||
assert.strictEqual(fp2, 'high'); | ||
}); | ||
|
||
it('should not apply priority hints to prior non-LCP images', async () => { | ||
await browser.url(urlWithoutLCPImage); | ||
|
||
const fp1 = await browser.execute(() => { | ||
return document.querySelector('img').getAttribute('fetchpriority'); | ||
}); | ||
|
||
// No hint should be sent for this page, but we need to wait a bit | ||
// to ensure that it doesn't happen. | ||
await browser.pause(2000); | ||
|
||
await browser.url('/__reset__'); | ||
|
||
// Wait until the SW has unregistered. | ||
await browser.waitUntil(async () => { | ||
return await browser.execute(() => { | ||
return window.__ready__ === true; | ||
}); | ||
}); | ||
|
||
await browser.url(urlWithoutLCPImage); | ||
|
||
const fp2 = await browser.execute(() => { | ||
return document.querySelector('img').getAttribute('fetchpriority'); | ||
}); | ||
|
||
assert.strictEqual(fp1, null); | ||
assert.strictEqual(fp2, null); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
class PriorityHintsTransform { | ||
#applied = false; | ||
element(element) { | ||
if (!this.#applied) { | ||
element.setAttribute('fetchpriority', 'high'); | ||
this.#applied = true; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @param {string} experiment | ||
* @param {HTMLRewriter} rewriter | ||
*/ | ||
export function addPriorityHints(rewriter, selector) { | ||
rewriter.on(selector, new PriorityHintsTransform()); | ||
} | ||
|
||
/** | ||
* @param {Request} request | ||
* @param {string} path | ||
* @returns {string} | ||
*/ | ||
export function getPriorityHintKey(request, path) { | ||
const device = | ||
request.headers.get('sec-ch-ua-mobile') === '?1' ? 'mobile' : 'desktop'; | ||
|
||
// URL-encode the path because wrangler doesn't handle slashes when | ||
// running locally (it treats them as directory separators). | ||
return `${device}:${encodeURIComponent(path)}`; | ||
} | ||
|
||
/** | ||
* @param {Request} request | ||
* @param {Object} store | ||
*/ | ||
async function storePriorityHints(request, store) { | ||
const {path, selector} = await request.json(); | ||
const key = getPriorityHintKey(request, path); | ||
|
||
const storedSelector = await store.get(key); | ||
if (selector !== storedSelector) { | ||
await store.put(key, selector); | ||
} | ||
} | ||
|
||
/** | ||
* @param {Request} request | ||
* @param {Object} store | ||
* @returns {Response} | ||
*/ | ||
export async function logPriorityHint(request, store) { | ||
await storePriorityHints(request, store); | ||
return new Response(); // Empty 200. | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters