vercel/serve-handler

Don't understand the docs! How do I redirect all '/default/assets' to 'public/default/assets'?

blaasvaer opened this issue · 2 comments

Due to running a frontend project on multiple backend systems and in need of consistency I need to redirect all incoming requests that contain the part "/assets/" in the url to "public/default/assets/".

I tried:

…
} else if ( request_url.pathname.split('/').includes( 'assets' ) ) {
	let static_options = {
		"redirects" : [
			{
				"source" : "/default/assets", "destination" : "public/default/assets"
			}
		]
	};

	statics( req, res, static_options );
			
	return;
} else {
…

Am I missing some parts in the string parts of the options (**/*.*) or the like?

Now, I don't get what the redirects part of the docs actually do, other that redirecting 'from' SOMETHING 'to' SOMETHING ELSE. But what are these parts actually meaning? Are they request.url parts or what?

I just want all requests that contain '/assets/' redirect to a different folder leaving the rest of the request intact ... I'm not sure the above does that.

And by the way: I have trial and errored the hell out of it – trying all sorts of values (path and wildcards) for the 'source' and 'destination' ... I've completely run out of ideas.

Getting 'creative' and trying this:

const source = request_url.pathname;
const destination = `public/${source}`;

let static_options = {
	"redirects" : [
		{
			"source" : source, "destination" : destination
		}
	]
};

statics( req, res, static_options );

Produces this output on the server(?):

STATIC url request_url.pathname /default/assets/css/platform.min.css
STATIC url request_url.pathname.split('/').includes( 'assets' ) true
STATIC url request_url.pathname /public/default/assets/css/platform.min.css
STATIC url request_url.pathname.split('/').includes( 'assets' ) true
STATIC url request_url.pathname /public/public/default/assets/css/platform.min.css
STATIC url request_url.pathname.split('/').includes( 'assets' ) true
STATIC url request_url.pathname /public/public/public/default/assets/css/platform.min.css
STATIC url request_url.pathname.split('/').includes( 'assets' ) true
STATIC url request_url.pathname /public/public/public/public/default/assets/css/platform.min.css
STATIC url request_url.pathname.split('/').includes( 'assets' ) true
STATIC url request_url.pathname /public/public/public/public/public/default/assets/css/platform.min.css
STATIC url request_url.pathname.split('/').includes( 'assets' ) true
STATIC url request_url.pathname /public/public/public/public/public/public/default/assets/css/platform.min.css
STATIC url request_url.pathname.split('/').includes( 'assets' ) true
STATIC url request_url.pathname /public/public/public/public/public/public/public/default/assets/css/platform.min.css
STATIC url request_url.pathname.split('/').includes( 'assets' ) true
STATIC url request_url.pathname /public/public/public/public/public/public/public/public/default/assets/css/platform.min.css
STATIC url request_url.pathname.split('/').includes( 'assets' ) true
STATIC url request_url.pathname /public/public/public/public/public/public/public/public/public/default/assets/css/platform.min.css
STATIC url request_url.pathname.split('/').includes( 'assets' ) true
STATIC url request_url.pathname /public/public/public/public/public/public/public/public/public/public/default/assets/css/platform.min.css
STATIC url request_url.pathname.split('/').includes( 'assets' ) true
STATIC url request_url.pathname /public/public/public/public/public/public/public/public/public/public/public/default/assets/css/platform.min.css
STATIC url request_url.pathname.split('/').includes( 'assets' ) true
STATIC url request_url.pathname /public/public/public/public/public/public/public/public/public/public/public/public/default/assets/css/platform.min.css
STATIC url request_url.pathname.split('/').includes( 'assets' ) true
STATIC url request_url.pathname /public/public/public/public/public/public/public/public/public/public/public/public/public/default/assets/css/platform.min.css
STATIC url request_url.pathname.split('/').includes( 'assets' ) true
STATIC url request_url.pathname /public/public/public/public/public/public/public/public/public/public/public/public/public/public/default/assets/css/platform.min.css
STATIC url request_url.pathname.split('/').includes( 'assets' ) true
STATIC url request_url.pathname /public/public/public/public/public/public/public/public/public/public/public/public/public/public/public/default/assets/css/platform.min.css

etc. etc. etc.

I have absolutely no clue what's going on with this module. Anyone able to shed some light on this?

Well, never mind. Switched to serve-static module ... a breeze.