-
-
Notifications
You must be signed in to change notification settings - Fork 169
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
Support modulePreload when generating html #1063
Comments
The feature you're asking about, injecting a tag into HTML, is designed to improve the performance of web applications by allowing the browser to preload JavaScript modules before it's actually needed. This can significantly reduce the loading time of the application, especially for large or complex modules. Here's a basic example of what the API might look like: const htmlConfig = { In this example,
In this way, when the browser loads the HTML, it will start downloading the specified module in the background. |
PR welcome!
Further more, |
Is it better to add two new fields in the ResourcesInjectorOptions struct: preload and prefetch?
|
Yes, I think config `html: { preload: boolean, prefetch: boolean } should be added too. // farm.config.ts
export default defineConfig({
compilation: {
html: {
preload: true; // enable preload
prefetch: false; // disable prefetch
}
}
});
|
I think |
pub struct ResourcesInjectorOptions { Here's the updated code for injecting preload and prefetch link tags: if element.tag_name.to_string() == "head" { // inject preload for first-screen resources for preload in &self.options.preload { // inject prefetch for dynamic resources for prefetch in &self.options.prefetch { // inject css ........ |
pub struct ResourcesInjector {
script_resources: Vec<String>,
css_resources: Vec<String>,
script_entries: Vec<String>,
dynamic_resources_map: HashMap<ModuleId, Vec<(String, ResourceType)>>,
// ignore others fields
}
pub preload: Vec, // for preload
pub prefetch: Vec, // for prefetch is not needed. |
I've added two new fields to ResourcesInjectorOptions: preload and prefetch. These are vectors of PreloadResource and PrefetchResource objects, respectively. Each object represents a resource to be preloaded or prefetched, and contains information about the resource's URL, type, and crossorigin setting. I've also added a new method to ResourcesInjector: inject_preload_and_prefetch. This method iterates over the preload and prefetch vectors, and injects the appropriate link tags into the HTML AST. Finally, I've modified the visit_mut_element method to call inject_preload_and_prefetch when processing the element. This ensures that the preload and prefetch link tags are injected into the HTML at the appropriate location. Is this the correct way ? |
But how to generate |
To generate the preload and prefetch vectors, you can iterate over the css_resources and script_resources vectors and create PreloadResource and PrefetchResource objects with the same URLs. Like this:
is this enough? |
Ok, I think it's enough for But prefetch are more complicated, see https://developer.mozilla.org/en-US/docs/Glossary/Prefetch. Resources in
when A is loaded, B should be prefetched(not C), and when B is loaded, C should be prefetched. |
The ResourcesInjectorOptions struct now includes a dynamic_prefetch field that is a vector of DynamicPrefetchResource objects. In the inject_dynamic_prefetch method, we inject a element with rel="prefetch" for each dynamic prefetch resource. We also add an onload event listener to the element that will prefetch the dynamic imports of the corresponding module when the element has finished loading. The onload event listener uses the getDynamicModuleResourcesMap method from the FARM_MODULE_SYSTEM to get the dynamic imports of the corresponding module. In the visit_mut_element method, we call the inject_dynamic_prefetch method in addition to the inject_preload_and_prefetch method when processing the element. I think this might work. |
I create a pull request, I you find it helpful. Please merge it. |
Thank! We are glad to merge all contributions |
Thanks @wre232114 for your cooperation. |
Here is an example HTML file that demonstrates the html-preload feature: <title>Farm HTML Preload Example</title>Hello, world!<script type="module" src="/app.js"></script>And here is the corresponding end-to-end test in Rust: use std::error::Error; #[wasm_bindgen_test]
} I hope this helps! |
Could you add a example like https://github.com/farm-fe/farm/tree/main/examples/env in pr #1079 |
I have already provided the html code and test case above. Please review it To add the example, please create a new file called example.html in the examples directory with the following contents: <title>Farm HTML Preload Example</title>Hello, world!<script type="module" src="/app.js"></script> This example uses the html-preload feature to preload the app.js module.To add the end-to-end test, please create a new file called e2e.rs in the examples directory with the following contents: use std::error::Error; #[wasm_bindgen_test]
} To run the test, please use the following command: wasm-pack test --chrome --headless Sorry, I could not add it by itself. Please add it |
sounds like gpt... |
Yes, sorry I don't how to test it |
thanks, I'll add the test |
Thanks @wre232114 you too. |
What problem does this feature solve?
Inject
modulePreload <link />
tag when generating html.What does the proposed API look like?
Add configuration
html: { modulePreload }
The text was updated successfully, but these errors were encountered: