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
Laravel 12.21.0 – A Smart Update for Cleaner Queries and Stricter Validation
Aug 03, 2025
Laravel 12.21.0 introduces two game-changing features aimed at writing cleaner, more maintainable code. The update includes the new whereValueBetwe...
CSS Specificity: Layers vs BEM vs Utility Classes
Jun 26, 2025
CSS Specificity: Cascade Layers vs BEM vs Utility Classes This article compares three approaches to managing CSS specificity — BEM, utility‑f...
Essential React Native UI & Interaction Components
Jul 01, 2025
Essential React Native UI & Interaction Components React Native provides a powerful set of built-in components for creating native mobile apps....
Color Everything in CSS – Simple Guide
Jun 26, 2025
Color Everything in CSS – Simple Guide Today we’re diving into CSS colors: how to define them, especially with modern methods like lab(...
Explore the Most Powerful Modern Laravel Tools: Inertia.js, View Creators, and HLS — Step by Step
Jul 27, 2025
Here’s a complete breakdown of essential tools to level up your Laravel development: Inertia.js v2, View Creators, and the Laravel HLS package...
Laravel 12.16.0 - New Features for Developers
Jun 03, 2025
Laravel 12.16.0 - New Features for Developers 1. New Validation Rule: in_array_keys You can now validate that an array contains at least one of the...