Larvel 11 with assets, public/build files return 404
I am far from being a Laravel or Vercel expert, but it looks like the current Laravel example app is missing a few things when it comes to deploying the app on Vercel with assets (which are built with Vite).
The problem seems to be that the files from the build process are not available and cannot be requested (adding npm ci && npm run build to the composer.json scripts "vercel" does the build process perfectly):
public/build/assets/main-SWg09SIz.css 8.88 kB │ gzip: 2.55 kB
public/build/assets/app-BmrwFrBv.js
https://mydomain.com/build/assets/app-BmrwFrBv.js will result in a 404 no matter what i change in the vercel.json.
I tested several configurations in my vercel.json. It's also not clear to beginners like me when to use "functions" and when to use "builds."
"functions": {
"api/index.php": {
"runtime": "[email protected]"
}
},
"routes": [
{
"src": "/(favicon\\.ico|robots\\.txt)",
"dest": "/public/$1"
},
{
"src": "/build/(.*)",
"dest": "/public/build/$1"
},
{
"src": "/(.*)",
"dest": "/api/index.php"
}
]
I tested this: https://github.com/treckstar/vercel-laravel-10-starter/blob/main/vercel.json
and also this: https://github.com/juicyfx/vercel-examples/blob/master/php-laravel/vercel.json
(There is also a problem with HTTPS and the path to assets, but I fixed it by using an absolute path and the environment option ASSET_URL.)
@f3l1x could you mabye point me into the right direction? 🙏
This is my configuration in vercel.json for laravel 11, this might help.
{
"version": 2,
"framework": null,
"functions": {
"api/*.php": {
"runtime": "[email protected]"
}
},
"routes": [
{
"src": "/(.*\\.(?:css|js|png|jpg|jpeg|gif|svg|ico|ttf|woff|woff2|eot|otf|webp|avif|txt))$",
"dest": "/public/$1"
},
{
"src": "/(.*)",
"dest": "/api/index.php"
}
],
"env": {
"APP_ENV": "production",
"APP_DEBUG": "false",
"APP_URL": "https://app.bintangdeveloper.eu.org",
"APP_CONFIG_CACHE": "/tmp/config.php",
"APP_EVENTS_CACHE": "/tmp/events.php",
"APP_PACKAGES_CACHE": "/tmp/packages.php",
"APP_ROUTES_CACHE": "/tmp/routes.php",
"APP_SERVICES_CACHE": "/tmp/services.php",
"VIEW_COMPILED_PATH": "/tmp",
"CACHE_DRIVER": "array",
"LOG_CHANNEL": "stderr",
"SESSION_DRIVER": "cookie",
}
}
for anyone else: this works, also ensure to set "env" variables via the UI and not the vercel.json
{
"version": 2,
"regions": [
"fra1"
],
"functions": {
"api/index.php": {
"runtime": "[email protected]"
}
},
"routes": [
{
"src": "/build/(.*)",
"dest": "/build/$1"
},
{
"src": "/(.*\\.(?:css|js|png|jpg|jpeg|gif|svg|ico|ttf|woff|woff2|eot|otf|webp|avif|txt))$",
"dest": "/public/$1"
},
{
"src": "/(.*)",
"dest": "/api/index.php"
}
],
"outputDirectory": "public"
}
still woks?
@sencin yes, the above still works, i just recently deployd a laravel app to vercel using the above. Sometimes when new composer deps has been added, it makes some problems and its required to redeploy without cache, but everything else works.