@adonisjs/inertia 4.0.0-next.8 → 4.0.0-next.9

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.
@@ -260,7 +260,7 @@ var Inertia = class {
260
260
  this.#shouldEncryptHistory = config.encryptHistory ?? false;
261
261
  this.#cachedVersion = this.config.assetsVersion ? String(this.config.assetsVersion) : void 0;
262
262
  }
263
- #sharedState;
263
+ #sharedStateProviders;
264
264
  #cachedRequestInfo;
265
265
  /**
266
266
  * Optional server-side renderer for SSR functionality
@@ -342,7 +342,11 @@ var Inertia = class {
342
342
  *
343
343
  * Handles both static strings and dynamic functions for the root view.
344
344
  *
345
- * @returns The resolved root view template name
345
+ * @example
346
+ * ```js
347
+ * const viewName = this.#resolveRootView()
348
+ * this.ctx.view.render(viewName, { page: pageObject })
349
+ * ```
346
350
  */
347
351
  #resolveRootView() {
348
352
  return typeof this.config.rootView === "function" ? this.config.rootView(this.ctx) : this.config.rootView;
@@ -351,14 +355,37 @@ var Inertia = class {
351
355
  * Constructs and serializes the page props for a given component
352
356
  *
353
357
  * Handles both full page loads and partial requests with prop filtering.
358
+ * Merges shared state providers with page-specific props, and handles
359
+ * prop cherry-picking for partial reloads based on the `only` and `except` parameters.
354
360
  *
355
361
  * @param component - The component name being rendered
356
362
  * @param requestInfo - Information about the current request
357
363
  * @param pageProps - Raw page props to be processed
358
- * @returns Promise resolving to processed props object with metadata
364
+ *
365
+ * @example
366
+ * ```js
367
+ * const result = await this.#buildPageProps('Dashboard', requestInfo, {
368
+ * user: { name: 'John' },
369
+ * posts: defer(() => getPosts())
370
+ * })
371
+ * ```
359
372
  */
360
- #buildPageProps(component, requestInfo, pageProps) {
361
- const finalProps = { ...this.#sharedState, ...pageProps };
373
+ async #buildPageProps(component, requestInfo, pageProps) {
374
+ let finalProps;
375
+ if (this.#sharedStateProviders) {
376
+ const sharedState = await Promise.all(
377
+ this.#sharedStateProviders.map((provider) => {
378
+ return typeof provider === "function" ? provider() : provider;
379
+ })
380
+ ).then((resolvedSharedState) => {
381
+ return resolvedSharedState.reduce((result, state) => {
382
+ return { ...result, ...state };
383
+ }, {});
384
+ });
385
+ finalProps = { ...sharedState, ...pageProps };
386
+ } else {
387
+ finalProps = { ...pageProps };
388
+ }
362
389
  if (requestInfo.partialComponent === component) {
363
390
  const only = requestInfo.onlyProps;
364
391
  const except = requestInfo.exceptProps ?? [];
@@ -376,10 +403,18 @@ var Inertia = class {
376
403
  return buildStandardVisitProps(finalProps, this.ctx.containerResolver);
377
404
  }
378
405
  /**
379
- * Handle Inertia request by setting headers and returning page object
406
+ * Handle Inertia AJAX request by setting appropriate headers
407
+ *
408
+ * Sets the `X-Inertia` header to 'true' indicating this is an Inertia response,
409
+ * then returns the page object which will be serialized as JSON.
380
410
  *
381
411
  * @param pageObject - The page object to return
382
- * @returns The page object with appropriate headers set
412
+ *
413
+ * @example
414
+ * ```js
415
+ * const pageObj = await this.page('Dashboard', props)
416
+ * return this.#handleInertiaRequest(pageObj)
417
+ * ```
383
418
  */
384
419
  #handleInertiaRequest(pageObject) {
385
420
  this.ctx.response.header(InertiaHeaders.Inertia, "true");
@@ -517,7 +552,10 @@ var Inertia = class {
517
552
  * ```
518
553
  */
519
554
  share(sharedState) {
520
- this.#sharedState = { ...this.#sharedState, ...sharedState };
555
+ if (!this.#sharedStateProviders) {
556
+ this.#sharedStateProviders = [];
557
+ }
558
+ this.#sharedStateProviders.push(sharedState);
521
559
  return this;
522
560
  }
523
561
  /**
@@ -4,7 +4,7 @@ import {
4
4
  import {
5
5
  Inertia,
6
6
  ServerRenderer
7
- } from "../chunk-GLGCE7D6.js";
7
+ } from "../chunk-XXMTGYB5.js";
8
8
  import {
9
9
  InertiaHeaders
10
10
  } from "../chunk-DISC5OYC.js";
package/build/index.js CHANGED
@@ -7,7 +7,7 @@ import {
7
7
  InertiaManager,
8
8
  ServerRenderer,
9
9
  symbols_exports
10
- } from "./chunk-GLGCE7D6.js";
10
+ } from "./chunk-XXMTGYB5.js";
11
11
  import {
12
12
  InertiaHeaders
13
13
  } from "./chunk-DISC5OYC.js";
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  InertiaManager
3
- } from "../chunk-GLGCE7D6.js";
3
+ } from "../chunk-XXMTGYB5.js";
4
4
  import "../chunk-DISC5OYC.js";
5
5
  import "../chunk-4EZ2J6OA.js";
6
6
  import "../chunk-MLKGABMK.js";
@@ -3,6 +3,7 @@ import type { HttpContext } from '@adonisjs/core/http';
3
3
  import { type ServerRenderer } from './server_renderer.js';
4
4
  import type { PageProps, PageObject, AsPageProps, RequestInfo, InertiaConfig, ComponentProps, SharedProps } from './types.js';
5
5
  import { defer, merge, always, optional, deepMerge } from './props.ts';
6
+ import { type AsyncOrSync } from '@poppinss/utils/types';
6
7
  /**
7
8
  * Main class used to interact with Inertia
8
9
  *
@@ -165,7 +166,7 @@ export declare class Inertia<Pages> {
165
166
  * .share({ permissions: userPermissions })
166
167
  * ```
167
168
  */
168
- share(sharedState: PageProps): this;
169
+ share(sharedState: PageProps | (() => AsyncOrSync<PageProps>)): this;
169
170
  /**
170
171
  * Build a page object with processed props and metadata
171
172
  *
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  InertiaManager
3
- } from "../chunk-GLGCE7D6.js";
3
+ } from "../chunk-XXMTGYB5.js";
4
4
  import {
5
5
  InertiaHeaders
6
6
  } from "../chunk-DISC5OYC.js";
@@ -65,7 +65,7 @@ var BaseInertiaMiddleware = class {
65
65
  const inertiaContainer = await ctx.containerResolver.make(InertiaManager);
66
66
  ctx.inertia = inertiaContainer.createForRequest(ctx);
67
67
  if (this.share) {
68
- ctx.inertia.share(await this.share(ctx));
68
+ ctx.inertia.share(() => this.share(ctx));
69
69
  }
70
70
  }
71
71
  /**
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@adonisjs/inertia",
3
3
  "description": "Official Inertia.js adapter for AdonisJS",
4
- "version": "4.0.0-next.8",
4
+ "version": "4.0.0-next.9",
5
5
  "engines": {
6
6
  "node": ">=24.0.0"
7
7
  },