Load JavaScript on Demand to Boost Site Performance
July 24, 2025🧠 What is Dynamic Import?
Dynamic import allows you to load JavaScript modules at runtime—only when needed. This approach is fantastic for reducing initial load time, improving perceived performance, and saving the user's bandwidth.
💡 Example: Load a Chart Module on Click
Let’s say you have a button that shows a chart. Why load the chart logic upfront if the user may never click it?
<button class="btn btn-outline-dark" id="loadChart">Load Chart</button>
<script>
document.getElementById("loadChart").addEventListener("click", async () => {
const module = await import("./charts.js");
const {{ showChart }} = module;
if (typeof showChart === "function") {
showChart(); // Renders the chart
} else {
console.error("showChart function not found in charts.js");
}
});
</script>
Important: Make sure charts.js
exports the function:
// charts.js
export function showChart() {
alert("Chart loaded successfully!");
}
📨 Example: Load Script on Form Submit
There’s no need to load your form-handling logic until a user actually submits a form:
<form id="contactForm">
<input type="text" name="name" placeholder="Your name" required />
<button type="submit">Send</button>
</form>
<script>
document.getElementById("contactForm").addEventListener("submit", async (e) => {
e.preventDefault();
const module = await import("./formHandler.js");
const {{ processForm }} = module;
if (typeof processForm === "function") {
processForm(new FormData(e.target));
} else {
console.error("processForm function not found in formHandler.js");
}
});
</script>
Make sure your module looks like this:
// formHandler.js
export function processForm(formData) {
const name = formData.get("name");
alert("Data received: " + name);
}
⚡ Why It Matters
- ⏱️ Faster initial page load
- 📦 Smaller JavaScript bundles
- 📈 Better performance scores (e.g., Google Lighthouse, Core Web Vitals)
- 🧩 Easier code maintenance and splitting
Blog
Stop Copy-Pasting Code! Learn How to Use Traits in Laravel the Right Way
Jul 01, 2025
🚫 Stop Copy-Pasting Code! Ever duplicated slug logic or logging across multiple models? Laravel's Traits got your back. 1. What’s a Trait?...
Analyze Laravel Projects with Introspect
Jul 01, 2025
Analyze Laravel Codebases with Laravel Introspect If you’re doing a complex refactor or building dev tools, Laravel Introspect helps you quer...
Laravel 12.19: Elegant Query Builders with PHP Attributes
Jul 07, 2025
Laravel 12.19: Elegant Query Builders with PHP Attributes In Laravel 12.19, you can now use the #[UseEloquentBuilder] PHP attribute to assign a cus...
Laravel 12.18.0 Update
Jun 17, 2025
Laravel 12.18.0 Update The Laravel team released version 12.18.0 with several cool updates: String encrypt() & decrypt() helpers are...
Using Web Components the Smart Way
Jul 06, 2025
Using Web Components the Smart Way A lot of developers assume Web Components are meant to replace full SPA frameworks like React or Vue. But in rea...
React Labs: View Transitions & Activity
Jun 17, 2025
React Labs: View Transitions & Activity Published April 23, 2025 by Ricky Hanlon. React Labs is sharing two new experimental featu...
