To boot the Inertia app on the client side, install the Inertia library for your framework (e.g., @inertiajs/react) and configure your entrypoint (e.g., assets/js/app.jsx).
Client-side Bootstrapping
// assets/js/app.jsx
import React from "react";
import axios from "axios";
import { createInertiaApp } from "@inertiajs/react";
import { createRoot } from "react-dom/client";
axios.defaults.xsrfHeaderName = "x-csrf-token";
createInertiaApp({
resolve: async (name) => {
return await import(`./pages/${name}.jsx`);
},
setup({ App, el, props }) {
createRoot(el).render(<App {...props} />);
},
});
Esbuild Configuration
Ensure your esbuild configuration meets these requirements:
version: >= 0.19.0 (for glob-style imports).args: The entrypoint should use the correct extension (e.g., .jsx) and the --target should be at least es2020.
Enabling ESM Code Splitting
To enable code splitting with esbuild, update your config.exs with the following flags:
--splitting--format=esm--chunk-names=chunks/[name]-[hash]
Note: ESM code splitting requires updating your root layout to load the script as a module:
<script type='module' defer phx-track-static src={~p"/assets/app.js"}></script>
// assets/js/app.jsx
import React from "react";
import axios from "axios";
import { createInertiaApp } from "@inertiajs/react";
import { createRoot } from "react-dom/client";
axios.defaults.xsrfHeaderName = "x-csrf-token";
createInertiaApp({
resolve: async (name) => {
return await import(`./pages/${name}.jsx`);
},
setup({ App, el, props }) {
createRoot(el).render(<App {...props} />);
},
});