Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unnecessary server sessions [fix] #509

Open
rodeyseijkens opened this issue Jan 8, 2016 · 2 comments
Open

Unnecessary server sessions [fix] #509

rodeyseijkens opened this issue Jan 8, 2016 · 2 comments

Comments

@rodeyseijkens
Copy link

I think this is worth sharing.

By default the rendr server side is creating a new session every time it is doing stuff. Every visit or hard page refresh. This of course isn't really nice especially if you store it in a database for example because the a lot of unnecessary sessions are stored.

var sessionMiddleware = session({
    secret: config.server.session.secret,
    cookie: { maxAge: 24 * 60 * 60 * 1000 * 14 },
    secure: true,
    store: new MongoStore({ mongooseConnection: mongoose.connection }),
    resave: false,
    saveUninitialized: false
});

// Do not make a session when its the server or when it is a bot
app.use(function useSession(req, res, next) {
    var userAgent = req.headers['user-agent'],
          isBot = (/bot|googlebot|crawler|spider|robot|crawling/i.test(userAgent)),
          isServer = (userAgent == "Rendr RestAdapter; Node.js");

    if (!isBot && !isServer) {
        return sessionMiddleware(req, res, next)
    }

    next()
});
@pjanuario
Copy link

👍

@bigethan
Copy link
Contributor

Shouldn't the middleware see the session cookie when the client app is communicating to the server and not create a new session? Or am I missing something?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants