Skip to content

Dynamic imports in module scripts

Astro ^4.0.0

Scripts with no attributes in Astro are automatically processed into type="module" and all hoisted into one .js file that has static imports to other .js files. It is possible to use dynamic module loading instead of the default static imports, in order to gain more granular control over if/when the imported code is actually imported (downloaded).

For more details on Astro’s script processing, refer to the official documentation.


If you want to see a more interactive example that might help explain or let you test things out better you can:

Granular Control

Prioritize critical modules by importing less critical modules only after the critical ones are processed.

import criticalModule from 'criticalModule';
const lessCriticalModule = await import('lessCriticalModule');

Recreate Client Directives in <script>s

Effects similar to client:visible, client:media, and client:idle can all be achieved with this technique.

const observer = new IntersectionObserver(async (entries, observer) => {
entries.forEach(async entry => {
if (entry.isIntersecting) {
const { module } = await import('module');

Feature Flags

Dynamically import modules based on feature flags. For example, loading a specific feature only for users who have opted in or who meet certain criteria.

if (featureFlag) {
const { featureModule } = await import('featureModule');

Device-Specific Code

Import desktop-specific code that doesn’t need to be downloaded on mobile devices.

(async () => {
if (!('ontouchstart' in window || navigator.maxTouchPoints > 0)) {
const { desktopModule } = await import('desktopModule');

Rarely Clicked Buttons

A button that might be rarely clicked, and when it is, the module needed to hydrate it is small enough that pre-hydration is unnecessary.

document.querySelector('#rareButton').addEventListener('click', async () => {
const { buttonModule } = await import('buttonModule');

Considerations before using this technique

When a user clicks a button they expect it to react instantly. If you dynamically import heavy code that hydrates a button only when it is clicked that could create a delay between interaction and next paint. As a general rule any code that hydrates interactivity shouldn’t be delayed.