Skip to main content

Sharing Assets Between Environments

Matt Gray avatar
Written by Matt Gray
Updated over 2 weeks ago

When you connect to a Servd project's asset bucket, you'll likely notice that there's a directory per environment - `local/`, `staging/` and `production/`. Not only does this structure keep things nice and tidy, it also allows each environment to have an independent set of assets that can be edited without interfering with other environments. By default, the Servd plugin will detect a project's environment and generate assets URLs which include these environment directories in their paths, so following this convention minimises the initial configuration that's required.

The compromise with this approach is that having independent sets of assets uses a lot more storage than a single set, so costs more if the total takes you past your plan's asset storage limit. In which case, you'd need to use our Asset Platform Boost addon to raise the storage limit.

If your project has a lot of assets, this can potentially make the asset storage side of things quite expensive. Therefore the Servd plugin supports pulling assets from a single directory regardless of the environment. This can be configured using the Force Assets Environment Servd plugin setting, which looks like this. πŸ‘‡

It's empty by default, which means the Servd plugin will autodetect the environment. However if you set it to say, `production`, then all asset URLs will reference files in the `production/` asset bucket directory.

Caveats

If you set the Force Assets Environment setting to a specific environment then it's important to understand potential issues that may arise. The primary one being that environment assets are no longer independent, so deleting an asset via Craft's admin panel in one environment, will prevent it from being accessed by other environments.

A common issue this setting causes is when a local development environment's Force Assets Environment setting is set to `production`, and contains the same asset database data as the production environment. If a developer decides to delete an asset from their local Craft admin panel, then the record of it will be removed from their local database data and the asset file will be removed from the `production/` asset bucket directory. The production environment's database will still have a record of that particular asset, and if it attempts to render an uncached version of it, a 404 response will be returned. If the asset has already been cached by Servd's asset CDN, then it may continue to render fine for some time, but as soon as it is evicted the asset will start returning a 404 response.

To avoid these situations, we recommend employing a strict policy of only adding, updating or deleting assets in a single environment, and keeping other environment's asset database data synchronised with that particular environment.

If you do unfortunately fall foul of the above scenario, then restoring a backup in the relevant environment will reinstate the files in the environment's asset directory to as they were at the time the backup was captured.

Did this answer your question?