0

We have a shortener Koa app that handles urls like https://my-shortener.com/<code> (or when developing: http://localhost:2009/aaa), and then redirects the user.

I made a simple Koa app to reproduce the behaviour: index.js

const port = process.env.PORT || 2009

const Koa = require('koa')
const router = require('koa-router')()

const app = new Koa()

// router.get('/foo/:param1', async (ctx) => {

router.get('/:param1', async (ctx) => {
  const { param1 } = ctx.params
  console.log('param1', param1)
  console.log('ctx.request', ctx.request)

  const redirectUrl = 'https://www.google.com'

  ctx.status = 302
  ctx.redirect(redirectUrl)
})

app.use(router.routes())
app.use(router.allowedMethods())

app.listen(port, () => {
  console.log('[INFO] App started running on port', port)
})



package.json

"dependencies": {
    "koa": "^2.13.0",
    "koa-router": "^9.1.0"
  },

When I enter the url (http://localhost:2009/aaa) in the browser (Chrome), it will call the route twice. Not always immediately, but if you do it a couple of times, it will consistently be called twice.

this is the console.log output:

[INFO] App started running on port 2009
param1 aa
ctx.request {
  method: 'GET',
  url: '/aa',
  header: {
    host: 'localhost:2009',
    connection: 'keep-alive',
    'sec-ch-ua': '"Not.A/Brand";v="8", "Chromium";v="114", "Google Chrome";v="114"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"macOS"',
    'upgrade-insecure-requests': '1',
    'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36',
    'sec-purpose': 'prefetch;prerender',
    purpose: 'prefetch',
    accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
    'sec-fetch-site': 'none',
    'sec-fetch-mode': 'navigate',
    'sec-fetch-user': '?1',
    'sec-fetch-dest': 'document',
    'accept-encoding': 'gzip, deflate, br',
    'accept-language': 'en-GB,en-US;q=0.9,en;q=0.8,nl;q=0.7'
  }
}
param1 aa
ctx.request {
  method: 'GET',
  url: '/aa',
  header: {
    host: 'localhost:2009',
    connection: 'keep-alive',
    'sec-ch-ua': '"Not.A/Brand";v="8", "Chromium";v="114", "Google Chrome";v="114"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"macOS"',
    'upgrade-insecure-requests': '1',
    'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36',
    accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
    'sec-fetch-site': 'none',
    'sec-fetch-mode': 'navigate',
    'sec-fetch-user': '?1',
    'sec-fetch-dest': 'document',
    'accept-encoding': 'gzip, deflate, br',
    'accept-language': 'en-GB,en-US;q=0.9,en;q=0.8,nl;q=0.7'
  }
}

the big difference between the calls is the header: purpose: 'prefetch', which seems to be an unofficial http header the chrome uses, but I couldn't find much documentation on it.

The problem is that we register these calls as events, like a count of how often a link is used, and this way the counts are not accurate because sometimes they are counted twice.

I am not sure what to make of this, is this expected behaviour? What would be a good workaround for this? Should I just check the header and if it is a 'prefetch' simply not fire the count event?

devboell
  • 1,180
  • 2
  • 16
  • 34

0 Answers0