Skip to content

Instantly share code, notes, and snippets.

View nitayneeman's full-sized avatar

Nitay Neeman nitayneeman

View GitHub Profile
<body>
<a href="#module1">Module 1</a>
<a href="#module2">Module 2</a>
<script>
window.addEventListener('hashchange', async () => {
const route = window.location.hash.substr(1);
const { myFunction } = await import(`./${route}.mjs`);
myFunction();
});
</script>
window.addEventListener('hashchange', async () => {
// Extracts the new route
const route = window.location.hash.substr(1);
// Loads the module based on the route
const { myFunction } = await import(`./${route}.mjs`);
myFunction();
});
if (isFeatureEnabled) {
import('./module.mjs').then((module) => {
module.myFunction();
});
}
button.addEventListener('click', async () => {
const { myFunction } = await import('./module.mjs');
myFunction();
});
button.addEventListener('click', () =>
import('./module.mjs').then((module) => {
module.myFunction();
})
);
// Static imports
// import { ... } from '...';
// ...
import('./module.mjs').then((module) => {
// The exports are accessible through the module object
module.myFunction();
});
export const myFunction = () => console.log('This is an exported function!');
import { myFunction } from './module.mjs';
myFunction();
import FirstComponent from './FirstComponent.client.js';
import SecondComponent from './SecondComponent.client.js';
function MyServerComponent(props) {
const shouldDisplaySecondComponent = ...; // Some logic
return shouldDisplaySecondComponent ? <SecondComponent/> : <FirstComponent/>;
}
const FirstComponent = React.lazy(() => import('./FirstComponent.client.js'));
const SecondComponent = React.lazy(() => import('./SecondComponent.client.js'));
function MyClientComponent(props) {
const shouldDisplaySecondComponent = ...; // Some logic
return shouldDisplaySecondComponent ? <SecondComponent/> : <FirstComponent/>;
}