<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Dominik</title>
    <description>The latest articles on DEV Community by Dominik (@domvo).</description>
    <link>https://dev.to/domvo</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1339644%2Fb92e016e-3b1c-4643-983a-e46a875dc1c0.jpeg</url>
      <title>DEV Community: Dominik</title>
      <link>https://dev.to/domvo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/domvo"/>
    <language>en</language>
    <item>
      <title>Static No More: Transforming Frontend Deployment with Dynamic Configuration</title>
      <dc:creator>Dominik</dc:creator>
      <pubDate>Sat, 09 Mar 2024 22:19:00 +0000</pubDate>
      <link>https://dev.to/domvo/static-no-more-transforming-frontend-deployment-with-dynamic-configuration-1hca</link>
      <guid>https://dev.to/domvo/static-no-more-transforming-frontend-deployment-with-dynamic-configuration-1hca</guid>
      <description>&lt;h1&gt;
  
  
  tldr;
&lt;/h1&gt;

&lt;p&gt;Leverage bundler tools and a runtime script to inject dynamic configurations into static frontend deployments, enabling a single build to adapt to multiple environments seamlessly.&lt;/p&gt;

&lt;h2&gt;
  
  
  Preface
&lt;/h2&gt;

&lt;p&gt;If you're serious about your DevOps practices, you likely aim for a &lt;strong&gt;build once, deploy anywhere approach&lt;/strong&gt;. The idea behind this approach is that you build your application artifact (e.g. a Docker image) once and with the tweaking of configuration parameters you can deploy it to multiple environments, such as staging and production. This approach is also described in the popular &lt;a href="https://12factor.net/config"&gt;Twelve Factor App methodology&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In your &lt;strong&gt;backend applications&lt;/strong&gt; it is quite easy to achieve that. You have a running process and you can simply access your environment variables at runtime. &lt;/p&gt;

&lt;p&gt;In &lt;strong&gt;static frontend applications&lt;/strong&gt; however, this is not as easy. You probably have some sort of web server that serves your pre-built files. There is no process environment you can access from your client-side JavaScript code. So how can you make sure in such cases to be able to deploy the same artifact to any environment?&lt;/p&gt;

&lt;h2&gt;
  
  
  Leverage your bundler's capabilities
&lt;/h2&gt;

&lt;p&gt;&lt;small&gt;The following passages are based on &lt;a href="https://vite.dev"&gt;Vite&lt;/a&gt;, but the concepts are applicable to other frontend JS tools as well.&lt;/small&gt;&lt;/p&gt;

&lt;p&gt;Vite allows for the &lt;a href="https://vitejs.dev/guide/env-and-mode.html"&gt;injection environment variables&lt;/a&gt; at build time. Environment variables prefixed with &lt;code&gt;VITE_&lt;/code&gt; are bundled with your application. These variables prefixed with &lt;code&gt;VITE_&lt;/code&gt; become accessible in your code as &lt;code&gt;import.meta.env.VITE_&amp;lt;VARIABLE_NAME&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If you let vite inject this .env file during the build process, the first two ones will be accessible via &lt;code&gt;import.meta.env.VITE_BACKEND_API_URL&lt;/code&gt; and &lt;code&gt;import.meta.env.VITE_GOOGLE_MAPS_KEY&lt;/code&gt;. &lt;code&gt;SOME_SECRET&lt;/code&gt; is not prefixed with &lt;code&gt;VITE_&lt;/code&gt;, so it will be disregarded.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;VITE_BACKEND_API_URL=https://example.com/api
VITE_GOOGLE_MAPS_KEY=12345qwerty
SOME_SECRET=mySecret
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Awareness of this capability is crucial for achieving our ultimate goal.&lt;/p&gt;

&lt;h2&gt;
  
  
  The final trick
&lt;/h2&gt;

&lt;p&gt;Rather than passing the actual values, we use placeholder values:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;VITE_BACKEND_API_URL=MY_APP_PREFIX_BACKEND_API_URL
VITE_GOOGLE_MAPS_KEY=MY_APP_PREFIX_GOOGLE_MAPS_KEY
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Just before starting the application, we run a script that replaces all occurrences of &lt;code&gt;MY_APP_PREFIX_&amp;lt;VARIABLE_NAME&amp;gt;&lt;/code&gt; in the bundle with the correct values. Here is a small bash script that you can run in your container right before starting the actual application. This script assumes that we're serving the files with an nginx web server from the standard directory &lt;code&gt;/usr/share/nginx/html&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/sh&lt;/span&gt;
&lt;span class="nv"&gt;RELEVANT_ENV_VARS&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;env&lt;/span&gt; | &lt;span class="nb"&gt;grep &lt;/span&gt;MY_APP_PREFIX&lt;span class="si"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;for &lt;/span&gt;i &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nv"&gt;$RELEVANT_ENV_VARS&lt;/span&gt;
&lt;span class="k"&gt;do
    &lt;/span&gt;&lt;span class="nv"&gt;ENV_VAR_NAME&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$i&lt;/span&gt; | &lt;span class="nb"&gt;cut&lt;/span&gt; &lt;span class="nt"&gt;-d&lt;/span&gt; &lt;span class="s1"&gt;'='&lt;/span&gt; &lt;span class="nt"&gt;-f&lt;/span&gt; 1&lt;span class="si"&gt;)&lt;/span&gt;
    &lt;span class="nv"&gt;ENV_VAR_VALUE&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$i&lt;/span&gt; | &lt;span class="nb"&gt;cut&lt;/span&gt; &lt;span class="nt"&gt;-d&lt;/span&gt; &lt;span class="s1"&gt;'='&lt;/span&gt; &lt;span class="nt"&gt;-f&lt;/span&gt; 2-&lt;span class="si"&gt;)&lt;/span&gt;

    find /usr/share/nginx/html &lt;span class="nt"&gt;-type&lt;/span&gt; f &lt;span class="nt"&gt;-name&lt;/span&gt; &lt;span class="s1"&gt;'*.js'&lt;/span&gt; &lt;span class="nt"&gt;-exec&lt;/span&gt; &lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="nt"&gt;-i&lt;/span&gt; &lt;span class="s2"&gt;"s|&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;ENV_VAR_NAME&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;|&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;ENV_VAR_VALUE&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;|g"&lt;/span&gt; &lt;span class="s1"&gt;'{}'&lt;/span&gt; +
