@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
|
-
#
|
|
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
|
-
* @
|
|
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
|
-
*
|
|
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
|
-
|
|
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
|
|
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
|
-
*
|
|
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.#
|
|
555
|
+
if (!this.#sharedStateProviders) {
|
|
556
|
+
this.#sharedStateProviders = [];
|
|
557
|
+
}
|
|
558
|
+
this.#sharedStateProviders.push(sharedState);
|
|
521
559
|
return this;
|
|
522
560
|
}
|
|
523
561
|
/**
|
package/build/factories/main.js
CHANGED
package/build/index.js
CHANGED
package/build/src/inertia.d.ts
CHANGED
|
@@ -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-
|
|
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(
|
|
68
|
+
ctx.inertia.share(() => this.share(ctx));
|
|
69
69
|
}
|
|
70
70
|
}
|
|
71
71
|
/**
|