Closed suhanw closed 4 years ago
What is happening first, yar registration or your custom onPreResponse ext setup?
They are both plugins included in the same array, but yar
appears first in the array:
const plugins = [
// other plugins
{
plugin: require('yar'),
options: {
name: nconf.get('redis:cookieName'),
maxCookieSize: 0,
cache: {
expiresIn: nconf.get('redis:ttl')
},
cookieOptions: {
ttl: nconf.get('redis:ttl'),
password: nconf.get('redis:secret'),
isSecure:
process.env.NODE_ENV === 'production' ||
process.env.NODE_ENV === 'staging' ||
process.env.NODE_ENV === 'ephemeral',
isHttpOnly:
process.env.NODE_ENV === 'production' ||
process.env.NODE_ENV === 'staging' ||
process.env.NODE_ENV === 'ephemeral'
},
storeBlank: false
}
},
// other plugins
{ plugin: require('./routes/main') },
// other plugins
];
await server.register(plugins)
require('./routes/main')
is the plugin where the onPreResponse
ext setup lives.
You need to call the new await request.yar.commit(h)
before returning the redirect.
Is this a new feature that's not available in yar@9.1.0?
Only in 10.1.0.
Support plan
Context
How can we help?
Hi! I'm fairly new to
yar
andhapi
, but let me do my best to explain what I'm trying to accomplish:My Hapi server is making calls to another API service, and I use
keepAlive
agent on these calls, but the challenge we are facing now is every once in a while the service closes the connection and responds withECONNRESET
error.So our 'band-aid' solution for now is to detect these
ECONNRESET
errors, redirect to homepage, and show a simple alert.What I was hoping to do is to (a) detect the
ECONNRESET
response on theonPreResponse
lifecycle, (b) set an alert message in session, and (c) change the response to a redirect to homepage. Here's the snippet:Then, I was going to retrieve the alert message from session in one of my homepage route pre-handlers, and save the alert into an app state object that gets sent to the front-end to display the alert.
const getBase = async (request, h) => { let alerts = request.yar.get('alerts');
}
Many thanks!