I hosted a Python serverless function using Flask. It works. The problem is that README.md, requirements.txt, and all other files in my root directory are being exposed as static assets. I confirmed my vercel.json
was the same as the one in the vercel flask example template.
To debug, I deployed the flask hello world template available on Vercel: Flask Hello World.
After some digging, I found .vercelignore
, which solves my problem. However, requirements.txt
is still exposed as a static asset. If I include requirements.txt
in .vercelignore
, the code fails to run complaining it cannot find the package flask
. After the initial deployment, I have been testing locally using vercel dev --debug --local-config vercel.json
.
How should I fix this?
Notably, using the legacy routes
property, the project builds successfully, and the internal files are not exposed as static assets. I’m looking for the current recommended approach.
After reading this: Configuring projects with vercel.json, I believe the reason for rewrites not working is precedence being given to the filesystem:
The
source
property should NOT be a file because precedence is given to the filesystem prior to rewrites being applied. Instead, you should rename your static file or Serverless Function.
Project structure:
.
├── README.md
├── api
│ └── index.py
├── requirements.txt
├── vercel.json
├── .vercelignore
└── .gitignore
vercel.json
{
"rewrites": [
{ "source": "/(.*)", "destination": "/api/index" }
]
}
Woking .vercelignore, exposing requirements.txt
# Ignore everything (folders and files) on root only
/*
!api
!vercel.json
!requirements.txt
.vercelignore without requirements.txt, breaking build:
# Ignore everything (folders and files) on root only
/*
!api
!vercel.json
vercel.json with legacy routes property works and returns 404 for all static assets. No need for .vercelignore in this case:
{
"routes": [
{ "src": "/(.*)", "dest": "/api/index" }
]
}
Relevant links browsed: