We’re deploying a static site that will serve a different file depending on whether it’s prod or dev.
You will need:
- A Platform.sh project
- Git access
We have a static site that consumes an outside API from the browser.
This API has different keys for prod and dev mode.
How can a static site serve different files per environment?
1. Add your key files to the repository
$ touch keys/dev.json $ touch keys/prod.json
2. Add a writable path
Supposing your site is served from the
web directory, we’ll add a writable path underneath it to host our symlink.
Add the mount in your
mounts: 'web/keys': source: local source_path: keys
3. Symlink the changed files at deploy time
Now that we have our key files in the repository and a writable path available, we can use our deploy hook to create a symlink to the appropriate file:
hooks: deploy: | set -e if [ "$PLATFORM_BRANCH" = "master" ] ; then ln -sfv ~/keys/prod.json web/keys/key.json else ln -sfv ~/keys/dev.json web/keys/key.json fi
Now we can serve different content to the browser per-environment without changing the static nature of our site.