@adonisjs/inertia 1.0.0-0 → 1.0.0-10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. package/build/app.css.stub +39 -0
  2. package/build/chunk-CXICUKHN.js +165 -0
  3. package/build/{stubs/config.stub → config.stub} +9 -1
  4. package/build/index.d.ts +18 -3
  5. package/build/index.js +231 -11
  6. package/build/providers/inertia_provider.d.ts +12 -12
  7. package/build/providers/inertia_provider.js +47 -46
  8. package/build/react/app.tsx.stub +25 -0
  9. package/build/react/home.tsx.stub +21 -0
  10. package/build/react/root.edge.stub +22 -0
  11. package/build/react/ssr.tsx.stub +17 -0
  12. package/build/react/tsconfig.json.stub +25 -0
  13. package/build/solid/app.tsx.stub +24 -0
  14. package/build/solid/home.tsx.stub +21 -0
  15. package/build/solid/root.edge.stub +21 -0
  16. package/build/solid/ssr.tsx.stub +19 -0
  17. package/build/solid/tsconfig.json.stub +26 -0
  18. package/build/src/helpers.d.ts +12 -0
  19. package/build/src/helpers.js +14 -0
  20. package/build/src/inertia_middleware.d.ts +68 -7
  21. package/build/src/inertia_middleware.js +6 -47
  22. package/build/src/plugins/edge/plugin.d.ts +8 -0
  23. package/build/src/plugins/edge/plugin.js +85 -0
  24. package/build/src/plugins/{api_client.d.ts → japa/api_client.d.ts} +9 -4
  25. package/build/src/plugins/japa/api_client.js +70 -0
  26. package/build/src/plugins/vite.d.ts +26 -0
  27. package/build/src/plugins/vite.js +33 -0
  28. package/build/src/types.d.ts +72 -8
  29. package/build/src/types.js +0 -9
  30. package/build/vue/app.ts.stub +27 -0
  31. package/build/vue/home.vue.stub +21 -0
  32. package/build/vue/root.edge.stub +21 -0
  33. package/build/vue/ssr.ts.stub +22 -0
  34. package/build/vue/tsconfig.json.stub +26 -0
  35. package/package.json +64 -47
  36. package/build/configure.js +0 -82
  37. package/build/src/debug.d.ts +0 -3
  38. package/build/src/debug.js +0 -10
  39. package/build/src/define_config.d.ts +0 -5
  40. package/build/src/define_config.js +0 -14
  41. package/build/src/inertia.d.ts +0 -39
  42. package/build/src/inertia.js +0 -119
  43. package/build/src/plugins/api_client.js +0 -62
  44. package/build/src/plugins/edge.d.ts +0 -5
  45. package/build/src/plugins/edge.js +0 -43
  46. package/build/src/version_cache.d.ts +0 -27
  47. package/build/src/version_cache.js +0 -68
  48. package/build/stubs/main.d.ts +0 -1
  49. package/build/stubs/main.js +0 -10
  50. package/providers/inertia_provider.ts +0 -66
  51. package/src/debug.ts +0 -12
  52. package/src/define_config.ts +0 -17
  53. package/src/inertia.ts +0 -140
  54. package/src/inertia_middleware.ts +0 -54
  55. package/src/plugins/api_client.ts +0 -127
  56. package/src/plugins/edge.ts +0 -50
  57. package/src/types.ts +0 -48
  58. package/src/version_cache.ts +0 -74
