I created a pages project for proofing of concept.
I would like functions to only be trigged when accessed through the top-level path, which is handled by function /functions/[catchall].js
For examples:
https://XXXX.pages.dev/anywords
https://XXXX.pages.dev/any-words
The result is work as expected. I can my code of [catchall].js trigged.
[catchall].js code is below
export function onRequest(context) {
let result = "this is first level path: \n";
result += "url: " + context.request.url + "\n";
for (const header of context.request.headers) {
result += "header: " + header + "\n";
}
return new Response(result);
}
Beyond the top-level path will be immediately redirected to ‘404.html’ without invoking any functions, potentially incurring increased costs due to unwanted traffic.
For examples:
https://XXXX.pages.dev/anywords/unexcepted
https://XXXX.pages.dev/any-words/unexcepted/
https://XXXX.pages.dev/any-words/unexcepted/asdasdasdas
The result is also showing 404 as expected.
But I can see the total requests is increased in the Function metrics.
My question is how to use _routes.json to handle only top-level path.
Could someone assist me in achieving this? Thank you very much.