Skip to content

Commit

Permalink
Remove lazy log, update README
Browse files Browse the repository at this point in the history
  • Loading branch information
wille committed Sep 13, 2024
1 parent ab3ac5d commit b84008e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ This plugin will significantly speed up your server rendered Vite application by

This plugin is different to [vite-plugin-preload](https://www.npmjs.com/package/vite-plugin-preload) because it evaluates used modules at render time rather than including every single module in the HTML at build time.

Includes functionality similar to [loadable-components](https://loadable-components.com/) where you can create `<link rel=preload>` tags and `Link: </s.js>; rel=preloadmodule;` headers using `getLinkTags()` and `getLinkHeaders()`
Includes functionality similar to [loadable-components](https://loadable-components.com/) where you can create `<link>` tags and `Link` headers for the rendered chunks.

#### See [./playground](./playground/) for a basic setup with preloading
#### See [`./playground`](./playground/) for a basic setup with preloading

## Explainer

Expand Down Expand Up @@ -72,7 +72,8 @@ async function handler(req, res) {
const template = process.env.NODE_ENV === 'production' ? await fs.readFile('./dist/client/index.html', 'utf8') : undefined;
const [head, tail] = template.split('<!-- app-root -->')

// Write a HTTP 103 Early Hint way before React even started rendering with the entry chunks which we know we will be needed by the client
// Write a HTTP 103 Early Hint way before React even started rendering with the entry chunks which we know we will be needed by the client.
// Chrome will only pick it up when running with HTTP/2 so try Firefox if you want to test it.
res.writeEarlyHints({
link: collector.getLinkHeaders()
})
Expand Down
5 changes: 4 additions & 1 deletion src/lazy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@ export function lazy<T extends ComponentType<any>>(
export async function preloadAll() {
// Preload all lazy components up to a depth of 3
for (let i = 0; i < 3 && preloads.length > 0; i++) {
console.log('Preloading', preloads.length, 'lazy components');
const _preloads = preloads;
preloads = [];

if (_preloads.length === 0) {
return;
}
await Promise.all(_preloads.map((preload) => preload()));
}
}

0 comments on commit b84008e

Please sign in to comment.