From 0a712e34ddc9a565401baf1ff3027c32545e3d54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Re=C3=A9?= Date: Sun, 11 Jan 2026 23:10:53 +0100 Subject: [PATCH] Add Next.js dynamic import bindings --- src/Dynamic.res | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/Dynamic.res diff --git a/src/Dynamic.res b/src/Dynamic.res new file mode 100644 index 0000000..28ec519 --- /dev/null +++ b/src/Dynamic.res @@ -0,0 +1,31 @@ +// Next.js dynamic import binding +// https://nextjs.org/docs/app/building-your-application/optimizing/lazy-loading + +type dynamicOptions = { + ssr?: bool, + loading?: unit => React.element, +} + +/** + * Dynamically import a React component with code splitting. + * + * This creates a separate chunk that is loaded on-demand, reducing the initial + * bundle size. Use with `ssr: false` to also skip server-side rendering. + * + * @example + * ```rescript + * module LazyComponent = { + * type props = {children: React.element} + * + * let component: React.component = GreenfinityNext2_Dynamic.dynamic( + * async () => await Js.import(MyComponent.make), + * {ssr: false, loading: () => }, + * ) + * } + * + * // Usage: + * {React.createElement(LazyComponent.component, {children: ...})} + * ``` + */ +@module("next/dynamic") +external dynamic: ('a => promise<'b>, dynamicOptions) => React.component<'props> = "default"