inertia-sails 1.1.0 → 1.3.0
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.
- package/README.md +4 -4
- package/index.js +19 -19
- package/lib/helpers/request-context.js +9 -9
- package/lib/render.js +6 -9
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -75,7 +75,7 @@ Return an Inertia page response:
|
|
|
75
75
|
return {
|
|
76
76
|
page: 'users/index', // Component name
|
|
77
77
|
props: { users: [...] }, // Props passed to component
|
|
78
|
-
|
|
78
|
+
locals: { title: '...' } // Locals for root EJS template
|
|
79
79
|
}
|
|
80
80
|
```
|
|
81
81
|
|
|
@@ -106,12 +106,12 @@ Share data across all requests (app-wide):
|
|
|
106
106
|
sails.inertia.shareGlobally('appName', 'My App')
|
|
107
107
|
```
|
|
108
108
|
|
|
109
|
-
#### `
|
|
109
|
+
#### `local(key, value)`
|
|
110
110
|
|
|
111
|
-
|
|
111
|
+
Set a local variable for the root EJS template:
|
|
112
112
|
|
|
113
113
|
```js
|
|
114
|
-
sails.inertia.
|
|
114
|
+
sails.inertia.local('title', 'Dashboard')
|
|
115
115
|
```
|
|
116
116
|
|
|
117
117
|
### Once Props (Cached)
|
package/index.js
CHANGED
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
* @typedef {Object} InertiaRenderData
|
|
31
31
|
* @property {string} page - The component name to render
|
|
32
32
|
* @property {Object.<string, *>} [props] - Props to pass to the component
|
|
33
|
-
* @property {Object.<string, *>} [
|
|
33
|
+
* @property {Object.<string, *>} [locals] - Additional locals for the root EJS template
|
|
34
34
|
*/
|
|
35
35
|
|
|
36
36
|
/**
|
|
@@ -116,7 +116,7 @@ module.exports = function defineInertiaHook(sails) {
|
|
|
116
116
|
// Global shared props (for app-wide data like app name, version)
|
|
117
117
|
// These are merged with request-scoped shares
|
|
118
118
|
sails.inertia.globalSharedProps = {}
|
|
119
|
-
sails.inertia.
|
|
119
|
+
sails.inertia.globalSharedLocals = {}
|
|
120
120
|
// Default history encryption from config
|
|
121
121
|
sails.inertia.defaultEncryptHistory = sails.config.inertia.history.encrypt
|
|
122
122
|
sails.on('router:before', function () {
|
|
@@ -215,42 +215,42 @@ module.exports = function defineInertiaHook(sails) {
|
|
|
215
215
|
},
|
|
216
216
|
|
|
217
217
|
/**
|
|
218
|
-
*
|
|
218
|
+
* Set a local for the current request's root EJS template.
|
|
219
219
|
* Uses AsyncLocalStorage to ensure data doesn't leak between concurrent requests.
|
|
220
|
-
* @param {string} key - The
|
|
221
|
-
* @param {*} value - The value
|
|
220
|
+
* @param {string} key - The local variable name
|
|
221
|
+
* @param {*} value - The value
|
|
222
222
|
* @returns {*} - The value that was set
|
|
223
223
|
*/
|
|
224
|
-
|
|
224
|
+
local(key, value) {
|
|
225
225
|
const context = requestContext.getContext()
|
|
226
226
|
if (context) {
|
|
227
|
-
requestContext.
|
|
227
|
+
requestContext.setSharedLocal(key, value)
|
|
228
228
|
return value
|
|
229
229
|
}
|
|
230
230
|
// Fallback to global if called outside request
|
|
231
|
-
sails.inertia.
|
|
231
|
+
sails.inertia.globalSharedLocals[key] = value
|
|
232
232
|
return value
|
|
233
233
|
},
|
|
234
234
|
|
|
235
235
|
/**
|
|
236
|
-
*
|
|
237
|
-
* @param {string} key - The
|
|
238
|
-
* @param {*} value - The value
|
|
236
|
+
* Set a local globally across all requests.
|
|
237
|
+
* @param {string} key - The local variable name
|
|
238
|
+
* @param {*} value - The value
|
|
239
239
|
* @returns {*} - The value that was set
|
|
240
240
|
*/
|
|
241
|
-
|
|
242
|
-
sails.inertia.
|
|
241
|
+
localGlobally(key, value) {
|
|
242
|
+
sails.inertia.globalSharedLocals[key] = value
|
|
243
243
|
return value
|
|
244
244
|
},
|
|
245
245
|
|
|
246
246
|
/**
|
|
247
|
-
* Get
|
|
248
|
-
* @param {string} key - The
|
|
249
|
-
* @returns {*} - The
|
|
247
|
+
* Get locals (merges global + request-scoped)
|
|
248
|
+
* @param {string} key - The local variable name to get
|
|
249
|
+
* @returns {*} - The locals
|
|
250
250
|
*/
|
|
251
|
-
|
|
252
|
-
const globalData = sails.inertia.
|
|
253
|
-
const requestData = requestContext.
|
|
251
|
+
getLocals(key) {
|
|
252
|
+
const globalData = sails.inertia.globalSharedLocals
|
|
253
|
+
const requestData = requestContext.getSharedLocals()
|
|
254
254
|
const merged = { ...globalData, ...requestData }
|
|
255
255
|
return key ? merged[key] : merged
|
|
256
256
|
},
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
* req: Request,
|
|
12
12
|
* res: Response,
|
|
13
13
|
* sharedProps: {}, // Request-scoped shared props
|
|
14
|
-
*
|
|
14
|
+
* sharedLocals: {}, // Request-scoped locals for root EJS template
|
|
15
15
|
* encryptHistory: null, // Request-scoped history encryption (null = use default)
|
|
16
16
|
* clearHistory: false, // Request-scoped clear history flag
|
|
17
17
|
* refreshOnceProps: [], // Props to force-refresh for this request
|
|
@@ -40,7 +40,7 @@ module.exports = {
|
|
|
40
40
|
req,
|
|
41
41
|
res,
|
|
42
42
|
sharedProps: {},
|
|
43
|
-
|
|
43
|
+
sharedLocals: {},
|
|
44
44
|
encryptHistory: null,
|
|
45
45
|
clearHistory: false,
|
|
46
46
|
refreshOnceProps: [], // Props to force-refresh for this request
|
|
@@ -97,23 +97,23 @@ module.exports = {
|
|
|
97
97
|
},
|
|
98
98
|
|
|
99
99
|
/**
|
|
100
|
-
* Get request-scoped shared
|
|
101
|
-
* @returns {Object} - The shared
|
|
100
|
+
* Get request-scoped shared locals
|
|
101
|
+
* @returns {Object} - The shared locals for this request
|
|
102
102
|
*/
|
|
103
|
-
|
|
103
|
+
getSharedLocals() {
|
|
104
104
|
const context = requestContext.getStore()
|
|
105
|
-
return context?.
|
|
105
|
+
return context?.sharedLocals || {}
|
|
106
106
|
},
|
|
107
107
|
|
|
108
108
|
/**
|
|
109
|
-
* Set request-scoped shared
|
|
109
|
+
* Set a request-scoped shared local
|
|
110
110
|
* @param {string} key - The key
|
|
111
111
|
* @param {*} value - The value
|
|
112
112
|
*/
|
|
113
|
-
|
|
113
|
+
setSharedLocal(key, value) {
|
|
114
114
|
const context = requestContext.getStore()
|
|
115
115
|
if (context) {
|
|
116
|
-
context.
|
|
116
|
+
context.sharedLocals[key] = value
|
|
117
117
|
}
|
|
118
118
|
},
|
|
119
119
|
|
package/lib/render.js
CHANGED
|
@@ -8,11 +8,11 @@ module.exports = async function render(req, res, data) {
|
|
|
8
8
|
// Use request-scoped rootView if set, otherwise fall back to config
|
|
9
9
|
const rootView = requestContext.getRootView() || sails.config.inertia.rootView
|
|
10
10
|
|
|
11
|
-
// Use request-scoped
|
|
12
|
-
// This prevents
|
|
13
|
-
const
|
|
14
|
-
...sails.inertia.
|
|
15
|
-
...data.
|
|
11
|
+
// Use request-scoped locals merged with global locals
|
|
12
|
+
// This prevents locals from leaking between concurrent requests
|
|
13
|
+
const allLocals = {
|
|
14
|
+
...sails.inertia.getLocals(), // Merges global + request-scoped
|
|
15
|
+
...data.locals
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
let page = await buildPageObject(req, data.page, data.props)
|
|
@@ -32,9 +32,6 @@ module.exports = async function render(req, res, data) {
|
|
|
32
32
|
return res.json(page)
|
|
33
33
|
} else {
|
|
34
34
|
// Implements full page reload
|
|
35
|
-
return res.view(rootView, {
|
|
36
|
-
page,
|
|
37
|
-
viewData: allViewData
|
|
38
|
-
})
|
|
35
|
+
return res.view(rootView, { page, ...allLocals })
|
|
39
36
|
}
|
|
40
37
|
}
|