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

Support modulePreload when generating html #1079

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 120 additions & 1 deletion crates/plugin_html/src/resources_injector.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::{collections::HashMap, sync::Arc};
use std::{collections::HashMap, fmt::Display, sync::Arc};
use url::Url;

use farmfe_core::{
config::{Mode, FARM_MODULE_SYSTEM},
Expand All @@ -19,13 +20,38 @@ use crate::utils::{
FARM_ENTRY, FARM_RESOURCE,
};

#[derive(Debug, Clone)]
pub struct PreloadResource {
pub href: String,
pub as_: String,
pub crossorigin: Option<String>,
}

#[derive(Debug, Clone)]
pub struct PrefetchResource {
pub href: String,
pub as_: Option<String>,
pub crossorigin: Option<String>,
}

#[derive(Debug, Clone)]
pub struct DynamicPrefetchResource {
pub module_id: ModuleId,
pub href: String,
pub as_: Option<String>,
pub crossorigin: Option<String>,
}

pub struct ResourcesInjectorOptions {
pub mode: Mode,
pub public_path: String,
pub define: std::collections::HashMap<String, serde_json::Value>,
pub namespace: String,
pub current_html_id: ModuleId,
pub context: Arc<CompilationContext>,
pub preload: Vec<PreloadResource>,
pub prefetch: Vec<PrefetchResource>,
pub dynamic_prefetch: Vec<DynamicPrefetchResource>,
}

/// inject resources into the html ast
Expand Down Expand Up @@ -59,6 +85,93 @@ impl ResourcesInjector {
}
}

fn inject_preload_and_prefetch(&self, element: &mut Element) {
// Inject preload links
for resource in &self.options.preload {
let mut attrs = vec![
("rel", "preload"),
("href", resource.href.as_str()),
("as", resource.as_.as_str()),
];
if let Some(crossorigin) = &resource.crossorigin {
attrs.push(("crossorigin", crossorigin.as_str()));
}
element.children.push(Child::Element(create_element(
"link",
None,
attrs,
)));
}

// Inject prefetch links
for resource in &self.options.prefetch {
let mut attrs = vec![
("rel", "prefetch"),
("href", resource.href.as_str()),
];
if let Some(as_) = &resource.as_ {
attrs.push(("as", as_.as_str()));
}
if let Some(crossorigin) = &resource.crossorigin {
attrs.push(("crossorigin", crossorigin.as_str()));
}
element.children.push(Child::Element(create_element(
"link",
None,
attrs,
)));
}
}

fn inject_dynamic_prefetch(&self, element: &mut Element) {
// Inject dynamic prefetch links
for resource in &self.options.dynamic_prefetch {
let mut attrs = vec![
("rel", "prefetch"),
("href", resource.href.as_str()),
];
if let Some(as_) = &resource.as_ {
attrs.push(("as", as_.as_str()));
}
if let Some(crossorigin) = &resource.crossorigin {
attrs.push(("crossorigin", crossorigin.as_str()));
}
let onload = format!(
r#"
const moduleId = "{}";
const href = this.href;
const as_ = this.as;
const crossorigin = this.crossorigin;
const link = document.createElement("link");
link.rel = "modulepreload";
link.href = href;
link.as = as_;
link.crossorigin = crossorigin;
link.onload = () => {{
const dynamicResources = {}.{}.getDynamicModuleResourcesMap()[moduleId];
for (const [resource, type_] of dynamicResources) {{
if (type_ === "dynamic-import" && !resource.starts_with(href)) {{
const prefetchLink = document.createElement("link");
prefetchLink.rel = "prefetch";
prefetchLink.href = resource;
prefetchLink.as = "fetch";
document.head.appendChild(prefetchLink);
}}
}}
}};
document.head.appendChild(link);
"#,
resource.module_id, self.farm_global_this, FARM_MODULE_SYSTEM
);
attrs.push(("onload", onload.as_str()));
element.children.push(Child::Element(create_element(
"link",
None,
attrs,
)));
}
}

pub fn inject(&mut self, ast: &mut Document) {
ast.visit_mut_with(self);
}
Expand Down Expand Up @@ -147,6 +260,12 @@ impl VisitMut for ResourcesInjector {
}

if element.tag_name.to_string() == "head" {
// Inject preload and prefetch links
self.inject_preload_and_prefetch(element);

// Inject dynamic prefetch links
self.inject_dynamic_prefetch(element);

// inject css <link>
for css in &self.css_resources {
element.children.push(Child::Element(create_element(
Expand Down
Loading