Worker - Redirecting /* to /ajax/libs/*

ExpertTip
#21

@matteo Cloudflare engineering replied to the ticket and suggested that instead I put the latency onto the requests for resources at root and prioritise checking if the request exists at /ajax/libs and if it doesn’t, assuming the request is actually for a root file.

Does this code look right to achieve that?

addEventListener('fetch', event => {
  event.respondWith(fetchAndApply(event.request))
})

async function fetchAndApply(request) {
  const requestURL = new URL(request.url)
  if(!requestURL.pathname.startsWith('/ajax/libs')) {
    requestURL.pathname = '/ajax/libs' + requestURL.pathname
    const newResponse = await fetch(requestURL, request)
    if(newResponse.status == 200) return newResponse
  }
  return fetch(request)
}
0 Likes

#22

That depends on the usage, if that makes sense it can be a good idea.

Looks good to me.

2 Likes

#23

Brilliant, thank you for the help once again. Workers is something absolutely new to me so the community is always amazing to have around to help :slight_smile:

2 Likes

#24

Just to give you an update, got the stats for Feb from Cf and 5 billion was right on the mark :tada:
141 billion in total for Feb :sunglasses:

2 Likes

#25

Thanks for getting back to me! That is an impressive amount of traffic. Good job! Is the worker ok?

1 Like

#26

I would assume the code is okay, engineering had no further comments regarding it.

They’re still working on getting it deployed as they need elevated internal access to do it, or something like that.

1 Like

#27

Understandable that not everyone can mess with configurations on Cloudflare’s main domain.

1 Like

#28

Yeah indeed! It seems the process for us getting stats is much faster now which is cool. Thank you for the help once again! I might remember to post another update here once we get it deployed :slight_smile:

3 Likes