Vapor: Serving Assets From The Root Domain

Sep, 23 2020#vapor

As you may know, during deployment Vapor automatically extracts all of the assets in your Laravel project's public directory and uploads them to S3. Also, Vapor creates an AWS CloudFront (CDN) distribution to distribute these assets efficiently around the world.

As convenient as that may be, some applications like PWAs may need to serve certain assets from the root domain. For example, JavaScript service workers must be served from the same origin as the referrer.

Starting today, you can now serve specific assets from the root domain. To get started, just head over to your application's config/vapor.php file, and add an array of assets that should be served in your root domain:

/*
|--------------------------------------------------------------------------
| Servable Assets
|--------------------------------------------------------------------------
|
| Here you can configure list of public assets that should be servable
| from your application's domain instead of only being servable via
| the public S3 "asset" bucket or the AWS CloudFront CDN network.
|
*/

'serve_assets' => [
    'serviceWorker.js',
],

At the time of this writing, and due to the serverless nature of applications powered by Vapor, assets served from the root domain are not cacheable at the client-side and they are served using Laravel routes. Therefore, you should only serve assets that absolutely must be served from the root domain as there is a slight performance penalty for doing so.

In addition to this new feature, you can now instruct Vapor to interpret the public directory's dot files as assets. For that, you just have to set the dot-files-as-assets key to true in your vapor.yml file:

id: 1
name: app-test
dot-files-as-assets: true

And, of course, you can opt to serve those dot files from the root domain as well:

'serve_assets' => [
    'serviceWorker.js',
    '.well-known/assetlinks.json',
],

Finally, keep in mind that those features are only available on the latest versions of vapor-cli, and vapor-core packages. Enjoy!

By Nuno Maduro

Software Engineer at Laravel.

Follow the RSS Feed.