|
| 1 | +import { createInertiaApp, type ResolvedComponent } from '@inertiajs/react' |
| 2 | +import { StrictMode } from 'react' |
| 3 | +import { createRoot } from 'react-dom/client' |
| 4 | + |
| 5 | +void createInertiaApp({ |
| 6 | + // Set default page title |
| 7 | + // see https://inertia-rails.dev/guide/title-and-meta |
| 8 | + // |
| 9 | + // title: title => title ? `${title} - App` : 'App', |
| 10 | + |
| 11 | + // Disable progress bar |
| 12 | + // |
| 13 | + // see https://inertia-rails.dev/guide/progress-indicators |
| 14 | + // progress: false, |
| 15 | + |
| 16 | + resolve: (name) => { |
| 17 | + const pages = import.meta.glob<{default: ResolvedComponent}>('../pages/**/*.tsx', { |
| 18 | + eager: true, |
| 19 | + }) |
| 20 | + const page = pages[`../pages/${name}.tsx`] |
| 21 | + if (!page) { |
| 22 | + console.error(`Missing Inertia page component: '${name}.tsx'`) |
| 23 | + } |
| 24 | + |
| 25 | + // To use a default layout, import the Layout component |
| 26 | + // and use the following line. |
| 27 | + // see https://inertia-rails.dev/guide/pages#default-layouts |
| 28 | + // |
| 29 | + // page.default.layout ||= (page: ReactNode) => (<Layout>{page}</Layout>) |
| 30 | + |
| 31 | + return page |
| 32 | + }, |
| 33 | + |
| 34 | + setup({ el, App, props }) { |
| 35 | + createRoot(el).render( |
| 36 | + <StrictMode> |
| 37 | + <App {...props} /> |
| 38 | + </StrictMode> |
| 39 | + ) |
| 40 | + }, |
| 41 | + |
| 42 | + defaults: { |
| 43 | + form: { |
| 44 | + forceIndicesArrayFormatInFormData: false, |
| 45 | + }, |
| 46 | + future: { |
| 47 | + useScriptElementForInitialPage: true, |
| 48 | + useDataInertiaHeadAttribute: true, |
| 49 | + useDialogForErrorModal: true, |
| 50 | + preserveEqualProps: true, |
| 51 | + }, |
| 52 | + }, |
| 53 | +}).catch((error) => { |
| 54 | + // This ensures this entrypoint is only loaded on Inertia pages |
| 55 | + // by checking for the presence of the root element (#app by default). |
| 56 | + // Feel free to remove this `catch` if you don't need it. |
| 57 | + if (document.getElementById("app")) { |
| 58 | + throw error |
| 59 | + } else { |
| 60 | + console.error( |
| 61 | + "Missing root element.\n\n" + |
| 62 | + "If you see this error, it probably means you loaded Inertia.js on non-Inertia pages.\n" + |
| 63 | + 'Consider moving <%= vite_typescript_tag "inertia.tsx" %> to the Inertia-specific layout instead.', |
| 64 | + ) |
| 65 | + } |
| 66 | +}) |
0 commit comments