&lt;span class="k"&gt;done&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's dissect this script line by line. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;RELEVANT_ENV_VARS=$(env | grep MY_APP_PREFIX)&lt;/code&gt; - we identify relevant environment variables by searching for those with the specified prefix.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;for i in $RELEVANT_ENV_VARS&lt;/code&gt; - we loop over each of the found variables.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;ENV_VAR_NAME=$(echo $i | cut -d '=' -f 1)&lt;/code&gt; - the left hand side of an environment variable declaration is the name. &lt;code&gt;cut&lt;/code&gt; is the equivalent of the widely available &lt;code&gt;split('=')&lt;/code&gt; function in other languages.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;ENV_VAR_VALUE=$(echo $i | cut -d '=' -f 2-)&lt;/code&gt; - the right hand side of an environment variable declaration is the value.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;find /usr/share/nginx/html -type f -name '*.js'&lt;/code&gt; - we search for all JavaScript files in the html folder

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;-exec sed -i "s|${key}|${value}|g" '{}' +&lt;/code&gt; - for all found files execute a &lt;code&gt;sed&lt;/code&gt; command that replaces all occurrences of the variable name with the variable value.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All you now have to do is run this script before serving your application and adding your environment variables to the execution context of your container. Here is a small &lt;code&gt;Dockerfile&lt;/code&gt; example. The example assumes you have a built bundle in &lt;code&gt;dist/my-app&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="s"&gt; nginx:alpine&lt;/span&gt;

&lt;span class="k"&gt;RUN &lt;/span&gt;apk add &lt;span class="nt"&gt;--no-cache&lt;/span&gt; bash

&lt;span class="k"&gt;WORKDIR&lt;/span&gt;&lt;span class="s"&gt; /usr/share/nginx/html&lt;/span&gt;

&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; dist/my-app .&lt;/span&gt;

&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; replace-envs.sh /usr/local/bin/replace-envs.sh&lt;/span&gt;

&lt;span class="k"&gt;EXPOSE&lt;/span&gt;&lt;span class="s"&gt; 8080&lt;/span&gt;

&lt;span class="k"&gt;CMD&lt;/span&gt;&lt;span class="s"&gt; /bin/bash -c "/usr/local/bin/replace-envs.sh &amp;amp;&amp;amp; exec nginx -g 'daemon off;'"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;This is it!&lt;/strong&gt; If you now run your container and pass the environment variables accordingly, they will be replaced before starting the actual nginx process. In a &lt;code&gt;docker-compose.yml&lt;/code&gt; this could look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;version&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;3'&lt;/span&gt;
&lt;span class="na"&gt;services&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;my-app&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;my-app&lt;/span&gt; &lt;span class="c1"&gt;# replace with your image&lt;/span&gt;
    &lt;span class="na"&gt;ports&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;8080:8080'&lt;/span&gt;
    &lt;span class="na"&gt;environment&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;MY_APP_PREFIX_BACKEND_API_URL=https://example.com/api&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;MY_APP_PREFIX_GOOGLE_MAPS_KEY=12345qwerty&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;By leveraging the ability of modern bundlers such as Vite to inject environment-specific variables and using a clever script to replace placeholders with actual environment values at runtime, developers can maintain a single artifact across multiple environments. In addition to streamlining the deployment process, this methodology aligns with the best practices outlined in the Twelve-Factor App Methodology, particularly with respect to app configuration. Implementing these practices ensures that your frontend deployments are as flexible and efficient as your backend, paving the way for a more robust and scalable application infrastructure.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sources
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://vite.dev"&gt;Vite&lt;/a&gt;, &lt;a href="https://docs.docker.com/"&gt;Docker&lt;/a&gt;, &lt;a href="https://12factor.net/"&gt;Twelve Factor Apps&lt;/a&gt;, &lt;a href="https://stackoverflow.com/a/77454537"&gt;Stack Overflow&lt;/a&gt;&lt;/p&gt;

</description>
      <category>docker</category>
      <category>vite</category>
      <category>webpack</category>
      <category>react</category>
    </item>
  </channel>
</rss>
