Skip to content

v4.0.0

Compare
Choose a tag to compare
@hoangvvo hoangvvo released this 22 Sep 10:56
· 13 commits to master since this release
  • ESM support, more lightweight
  • More reliable due to removal of troublesome features like dual-getInitialProps/api support, callback/promise store dual support, etc.
  • Add session.touch()

note: v3 is continued to be maintained with bug fixes.

Breaking changes

session.isNew is removed

You can check the number of keys in session to determined if it is new or set a custom property to your session

const isNew = Object.keys(req.session) > 1; // req.session only has the `cookie` property if new
// OR
req.session.isNotNew = true;

touchAfter is now in seconds and not miliseconds

// BEFORE
const options = {
  touchAfter: 7 * 24 * 60 * 60 * 1000, // 1 week
};

// AFTER
const options = {
  touchAfter: 7 * 24 * 60 * 60, // 1 week
};

remove useSession, applySession, and withSession in favor of a factory nextSession() method

// BEFORE
import { withSession, applySession, session } from "next-session";
withSession(handler, options);
applySession(req, res, options);
app.use(session(options));

// AFTER
import nextSession from "next-session";
const getSession = nextSession(options);

The returned getSession is almost the same with applySession, only without the options props. You can also re-implement withSession and session like below:

const getSession = nextSession(options);

function withSession(handler) {
  return async function handlerWithSession(req, res) {
    await getSession(req, res);
    return handler(req, res);
  };
}

async function session(req, res, next) {
  try {
    await getSession(req, res);
    next();
  } catch (err) {
    next(err);
  }
}

connect store requires promisification

In version v3.3.0, we remove the need to use promisifyStore with connect/express session store. After reconsideration on perf and reliability, this is added back.

Also, both promisifyStore and expressSession are moved to next-session/lib/compat for better bundle size if not used.

// BEFORE
import { expressSession } from "next-session";
const RedisStore = require("connect-redis")(expressSession);

const options = {
  store: new RedisStore(),
};

// AFTER
import { expressSession, promisifyStore } from "next-session/lib/compat";
const RedisStore = require("connect-redis")(expressSession);

const options = {
  store: promisifyStore(new RedisStore()),
};