fox
fox

Reputation: 167

Polyfill SameSite cookie attribute

Is there a way to polyfill the SameSite cookie attribute so I can use this feature with legacy browsers or older versions of a specific browser?

https://caniuse.com/#feat=same-site-cookie-attribute

Upvotes: 0

Views: 259

Answers (1)

fox
fox

Reputation: 167

I think I have to solve this in a reverse proxy:

const express = require('express');
const cp = require('cookie-parser');
const app = express();
app.use(cp());

app.get('/set', (req, res) => {
  // Set the new style cookie
  res.cookie('3pcookie', 'value', { sameSite: 'none', secure: true });
  // And set the same value in the legacy cookie
  res.cookie('3pcookie-legacy', 'value', { secure: true });
  res.end();
});

app.get('/', (req, res) => {
  let cookieVal = null;

  if (req.cookies['3pcookie']) {
    // check the new style cookie first
    cookieVal = req.cookies['3pcookie'];
  } else if (req.cookies['3pcookie-legacy']) {
    // otherwise fall back to the legacy cookie
    cookieVal = req.cookies['3pcookie-legacy'];
  }

  res.end();
});

app.listen(process.env.PORT);

see https://web.dev/samesite-cookie-recipes/

Upvotes: 1

Related Questions