Skip to content

Commit f99fff2

Browse files
authored
feat(beasties): add new swap-low preload strategy (#72)
1 parent 67281f2 commit f99fff2

File tree

6 files changed

+90
-3
lines changed

6 files changed

+90
-3
lines changed

packages/beasties/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,11 +240,12 @@ Note: <kbd>JS</kbd> indicates a strategy requiring JavaScript (falls back to `<n
240240
- **"media":** Load stylesheets asynchronously by adding `media="not x"` and removing once loaded. <kbd>JS</kbd>
241241
- **"swap":** Convert stylesheet links to preloads that swap to `rel="stylesheet"` once loaded ([details](https://www.filamentgroup.com/lab/load-css-simpler/#the-code)). <kbd>JS</kbd>
242242
- **"swap-high":** Use `<link rel="alternate stylesheet preload">` and swap to `rel="stylesheet"` once loaded ([details](http://filamentgroup.github.io/loadCSS/test/new-high.html)). <kbd>JS</kbd>
243+
- **"swap-low":** Use `<link rel="alternate stylesheet">` (no `preload` in `rel` here!) and swap to `rel="stylesheet"` once loaded ([details](http://filamentgroup.github.io/loadCSS/test/new-low.html)). It ensures lowest priority compared to `swap` and `swap-high`. <kbd>JS</kbd>
243244
- **"js":** Inject an asynchronous CSS loader similar to [LoadCSS](https://github.com/filamentgroup/loadCSS) and use it to load stylesheets. <kbd>JS</kbd>
244245
- **"js-lazy":** Like `"js"`, but the stylesheet is disabled until fully loaded.
245246
- **false:** Disables adding preload tags.
246247

247-
Type: (default | `"body"` | `"media"` | `"swap"` | `"swap-high"` | `"js"` | `"js-lazy"`)
248+
Type: (default | `"body"` | `"media"` | `"swap"` | `"swap-high"` | `"swap-low"` | `"js"` | `"js-lazy"`)
248249

249250
## Similar Libraries
250251

packages/beasties/src/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export interface Options {
4747
pruneSource?: boolean
4848
mergeStylesheets?: boolean
4949
additionalStylesheets?: string[]
50-
preload?: 'body' | 'media' | 'swap' | 'swap-high' | 'js' | 'js-lazy'
50+
preload?: 'body' | 'media' | 'swap' | 'swap-high' | 'swap-low' | 'js' | 'js-lazy'
5151
noscriptFallback?: boolean
5252
inlineFonts?: boolean
5353
preloadFonts?: boolean

packages/beasties/src/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,13 @@ export default class Beasties {
310310
link.setAttribute('onload', `this.title='';this.rel='stylesheet'`)
311311
noscriptFallback = true
312312
}
313+
else if (preloadMode === 'swap-low') {
314+
// @see http://filamentgroup.github.io/loadCSS/test/new-low.html
315+
link.setAttribute('rel', 'alternate stylesheet')
316+
link.setAttribute('title', 'styles')
317+
link.setAttribute('onload', `this.title='';this.rel='stylesheet'`)
318+
noscriptFallback = true
319+
}
313320
else if (preloadMode === 'swap') {
314321
link.setAttribute('onload', 'this.rel=\'stylesheet\'')
315322
updateLinkToPreload = true

packages/beasties/src/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ import type { Logger, LogLevel } from './util'
1010
* - **"media":** Load stylesheets asynchronously by adding `media="not x"` and removing once loaded. <kbd>JS</kbd>
1111
* - **"swap":** Convert stylesheet links to preloads that swap to `rel="stylesheet"` once loaded ([details](https://www.filamentgroup.com/lab/load-css-simpler/#the-code)). <kbd>JS</kbd>
1212
* - **"swap-high":** Use `<link rel="alternate stylesheet preload">` and swap to `rel="stylesheet"` once loaded ([details](http://filamentgroup.github.io/loadCSS/test/new-high.html)). <kbd>JS</kbd>
13+
* - **"swap-low":** Use `<link rel="alternate stylesheet">` (no `preload` in `rel` here!) and swap to `rel="stylesheet"` once loaded ([details](http://filamentgroup.github.io/loadCSS/test/new-low.html)). It ensures lowest priority compared to `swap` and `swap-high`. <kbd>JS</kbd>
1314
* - **"js":** Inject an asynchronous CSS loader similar to [LoadCSS](https://github.com/filamentgroup/loadCSS) and use it to load stylesheets. <kbd>JS</kbd>
1415
* - **"js-lazy":** Like `"js"`, but the stylesheet is disabled until fully loaded.
1516
* - **false:** Disables adding preload tags.
1617
*/
17-
type PreloadStrategy = 'body' | 'media' | 'swap' | 'swap-high' | 'js' | 'js-lazy'
18+
type PreloadStrategy = 'body' | 'media' | 'swap' | 'swap-high' | 'swap-low' | 'js' | 'js-lazy'
1819

1920
/**
2021
* Controls which keyframes rules are inlined.

packages/beasties/test/__snapshots__/beasties.test.ts.snap

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,3 +188,29 @@ exports[`preload modes > should use "swap" preload mode correctly 1`] = `
188188
</html>
189189
"
190190
`;
191+
192+
exports[`preload modes > should use "swap-high" preload mode correctly 1`] = `
193+
"
194+
<html data-beasties-container>
195+
<head>
196+
<style>h1{color:blue}</style><link rel="alternate stylesheet preload" href="/style.css" title="styles" onload="this.title='';this.rel='stylesheet'"><noscript><link rel="stylesheet" href="/style.css"></noscript>
197+
</head>
198+
<body>
199+
<h1>Hello World!</h1>
200+
</body>
201+
</html>
202+
"
203+
`;
204+
205+
exports[`preload modes > should use "swap-low" preload mode correctly 1`] = `
206+
"
207+
<html data-beasties-container>
208+
<head>
209+
<style>h1{color:blue}</style><link rel="alternate stylesheet" href="/style.css" title="styles" onload="this.title='';this.rel='stylesheet'"><noscript><link rel="stylesheet" href="/style.css"></noscript>
210+
</head>
211+
<body>
212+
<h1>Hello World!</h1>
213+
</body>
214+
</html>
215+
"
216+
`;

packages/beasties/test/beasties.test.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,4 +405,56 @@ describe('preload modes', () => {
405405
expect(result).not.toContain('<noscript>')
406406
expect(result).toMatchSnapshot()
407407
})
408+
409+
it('should use "swap-low" preload mode correctly', async () => {
410+
const beasties = new Beasties({
411+
reduceInlineStyles: false,
412+
path: '/',
413+
preload: 'swap-low',
414+
})
415+
const assets: Record<string, string> = {
416+
'/style.css': 'h1 { color: blue; }',
417+
}
418+
beasties.readFile = filename => assets[filename.replace(/^\w:/, '').replace(/\\/g, '/')]!
419+
const result = await beasties.process(`
420+
<html>
421+
<head>
422+
<link rel="stylesheet" href="/style.css">
423+
</head>
424+
<body>
425+
<h1>Hello World!</h1>
426+
</body>
427+
</html>
428+
`)
429+
expect(result).toContain('<style>h1{color:blue}</style>')
430+
expect(result).toContain(`<link rel="alternate stylesheet" href="/style.css" title="styles" onload="this.title='';this.rel='stylesheet'">`)
431+
expect(result).toContain('<noscript><link rel="stylesheet" href="/style.css"></noscript>')
432+
expect(result).toMatchSnapshot()
433+
})
434+
435+
it('should use "swap-high" preload mode correctly', async () => {
436+
const beasties = new Beasties({
437+
reduceInlineStyles: false,
438+
path: '/',
439+
preload: 'swap-high',
440+
})
441+
const assets: Record<string, string> = {
442+
'/style.css': 'h1 { color: blue; }',
443+
}
444+
beasties.readFile = filename => assets[filename.replace(/^\w:/, '').replace(/\\/g, '/')]!
445+
const result = await beasties.process(`
446+
<html>
447+
<head>
448+
<link rel="stylesheet" href="/style.css">
449+
</head>
450+
<body>
451+
<h1>Hello World!</h1>
452+
</body>
453+
</html>
454+
`)
455+
expect(result).toContain('<style>h1{color:blue}</style>')
456+
expect(result).toContain(`<link rel="alternate stylesheet preload" href="/style.css" title="styles" onload="this.title='';this.rel='stylesheet'">`)
457+
expect(result).toContain('<noscript><link rel="stylesheet" href="/style.css"></noscript>')
458+
expect(result).toMatchSnapshot()
459+
})
408460
})

0 commit comments

Comments
 (0)