Skip to content

Commit f560195

Browse files
committed
fix(inertia-sails): inject inertiaContext middleware when order is not configured
When sails.config.http.middleware.order is not explicitly defined (the default for all Sails apps), the hook skipped injecting the inertiaContext middleware entirely. This caused sails.inertia.share() calls in routes.before handlers to run outside AsyncLocalStorage context, silently dropping shared props. Initialize the middleware order with Sails' defaults when undefined, then inject inertiaContext before the router as intended. Closes #193
1 parent 960657a commit f560195

1 file changed

Lines changed: 16 additions & 1 deletion

File tree

packages/inertia-sails/index.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,22 @@ module.exports = function defineInertiaHook(sails) {
128128
// the request is already wrapped in AsyncLocalStorage context.
129129
if (sails.config.http && sails.config.http.middleware) {
130130
const mw = sails.config.http.middleware
131-
if (mw.order && mw.order.indexOf('inertiaContext') === -1) {
131+
// When order is not explicitly configured, Sails uses a default order
132+
// internally. We need to set it here so we can inject inertiaContext
133+
// before the router middleware.
134+
if (!mw.order) {
135+
mw.order = [
136+
'cookieParser',
137+
'session',
138+
'bodyParser',
139+
'compress',
140+
'poweredBy',
141+
'router',
142+
'www',
143+
'favicon'
144+
]
145+
}
146+
if (mw.order.indexOf('inertiaContext') === -1) {
132147
const routerIdx = mw.order.indexOf('router')
133148
if (routerIdx !== -1) {
134149
mw.order.splice(routerIdx, 0, 'inertiaContext')

0 commit comments

Comments
 (0)