Pages _routes.json file not respecting "exclude" URLs for SPA

What is the issue or error you’re encountering

Cloudflare Pages _routes.json config file is not respecting the URLs in the “exclude” list when using a multi-part catch all function and a Single Page Application

What steps have you taken to resolve the issue?

I have tried both on the local wrangler dev server and in production - same issue exists in both environments.

What are the steps to reproduce the issue?

Create a Cloudflare Pages project with the following directory structure:

project-root/
│
├── functions/
│   └── posts/
│       └── [[path]].js
│
├── dist/ (usually called "public" but our project uses "dist")
│   ├── index.html
│   ├── _redirects
│   ├── css/
│   ├── js/
│
└── _routes.json

The following _routes.json file contents:

{
  "version": 1,
  "include": ["/posts/*"],
  "exclude": ["/posts/new"]
  ]
}

And the following _redirects file contents:

/* /index.html 200

Then run your Pages project using either wrangler dev or in Cloudflare Pages production environment, and you will see that visiting /posts/new will run the posts/[[path.js]] function instead of loading the SPA located at index.html (as instructed to do so via the entry in the _redirects file).

Note that visiting something like /foo/bar will correctly load index.html.

Furthermore, removing the functions directory altogether (i.e. having no Functions at all), will also lead to the correct behaviour of loading the SPA via index.html.

I can see in the Cloudflare dashboard that Cloudflare decided to make its own “_routes.json” file and totally ignore mine:

Invocation routes

Invocation routes determine when your Functions script is executed. This file is generated based on the files present in the /functions directory.