Skip to content

Unnecessary server sessions [fix] #509

Description

@rodeyseijkens

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()
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions