KV Worker SPA + API

I’m running an SPA site using Cloudflare KV Store, it is matching all routes (domain/*)

but i need to let ‘/spa’ pass to domain dns, it is possible to do it using cloudflare?

The easiest way is to use the URL constructor to get the pathname from the Request.

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

const handleRequest = async (event) => {
	const {request} = event;
	const url = new URL(request.url)
	const pathnameArr = url.pathname.split('/').filter(i => i) || ''
	
	//calls the origin server
	if(pathnameArr[0] === 'spa')
	{
		return await fetch(request)
	}
	else
	{
		return new Response('your code goes here')
	}
}
1 Like

i need to use worker for api too? i’m afraid i will get more than 100k requests (max of cloudflare worker).

It is better to add spa files to my server and use nginx, right?

@diego.braga.guedes how are you implementing this application with Workers KV?

From the API Endpoint? https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/storage/kv/namespaces

Or from a Worker?

I’m using wrangler and VueJS

Yes, once the worker is assigned to the root domain/* you can´t bypass a specific pathname using DNS configuration.

Why not subscribing to a Bundled plan?

thank you, i will just add files on my sever, i think it is better than using KV Worker now. Thanks for the support