@@ -0,0 +1,39 @@
1
+ {{{
2
+ exports({ to: app.makePath('resources/css/app.css') })
3
+ }}}
4
+ @import url('https://fonts.googleapis.com/css2?family=Poppins:wght@400;500&display=swap');
5
+
6
+ * {
7
+ margin: 0;
8
+ padding: 0;
9
+ }
10
+
11
+ html,
12
+ body,
13
+ #app {
14
+ background-color: #F7F8FA;
15
+ font-family: 'Poppins', sans-serif;
16
+ color: #46444c;
17
+ height: 100%;
18
+ width: 100%;
19
+ }
20
+
21
+ .title {
22
+ font-size: 42px;
23
+ font-weight: 500;
24
+ color: #5a45ff;
25
+ }
26
+
27
+ .container {
28
+ display: flex;
29
+ justify-content: center;
30
+ align-items: center;
31
+ flex-direction: column;
32
+ height: 100%;
33
+ width: 100%;
34
+ }
35
+
36
+ a {
37
+ text-decoration: underline;
38
+ color: #5a45ff;
39
+ }
@@ -0,0 +1,165 @@
1
+ // src/inertia.ts
2
+ var kLazySymbol = Symbol("lazy");
3
+ var Inertia = class {
4
+ constructor(ctx, config, viteRuntime) {
5
+ this.ctx = ctx;
6
+ this.config = config;
7
+ this.viteRuntime = viteRuntime;
8
+ this.#sharedData = config.sharedData;
9
+ }
10
+ #sharedData = {};
11
+ /**
12
+ * Check if a value is a lazy prop
13
+ */
14
+ #isLazyProps(value) {
15
+ return typeof value === "object" && value && kLazySymbol in value;
16
+ }
17
+ /**
18
+ * Pick props to resolve based on x-inertia-partial-data header
19
+ *
20
+ * If header is not present, resolve all props except lazy props
21
+ * If header is present, resolve only the props that are listed in the header
22
+ */
23
+ #pickPropsToResolve(component, props) {
24
+ const partialData = this.ctx.request.header("x-inertia-partial-data")?.split(",").filter(Boolean);
25
+ const partialComponent = this.ctx.request.header("x-inertia-partial-component");
26
+ let entriesToResolve = Object.entries(props);
27
+ if (partialData && partialComponent === component) {
28
+ entriesToResolve = entriesToResolve.filter(([key]) => partialData.includes(key));
29
+ } else {
30
+ entriesToResolve = entriesToResolve.filter(([key]) => !this.#isLazyProps(props[key]));
31
+ }
32
+ return entriesToResolve;
33
+ }
34
+ /**
35
+ * Resolve the props that will be sent to the client
36
+ */
37
+ async #resolvePageProps(component, props) {
38
+ const entriesToResolve = this.#pickPropsToResolve(component, props);
39
+ const entries = entriesToResolve.map(async ([key, value]) => {
40
+ if (typeof value === "function") {
41
+ return [key, await value(this.ctx)];
42
+ }
43
+ if (this.#isLazyProps(value)) {
44
+ const lazyValue = value[kLazySymbol];
45
+ return [key, await lazyValue()];
46
+ }
47
+ return [key, value];
48
+ });
49
+ return Object.fromEntries(await Promise.all(entries));
50
+ }
51
+ /**
52
+ * Build the page object that will be returned to the client
53
+ *
54
+ * See https://inertiajs.com/the-protocol#the-page-object
55
+ */
56
+ async #buildPageObject(component, pageProps) {
57
+ return {
58
+ component,
59
+ version: this.config.versionCache.getVersion(),
60
+ props: await this.#resolvePageProps(component, { ...this.#sharedData, ...pageProps }),
61
+ url: this.ctx.request.url(true)
62
+ };
63
+ }
64
+ /**
65
+ * If the page should be rendered on the server
66
+ */
67
+ #shouldRenderOnServer(component) {
68
+ const isSsrEnabled = this.config.ssr.enabled;
69
+ const isSsrEnabledForPage = this.config.ssr.pages ? this.config.ssr.pages.includes(component) : true;
70
+ return isSsrEnabled && isSsrEnabledForPage;
71
+ }
72
+ /**
73
+ * Render the page on the server
74
+ *
75
+ * On development, we use the Vite Runtime API
76
+ * On production, we just import and use the SSR bundle generated by Vite
77
+ */
78
+ async #renderOnServer(pageObject, viewProps) {
79
+ let render;
80
+ if (this.viteRuntime) {
81
+ render = await this.viteRuntime.executeEntrypoint(this.config.ssr.entrypoint);
82
+ } else {
83
+ render = await import(this.config.ssr.bundle);
84
+ }
85
+ const result = await render.default(pageObject);
86
+ return this.ctx.view.render(this.config.rootView, {
87
+ ...viewProps,
88
+ page: { ssrHead: result.head, ssrBody: result.body }
89
+ });
90
+ }
91
+ /**
92
+ * Share data for the current request.
93
+ * This data will override any shared data defined in the config.
94
+ */
95
+ share(data) {
96
+ this.#sharedData = { ...this.#sharedData, ...data };
97
+ }
98
+ /**
99
+ * Render a page using Inertia
100
+ */
101
+ async render(component, pageProps, viewProps) {
102
+ const pageObject = await this.#buildPageObject(component, pageProps);
103
+ const isInertiaRequest = !!this.ctx.request.header("x-inertia");
104
+ if (!isInertiaRequest) {
105
+ const shouldRenderOnServer = this.#shouldRenderOnServer(component);
106
+ if (shouldRenderOnServer)
107
+ return this.#renderOnServer(pageObject, viewProps);
108
+ return this.ctx.view.render(this.config.rootView, { ...viewProps, page: pageObject });
109
+ }
110
+ this.ctx.response.header("x-inertia", "true");
111
+ return pageObject;
112
+ }
113
+ /**
114
+ * Create a lazy prop
115
+ *
116
+ * Lazy props are never resolved on first visit, but only when the client
117
+ * request a partial reload explicitely with this value.
118
+ *
119
+ * See https://inertiajs.com/partial-reloads#lazy-data-evaluation
120
+ */
121
+ lazy(callback) {
122
+ return { [kLazySymbol]: callback };
123
+ }
124
+ /**
125
+ * This method can be used to redirect the user to an external website
126
+ * or even a non-inertia route of your application.
127
+ *
128
+ * See https://inertiajs.com/redirects#external-redirects
129
+ */
130
+ async location(url) {
131
+ this.ctx.response.header("X-Inertia-Location", url);
132
+ this.ctx.response.status(409);
133
+ }
134
+ };
135
+
136
+ // src/inertia_middleware.ts
137
+ var InertiaMiddleware = class {
138
+ constructor(config, vite) {
139
+ this.config = config;
140
+ this.#runtime = vite?.getRuntime();
141
+ }
142
+ #runtime;
143
+ async handle(ctx, next) {
144
+ const { response, request } = ctx;
145
+ ctx.inertia = new Inertia(ctx, this.config, this.#runtime);
146
+ await next();
147
+ const isInertiaRequest = !!request.header("x-inertia");
148
+ if (!isInertiaRequest)
149
+ return;
150
+ response.header("Vary", "Accept");
151
+ const method = request.method();
152
+ if (response.getStatus() === 302 && ["PUT", "PATCH", "DELETE"].includes(method)) {
153
+ response.status(303);
154
+ }
155
+ const version = this.config.versionCache.getVersion().toString();
156
+ if (method === "GET" && request.header("x-inertia-version", "") !== version) {
157
+ response.header("x-inertia-location", request.url());
158
+ response.status(409);
159
+ }
160
+ }
161
+ };
162
+
163
+ export {
164
+ InertiaMiddleware
165
+ };
@@ -7,7 +7,7 @@ export default defineConfig({
7
7
  /**
8
8
  * Path to the Edge view that will be used as the root view for Inertia responses
9
9
  */
10
- rootView: 'home',
10
+ rootView: 'root',
11
11
 
12
12
  /**
13
13
  * Data that should be shared with all rendered pages
@@ -15,4 +15,12 @@ export default defineConfig({
15
15
  sharedData: {
16
16
  errors: (ctx) => ctx.session.flashMessages.get('errors'),
17
17
  },
18
+
19
+ /**
20
+ * Options for the server-side rendering
21
+ */
22
+ ssr: {
23
+ enabled: {{ ssr }},
24
+ entrypoint: '{{ ssrEntrypoint }}'
25
+ }
18
26
  })
package/build/index.d.ts CHANGED
@@ -1,3 +1,18 @@
1
- export { configure } from './configure.js';
2
- export { stubsRoot } from './stubs/main.js';
3
- export { defineConfig } from './src/define_config.js';
1
+ import Configure from '@adonisjs/core/commands/configure';
2
+ import { ConfigProvider } from '@adonisjs/core/types';
3
+ import { InertiaConfig, ResolvedConfig } from './src/types.js';
4
+ import '@adonisjs/core/http';
5
+
6
+ /**
7
+ * Configures the package
8
+ */
9
+ declare function configure(command: Configure): Promise<void>;
10
+
11
+ /**
12
+ * Define the Inertia configuration
13
+ */
14
+ declare function defineConfig(config: InertiaConfig): ConfigProvider<ResolvedConfig>;
15
+
16
+ declare const stubsRoot: string;
17
+
18
+ export { configure, defineConfig, stubsRoot };
package/build/index.js CHANGED
@@ -1,11 +1,231 @@
1
- /*
2
- * @adonisjs/inertia
3
- *
4
- * (c) AdonisJS
5
- *
6
- * For the full copyright and license information, please view the LICENSE
7
- * file that was distributed with this source code.
8
- */
9
- export { configure } from './configure.js';
10
- export { stubsRoot } from './stubs/main.js';
11
- export { defineConfig } from './src/define_config.js';
1
+ // stubs/main.ts
2
+ import { getDirname } from "@poppinss/utils";
3
+ var stubsRoot = getDirname(import.meta.url);
4
+
5
+ // configure.ts
6
+ var ADAPTERS = ["Vue 3", "React", "Svelte", "Solid"];
7
+ var ADAPTERS_INFO = {
8
+ "Vue 3": {
9
+ stubFolder: "vue",
10
+ appExtension: "ts",
11
+ componentsExtension: "vue",
12
+ dependencies: [
13
+ { name: "@inertiajs/vue3", isDevDependency: false },
14
+ { name: "vue", isDevDependency: false },
15
+ { name: "@vitejs/plugin-vue", isDevDependency: true }
16
+ ],
17
+ ssrDependencies: [{ name: "@vue/server-renderer", isDevDependency: false }],
18
+ viteRegister: {
19
+ pluginCall: "vue()",
20
+ importDeclarations: [{ isNamed: false, module: "@vitejs/plugin-vue", identifier: "vue" }]
21
+ },
22
+ ssrEntrypoint: "resources/ssr.ts"
23
+ },
24
+ "React": {
25
+ stubFolder: "react",
26
+ appExtension: "tsx",
27
+ componentsExtension: "tsx",
28
+ dependencies: [
29
+ { name: "@inertiajs/react", isDevDependency: false },
30
+ { name: "react", isDevDependency: false },
31
+ { name: "react-dom", isDevDependency: false },
32
+ { name: "@vitejs/plugin-react", isDevDependency: true },
33
+ { name: "@types/react", isDevDependency: true },
34
+ { name: "@types/react-dom", isDevDependency: true }
35
+ ],
36
+ viteRegister: {
37
+ pluginCall: "react()",
38
+ importDeclarations: [{ isNamed: false, module: "@vitejs/plugin-react", identifier: "react" }]
39
+ },
40
+ ssrEntrypoint: "resources/ssr.tsx"
41
+ },
42
+ "Svelte": {
43
+ stubFolder: "svelte",
44
+ appExtension: "ts",
45
+ componentsExtension: "svelte",
46
+ dependencies: [
47
+ { name: "@inertiajs/svelte", isDevDependency: false },
48
+ { name: "svelte", isDevDependency: false },
49
+ { name: "@sveltejs/vite-plugin-svelte", isDevDependency: true }
50
+ ],
51
+ viteRegister: {
52
+ pluginCall: "svelte()",
53
+ importDeclarations: [
54
+ { isNamed: false, module: "@sveltejs/vite-plugin-svelte", identifier: "svelte" }
55
+ ]
56
+ },
57
+ ssrEntrypoint: "resources/ssr.ts"
58
+ },
59
+ "Solid": {
60
+ stubFolder: "solid",
61
+ appExtension: "tsx",
62
+ componentsExtension: "tsx",
63
+ dependencies: [
64
+ { name: "solid-js", isDevDependency: false },
65
+ { name: "inertia-adapter-solid", isDevDependency: false },
66
+ { name: "vite-plugin-solid", isDevDependency: true },
67
+ { name: "@solidjs/meta", isDevDependency: false }
68
+ ],
69
+ viteRegister: {
70
+ pluginCall: "solid()",
71
+ ssrPluginCall: "solid({ ssr: true })",
72
+ importDeclarations: [{ isNamed: false, module: "vite-plugin-solid", identifier: "solid" }]
73
+ },
74
+ ssrEntrypoint: "resources/ssr.tsx"
75
+ }
76
+ };
77
+ async function defineExampleRoute(command, codemods) {
78
+ const tsMorph = await codemods.getTsMorphProject();
79
+ const routesFile = tsMorph?.getSourceFile(command.app.makePath("./start/routes.ts"));
80
+ if (!routesFile) {
81
+ return command.logger.warning("Unable to find the routes file");
82
+ }
83
+ const isAlreadyDefined = routesFile.getText().includes("/inertia");
84
+ if (isAlreadyDefined) {
85
+ command.logger.warning("/inertia route is already defined. Skipping");
86
+ return;
87
+ }
88
+ const action = command.logger.action("update start/routes.ts file");
89
+ try {
90
+ routesFile?.addStatements((writer) => {
91
+ writer.writeLine(`router.on('/inertia').renderInertia('home', { version: 6 })`);
92
+ });
93
+ await tsMorph?.save();
94
+ action.succeeded();
95
+ } catch (error) {
96
+ codemods.emit("error", error);
97
+ action.failed(error.message);
98
+ }
99
+ }
100
+ async function configure(command) {
101
+ const adapter = await command.prompt.choice(
102
+ "Select the Inertia adapter you want to use",
103
+ ADAPTERS,
104
+ { name: "adapter" }
105
+ );
106
+ const ssr = await command.prompt.confirm("Do you want to use server-side rendering?", {
107
+ name: "ssr"
108
+ });
109
+ const adapterInfo = ADAPTERS_INFO[adapter];
110
+ const codemods = await command.createCodemods();
111
+ await codemods.updateRcFile((rcFile) => {
112
+ rcFile.addProvider("@adonisjs/inertia/inertia_provider");
113
+ });
114
+ await codemods.registerMiddleware("router", [
115
+ { path: "@adonisjs/inertia/inertia_middleware", position: "after" }
116
+ ]);
117
+ const appExt = adapterInfo.appExtension;
118
+ const stubFolder = adapterInfo.stubFolder;
119
+ const compExt = adapterInfo.componentsExtension;
120
+ await codemods.makeUsingStub(stubsRoot, "config.stub", {
121
+ ssr,
122
+ ssrEntrypoint: adapterInfo.ssrEntrypoint
123
+ });
124
+ await codemods.makeUsingStub(stubsRoot, `app.css.stub`, {});
125
+ await codemods.makeUsingStub(stubsRoot, `${stubFolder}/root.edge.stub`, {});
126
+ await codemods.makeUsingStub(stubsRoot, `${stubFolder}/tsconfig.json.stub`, {});
127
+ await codemods.makeUsingStub(stubsRoot, `${stubFolder}/app.${appExt}.stub`, {});
128
+ await codemods.makeUsingStub(stubsRoot, `${stubFolder}/home.${compExt}.stub`, {});
129
+ if (ssr) {
130
+ await codemods.makeUsingStub(stubsRoot, `${stubFolder}/ssr.${appExt}.stub`, {});
131
+ }
132
+ const inertiaPluginCall = ssr ? `inertia({ ssr: { enabled: true, entrypoint: 'resources/ssr.${appExt}' } })` : `inertia({ ssr: { enabled: false } })`;
133
+ await codemods.registerVitePlugin(inertiaPluginCall, [
134
+ { isNamed: false, module: "@adonisjs/inertia/client", identifier: "inertia" }
135
+ ]);
136
+ await codemods.registerVitePlugin(
137
+ ssr && adapterInfo.viteRegister.ssrPluginCall ? adapterInfo.viteRegister.ssrPluginCall : adapterInfo.viteRegister.pluginCall,
138
+ adapterInfo.viteRegister.importDeclarations
139
+ );
140
+ await defineExampleRoute(command, codemods);
141
+ const pkgToInstall = adapterInfo.dependencies;
142
+ if (ssr && adapterInfo.ssrDependencies) {
143
+ pkgToInstall.push(...adapterInfo.ssrDependencies);
144
+ }
145
+ const shouldInstallPackages = await command.prompt.confirm(
146
+ `Do you want to install dependencies ${pkgToInstall.map((pkg) => pkg.name).join(", ")}?`,
147
+ { name: "install" }
148
+ );
149
+ if (shouldInstallPackages) {
150
+ await codemods.installPackages(pkgToInstall);
151
+ } else {
152
+ await codemods.listPackagesToInstall(pkgToInstall);
153
+ }
154
+ const colors = command.colors;
155
+ command.ui.instructions().heading("Inertia was successfully configured !").add(`We have added a dummy ${colors.cyan("/inertia")} route in your project.`).add(`Try visiting it in your browser after starting your server to see Inertia in action`).add("Happy coding !").render();
156
+ }
157
+
158
+ // src/define_config.ts
159
+ import { configProvider } from "@adonisjs/core";
160
+
161
+ // src/version_cache.ts
162
+ import { readFile } from "node:fs/promises";
163
+ var VersionCache = class {
164
+ constructor(appRoot, assetsVersion) {
165
+ this.appRoot = appRoot;
166
+ this.assetsVersion = assetsVersion;
167
+ this.#cachedVersion = assetsVersion;
168
+ }
169
+ #cachedVersion;
170
+ /**
171
+ * Compute the hash of the manifest file and cache it
172
+ */
173
+ async #getManifestHash() {
174
+ try {
175
+ const crc32 = await import("crc-32");
176
+ const manifestPath = new URL("public/assets/manifest.json", this.appRoot);
177
+ const manifestFile = await readFile(manifestPath, "utf-8");
178
+ this.#cachedVersion = crc32.default.str(manifestFile);
179
+ return this.#cachedVersion;
180
+ } catch {
181
+ this.#cachedVersion = "1";
182
+ return this.#cachedVersion;
183
+ }
184
+ }
185
+ /**
186
+ * Pre-compute the version
187
+ */
188
+ async computeVersion() {
189
+ if (!this.assetsVersion)
190
+ await this.#getManifestHash();
191
+ return this;
192
+ }
193
+ /**
194
+ * Returns the current assets version
195
+ */
196
+ getVersion() {
197
+ if (!this.#cachedVersion)
198
+ throw new Error("Version has not been computed yet");
199
+ return this.#cachedVersion;
200
+ }
201
+ /**
202
+ * Set the assets version
203
+ */
204
+ async setVersion(version) {
205
+ this.#cachedVersion = version;
206
+ }
207
+ };
208
+
209
+ // src/define_config.ts
210
+ function defineConfig(config) {
211
+ return configProvider.create(async (app) => {
212
+ const versionCache = new VersionCache(app.appRoot, config.assetsVersion);
213
+ await versionCache.computeVersion();
214
+ return {
215
+ rootView: config.rootView ?? "root",
216
+ sharedData: config.sharedData || {},
217
+ versionCache,
218
+ ssr: {
219
+ enabled: config.ssr?.enabled ?? false,
220
+ pages: config.ssr?.pages,
221
+ entrypoint: config.ssr?.entrypoint ?? app.makePath("resources/ssr.ts"),
222
+ bundle: config.ssr?.bundle ?? app.makePath("ssr/ssr.js")
223
+ }
224
+ };
225
+ });
226
+ }
227
+ export {
228
+ configure,
229
+ defineConfig,
230
+ stubsRoot
231
+ };
@@ -1,26 +1,26 @@
1
- import type { ApplicationService } from '@adonisjs/core/types';
2
- import { Inertia } from '../src/inertia.js';
3
- /**
4
- * HttpContext augmentations
5
- */
1
+ import { ApplicationService } from '@adonisjs/core/types';
2
+
6
3
  declare module '@adonisjs/core/http' {
7
- interface HttpContext {
8
- inertia: Inertia;
4
+ interface BriskRoute {
5
+ /**
6
+ * Render an inertia page without defining an
7
+ * explicit route handler
8
+ */
9
+ renderInertia(component: string, props?: Record<string, any>, viewProps?: Record<string, any>): void;
9
10
  }
10
11
  }
11
12
  /**
12
13
  * Inertia provider
13
14
  */
14
- export default class InertiaProvider {
15
+ declare class InertiaProvider {
15
16
  protected app: ApplicationService;
16
17
  constructor(app: ApplicationService);
17
18
  /**
18
19
  * Registers edge plugin when edge is installed
19
20
  */
20
21
  protected registerEdgePlugin(): Promise<void>;
21
- /**
22
- * Register Inertia middleware, edge plugin, and add
23
- * `inertia` property to the HttpContext
24
- */
22
+ register(): Promise<void>;
25
23
  boot(): Promise<void>;
26
24
  }
25
+
26
+ export { InertiaProvider as default };
@@ -1,46 +1,47 @@
1
- /*
2
- * @adonisjs/inertia
3
- *
4
- * (c) AdonisJS
5
- *
6
- * For the full copyright and license information, please view the LICENSE
7
- * file that was distributed with this source code.
8
- */
9
- import { HttpContext } from '@adonisjs/core/http';
10
- import { Inertia } from '../src/inertia.js';
11
- import { VersionCache } from '../src/version_cache.js';
12
- import InertiaMiddleware from '../src/inertia_middleware.js';
13
- /**
14
- * Inertia provider
15
- */
16
- export default class InertiaProvider {
17
- app;
18
- constructor(app) {
19
- this.app = app;
20
- }
21
- /**
22
- * Registers edge plugin when edge is installed
23
- */
24
- async registerEdgePlugin() {
25
- try {
26
- const edgeExports = await import('edge.js');
27
- const { edgePluginInertia } = await import('../src/plugins/edge.js');
28
- edgeExports.default.use(edgePluginInertia());
29
- }
30
- catch { }
31
- }
32
- /**
33
- * Register Inertia middleware, edge plugin, and add
34
- * `inertia` property to the HttpContext
35
- */
36
- async boot() {
37
- const appRoot = this.app.appRoot;
38
- const config = this.app.config.get('inertia', { view: 'app' });
39
- const versionCache = await new VersionCache(appRoot, config.assetsVersion).computeVersion();
40
- this.app.container.singleton(InertiaMiddleware, () => new InertiaMiddleware(versionCache));
41
- HttpContext.getter('inertia', function inertia() {
42
- return new Inertia(this, config, versionCache.getVersion());
43
- }, false);
44
- await this.registerEdgePlugin();
45
- }
46
- }
1
+ import {
2
+ InertiaMiddleware
3
+ } from "../chunk-CXICUKHN.js";
4
+
5
+ // providers/inertia_provider.ts
6
+ import { configProvider } from "@adonisjs/core";
7
+ import { RuntimeException } from "@poppinss/utils";
8
+ import { BriskRoute } from "@adonisjs/core/http";
9
+ var InertiaProvider = class {
10
+ constructor(app) {
11
+ this.app = app;
12
+ }
13
+ /**
14
+ * Registers edge plugin when edge is installed
15
+ */
16
+ async registerEdgePlugin() {
17
+ if (!this.app.usingEdgeJS)
18
+ return;
19
+ const edgeExports = await import("edge.js");
20
+ const { edgePluginInertia } = await import("../src/plugins/edge/plugin.js");
21
+ edgeExports.default.use(edgePluginInertia());
22
+ }
23
+ async register() {
24
+ this.app.container.singleton(InertiaMiddleware, async () => {
25
+ const inertiaConfigProvider = this.app.config.get("inertia");
26
+ const config = await configProvider.resolve(this.app, inertiaConfigProvider);
27
+ const vite = await this.app.container.make("vite");
28
+ if (!config) {
29
+ throw new RuntimeException(
30
+ 'Invalid "config/inertia.ts" file. Make sure you are using the "defineConfig" method'
31
+ );
32
+ }
33
+ return new InertiaMiddleware(config, vite);
34
+ });
35
+ }
36
+ async boot() {
37
+ await this.registerEdgePlugin();
38
+ BriskRoute.macro("renderInertia", function(template, props, viewProps) {
39
+ return this.setHandler(({ inertia }) => {
40
+ return inertia.render(template, props, viewProps);
41
+ });
42
+ });
43
+ }
44
+ };
45
+ export {
46
+ InertiaProvider as default
47
+ };
@@ -0,0 +1,25 @@
1
+ {{{
2
+ exports({ to: app.makePath('resources/app.tsx') })
3
+ }}}
4
+ import './css/app.css';
5
+
6
+ import { createRoot } from 'react-dom/client';
7
+ import { createInertiaApp } from '@inertiajs/react';
8
+
9
+ const appName = import.meta.env.VITE_APP_NAME || 'AdonisJS'
10
+
11
+ createInertiaApp({
12
+ progress: { color: '#5468FF' },
13
+
14
+ title: (title) => {{ '`${title} - ${appName}`' }},
15
+
16
+ resolve: (name) => {
17
+ const pages = import.meta.glob('./pages/**/*.tsx', { eager: true })
18
+ {{ 'return pages[`./pages/${name}.tsx`]' }}
19
+ },
20
+
21
+ setup({ el, App, props }) {
22
+ const root = createRoot(el);
23
+ root.render(<App {...props} />);
24
+ },
25
+ });
@@ -0,0 +1,21 @@
1
+ {{{
2
+ exports({ to: app.makePath('resources/pages/home.tsx') })
3
+ }}}
4
+ import { Head } from '@inertiajs/react'
5
+
6
+ export default function Home(props: { version: number }) {
7
+ return (
8
+ <>
9
+ <Head title="Homepage" />
10
+
11
+ <div className="container">
12
+ <div className="title">AdonisJS {props.version} x Inertia x React</div>
13
+
14
+ <span>
15
+ Learn more about AdonisJS and Inertia.js by visiting the{' '}
16
+ <a href="https://docs.adonisjs.com/inertia">AdonisJS documentation</a>.
17
+ </span>
18
+ </div>
19
+ </>
20
+ )
21
+ }