What
I've ran into an issue where I thought Vite and NextJS weren't exporting files correctly. The browser was throwing exceptions when loading javascript resources.
After lots of debugging, I figured that the site only breaks whilst I was running it in the static -webserver. That got my suspicion going with the environment variable replacement.
Diagnosis
This container replaces all variables with the following pattern: ${MY_ENVIRONMENT_VARIABLE}
. So I started searching for ${
in my static exported javascript files. It might not be a surprise anymore that modern javascript can use variables in backtick strings:
const string = `a string with a ${variable}`;
In recent exports, backtick strings became supported in all browsers, and this this is now valid browser compatible javascript.
Another finding was that empty variables also got replaced. So ${}
was just replaced with nothing.
Desired solution
We might want to release a new version with different environment variable replacement. Options are:
- Only replace environment variables with a specific prefix, and put those as a filter in the
envsubst
command
- Find a different replacement method, and use
![[MY_ENVIRONMENT_VARIABLE]]
or something like that
- Add a variable that lists all the environment variables that should be replaced (whitelist)
My workaround
How I solved it now in my personal project, was by overwriting 40-envsubst-on-www.sh
in the dockerfile, putting:
# Only replace variables in .html, .xml and .txt files
find "$www_dir" -regex '.*\.\(html\|xml\|txt\)$' -type f -exec sh -c 'envsubst < $0 > $0.tmp && mv $0.tmp $0' {} \;