<?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: Nick.Hu</title>
    <description>The latest articles on DEV Community by Nick.Hu (@nickhu150).</description>
    <link>https://dev.to/nickhu150</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4235516%2F0f4f62c4-a145-41f7-8dc4-ad5c153e945d.png</url>
      <title>DEV Community: Nick.Hu</title>
      <link>https://dev.to/nickhu150</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nickhu150"/>
    <language>en</language>
    <item>
      <title>One Vite build, two runtimes: shipping a browser game to the open web and into an offline WebView</title>
      <dc:creator>Nick.Hu</dc:creator>
      <pubDate>Fri, 13 Nov 2026 06:10:46 +0000</pubDate>
      <link>https://dev.to/nickhu150/one-vite-build-two-runtimes-shipping-a-browser-game-to-the-open-web-and-into-an-offline-webview-249a</link>
      <guid>https://dev.to/nickhu150/one-vite-build-two-runtimes-shipping-a-browser-game-to-the-open-web-and-into-an-offline-webview-249a</guid>
      <description>&lt;p&gt;I shipped &lt;a href="https://apexdrawn.org/" rel="noopener noreferrer"&gt;ApexDrawn&lt;/a&gt; as a plain static site: you draw a closed loop, it becomes a racetrack, and then you drive it. Later I needed a second build target - a mobile shell that loads the same game from local assets with no network at all.&lt;/p&gt;

&lt;p&gt;The interesting part wasn't the game logic. It was that the whole app assumed a server: accounts, &lt;code&gt;/api/*&lt;/code&gt; for purchases and garage state, a WebSocket relay for races, analytics, payments. None of that can exist in an offline WebView. I didn't want a fork, so the split is one build-time flag and everything else follows from it.&lt;/p&gt;

&lt;h2&gt;
  
  
  One flag, two bundles
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// src/appMode.ts&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="no"&gt;OFFLINE_APP&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;meta&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;VITE_OFFLINE_APP&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s2"&gt;"true"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Because this is a compile-time constant, Rollup tree-shakes the unreachable branch. The offline bundle genuinely contains no auth SDK, no analytics and no checkout code - not "code that checks a boolean at runtime and returns early". That distinction matters when your CSP forbids all network egress: the easiest network call to forget is the one hidden behind a feature flag.&lt;/p&gt;

&lt;p&gt;The web build emits absolute &lt;code&gt;/assets/...&lt;/code&gt; URLs. The offline build is emitted with relative paths and copied under &lt;code&gt;assets/web/&lt;/code&gt; in the Flutter project, so its HTML references &lt;code&gt;./i18n.js&lt;/code&gt; and &lt;code&gt;assets/index-*.js&lt;/code&gt; instead.&lt;/p&gt;

&lt;h2&gt;
  
  
  Replace the session at the boundary, not in every component
&lt;/h2&gt;

&lt;p&gt;Components ask a context for &lt;code&gt;authLoaded&lt;/code&gt;, &lt;code&gt;signedIn&lt;/code&gt;, &lt;code&gt;getToken()&lt;/code&gt; and &lt;code&gt;user&lt;/code&gt;. The web provider wires that to an auth SDK. The offline provider is deliberately boring:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nc"&gt;OfflineSessionProvider&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;children&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;children&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ReactNode&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;AppSession&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;authLoaded&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;signedIn&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;getToken&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"local-only"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;userLoaded&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="cm"&gt;/* a local profile */&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;SessionContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Provider&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;children&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;SessionContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Provider&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;No component needs &lt;code&gt;if (offline)&lt;/code&gt;. That's the whole point: one boundary, two implementations. The same trick covers state. Web state lives behind &lt;code&gt;/api/*&lt;/code&gt;; offline state is a &lt;code&gt;localStorage&lt;/code&gt; record with a schema version in the key (&lt;code&gt;apexdrawn.offline-profile.v1&lt;/code&gt;) holding the balance, a ledger, owned items and tuning. Every read is defensive:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;raw&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;localStorage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;STORAGE_KEY&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;parsed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fallback&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;parsed&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;balance&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isFinite&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parsed&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;balance&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parsed&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;balance&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;ledger&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parsed&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ledger&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;parsed&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ledger&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If storage is unavailable or contains garbage, it falls back to an in-memory copy. A WebView can and will hand you a corrupted profile; treating local storage as a trusted typed object is how you get a crash on launch.&lt;/p&gt;

&lt;h2&gt;
  
  
  Relative paths are the actual hard part
&lt;/h2&gt;

&lt;p&gt;The site uses directory-style routes (&lt;code&gt;/garage/&lt;/code&gt;, &lt;code&gt;/free-racing-games/&lt;/code&gt;, &lt;code&gt;/es/...&lt;/code&gt;). On a normal host the server resolves those. Inside a WebView loading &lt;code&gt;assets/web/garage/index.html&lt;/code&gt;, a relative asset URL resolves relative to &lt;em&gt;that file&lt;/em&gt;, so the correct prefix depends on how deep the current page is.&lt;/p&gt;

&lt;p&gt;The fix is a small path helper that strips the &lt;code&gt;assets/web/&lt;/code&gt; prefix, counts the remaining segments, and builds the prefix from that depth:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;marker&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"/assets/web/"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;markerIndex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pathname&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;lastIndexOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;marker&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;suffix&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;markerIndex&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;pathname&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;markerIndex&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;marker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;pathname&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;segments&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;suffix&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Boolean&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;segments&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;at&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s2"&gt;"index.html"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;segments&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;rootPrefix&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"../"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;repeat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;segments&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Extensionless routes get normalized to &lt;code&gt;index.html&lt;/code&gt;, query strings are preserved, and &lt;code&gt;publicAsset()&lt;/code&gt; passes through &lt;code&gt;data:&lt;/code&gt;, &lt;code&gt;blob:&lt;/code&gt; and &lt;code&gt;https?:&lt;/code&gt; URLs untouched. Routing itself uses the same logic: &lt;code&gt;appPathParts()&lt;/code&gt; removes the WebView mount prefix so the app sees &lt;code&gt;/garage&lt;/code&gt; whether it's hosted at a domain root or six directories deep in an APK.&lt;/p&gt;

&lt;p&gt;Language selection had the same shape. On the web, the language is the first path segment:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;pathLanguage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;location&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pathname&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Boolean&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toLowerCase&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pathLanguage&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;localeByLanguage&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;pathLanguage&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Offline there is no meaningful path segment, so it reads a stored preference (&lt;code&gt;apexLang&lt;/code&gt;) and finally falls back to &lt;code&gt;document.documentElement.lang&lt;/code&gt;. Same public API, different source of truth.&lt;/p&gt;

&lt;h2&gt;
  
  
  Enforce offline with the platform, not with discipline
&lt;/h2&gt;

&lt;p&gt;The offline HTML injects a strict CSP before anything else:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;default-src 'self' data: blob:; connect-src 'self'; object-src 'none';
base-uri 'self'; form-action 'self'; navigate-to 'self'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It also short-circuits &lt;code&gt;fetch&lt;/code&gt;, &lt;code&gt;XMLHttpRequest&lt;/code&gt; and &lt;code&gt;sendBeacon&lt;/code&gt; for cross-origin or &lt;code&gt;/api|auth|oauth&lt;/code&gt; URLs, neuters &lt;code&gt;window.open&lt;/code&gt;, and cancels clicks on external links in the capture phase. This is belt-and-braces: even if a component accidentally tries to phone home, it can't.&lt;/p&gt;

&lt;p&gt;Native purchases go through an explicit bridge instead. The JS side posts a message to the host, listens for a &lt;code&gt;CustomEvent&lt;/code&gt;, and matches on a request id:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"apexdrawn:purchase-result"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;onResult&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;bridge&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;postMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"purchase"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;requestId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;packageId&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A 180-second timeout, a matching &lt;code&gt;requestId&lt;/code&gt;, and validation of &lt;code&gt;packageId&lt;/code&gt; plus &lt;code&gt;transactionId&lt;/code&gt; before resolving. An async bridge without those checks is a race condition waiting for a referee.&lt;/p&gt;

&lt;h2&gt;
  
  
  A third shape, almost free
&lt;/h2&gt;

&lt;p&gt;There are also ~49 static landing pages generated by a Node script from a template. For those, the script strips the main module bundle and modulepreload links and replaces them with a tiny &lt;code&gt;/info-page.js&lt;/code&gt; plus a stylesheet:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="n"&gt;html&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;html&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;\&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;*&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;script&lt;/span&gt; &lt;span class="n"&gt;defer&lt;/span&gt; &lt;span class="n"&gt;src&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"\/i18n\.js"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;lt;&lt;/span&gt;\&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;script&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;/&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="s1"&gt;'&amp;lt;script defer src="/info-page.js"&amp;gt;&amp;lt;/script&amp;gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;html&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;html&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;\&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;*&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;script&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"module"&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;^&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;src&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"\/assets\/[^\"]+\.js"&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;^&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;*&amp;gt;&amp;lt;&lt;/span&gt;\&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;script&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;/&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Those pages are content, not an app. Shipping a full SPA bundle to them would be pure cost. It's the same lesson as the offline build in miniature: decide what a page actually needs, then remove the rest.&lt;/p&gt;

&lt;h2&gt;
  
  
  What this costs
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Every networked feature needs two implementations.&lt;/strong&gt; That's a real ongoing tax, not a one-time setup.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The build matrix is easy to get wrong.&lt;/strong&gt; I once nearly deployed the offline &lt;code&gt;dist/&lt;/code&gt; to the live site; the analytics code had been tree-shaken out, so the site would have quietly stopped reporting. Build flags belong in CI, not in someone's memory.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Offline state doesn't sync.&lt;/strong&gt; Local coins, unlocks and tuning are device-local by definition. That's a product decision, not a technical bug.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Store rules differ.&lt;/strong&gt; An offline in-app purchase goes through the platform's billing, with its own review and receipt semantics.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The upside is one set of game logic and one place to fix a bug. Keep the differences at a handful of explicit boundaries (session, state, asset paths, network), and the rest of the codebase never learns that there are two runtimes.&lt;/p&gt;

&lt;p&gt;The playable web build is at &lt;a href="https://apexdrawn.org/" rel="noopener noreferrer"&gt;https://apexdrawn.org/&lt;/a&gt; - no download or account needed. If you've shipped a Vite app inside a WebView, I'd like to hear which boundary caused you the most trouble.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>react</category>
      <category>typescript</category>
      <category>flutter</category>
    </item>
    <item>
      <title>Shipping a browser game as a static site (and why game portals want something else)</title>
      <dc:creator>Nick.Hu</dc:creator>
      <pubDate>Thu, 12 Nov 2026 11:32:08 +0000</pubDate>
      <link>https://dev.to/nickhu150/shipping-a-browser-game-as-a-static-site-and-why-game-portals-want-something-else-261p</link>
      <guid>https://dev.to/nickhu150/shipping-a-browser-game-as-a-static-site-and-why-game-portals-want-something-else-261p</guid>
      <description>&lt;p&gt;I built a racing game where you draw the circuit yourself. It runs at &lt;a href="https://apexdrawn.org/" rel="noopener noreferrer"&gt;https://apexdrawn.org/&lt;/a&gt; with no download and no launcher. That decision shaped the whole repo, and it is also the reason I cannot just zip the build and upload it to a game portal.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the repo actually is
&lt;/h2&gt;

&lt;p&gt;A Vite + React + TypeScript static site. The build script is three steps:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="nl"&gt;"build"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"tsc -b &amp;amp;&amp;amp; vite build &amp;amp;&amp;amp; node scripts/generate-seo-pages.mjs"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That last step matters more than it looks. The site serves English plus Spanish, Italian, French and German pages, so the localization output is generated at build time rather than at runtime. Deploying is just shipping the &lt;code&gt;dist&lt;/code&gt; directory to Cloudflare Pages.&lt;/p&gt;

&lt;p&gt;Behind it there is a Worker for &lt;code&gt;/api/*&lt;/code&gt;, a WebSocket relay at &lt;code&gt;/r&lt;/code&gt;, Clerk for auth, and D1 migrations. The game itself lives as its own HTML document rather than as a React component tree. That split is deliberate: the marketing/site layer and the playable layer have completely different release cycles.&lt;/p&gt;

&lt;h2&gt;
  
  
  The part nobody warns you about
&lt;/h2&gt;

&lt;p&gt;Game portals want an uploadable HTML5 package: a zip with &lt;code&gt;index.html&lt;/code&gt; at the archive root, runnable inside their iframe. A normal Vite build does not give you that.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Absolute asset paths.&lt;/strong&gt; Vite defaults to &lt;code&gt;base: /&lt;/code&gt;, so &lt;code&gt;dist/index.html&lt;/code&gt; references &lt;code&gt;/assets/index-....js&lt;/code&gt;. A portal hosts your game on a subdirectory or its own CDN path, and those absolute URLs die. You need a separate build with &lt;code&gt;--base ./&lt;/code&gt; so the emitted paths are relative.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Same-origin assumptions.&lt;/strong&gt; The playable document talks to &lt;code&gt;/api/*&lt;/code&gt;, opens a WebSocket to &lt;code&gt;/r&lt;/code&gt;, and initialises auth. Inside a third-party iframe, every one of those becomes a cross-origin problem. Cookies, CORS and the WebSocket handshake all behave differently from the origin you developed against.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Domain locking.&lt;/strong&gt; My game HTML checks the hostname against an allowlist and redirects anywhere else. That is a reasonable protection for a self-hosted product and a fatal bug for a portal build. It has to become a build-time flag, not a runtime check.&lt;/p&gt;

&lt;p&gt;I found all three while trying to publish a Game Jolt page. The form is happy to take a title, cover image, description and Website URL, but the Publish button stays disabled until a browser build is uploaded to the release. That is the platform telling you it wants a distributable artifact, not a link.&lt;/p&gt;

&lt;h2&gt;
  
  
  The trade I made
&lt;/h2&gt;

&lt;p&gt;Browser-first means the shortest possible path from curiosity to playing: open a URL, draw a loop, race it. It also means I get static hosting, generated localized pages and normal web analytics. Packaging for portals would mean a second build target, a different auth story, and a release process for every platform.&lt;/p&gt;

&lt;p&gt;Those are two different products. I am treating portal distribution as a separate project instead of pretending the static site can be zipped into it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Practical checklist if you are doing both
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Add a dedicated build target with relative asset paths; do not reuse your production &lt;code&gt;dist&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Verify &lt;code&gt;index.html&lt;/code&gt; sits at the zip root and has no absolute &lt;code&gt;/assets/&lt;/code&gt; references.&lt;/li&gt;
&lt;li&gt;Replace hostname allowlists with a compile-time flag for embedded builds.&lt;/li&gt;
&lt;li&gt;Decide early whether the embedded build needs auth, an API or a relay. If it does, budget for CORS and cookie work. If it does not, strip those calls out of the embedded build entirely.&lt;/li&gt;
&lt;li&gt;Keep the Website field on your portal page pointing at your canonical domain. It is the one link that survives every platform's packaging rules.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;ApexDrawn is free to play at &lt;a href="https://apexdrawn.org/" rel="noopener noreferrer"&gt;https://apexdrawn.org/&lt;/a&gt; . If you have shipped a static web game into a portal iframe, I would like to hear which of the above bit you first.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>gamedev</category>
      <category>javascript</category>
      <category>typescript</category>
    </item>
    <item>
      <title>The TCP three-way handshake, packet by packet</title>
      <dc:creator>Nick.Hu</dc:creator>
      <pubDate>Mon, 09 Nov 2026 16:25:40 +0000</pubDate>
      <link>https://dev.to/nickhu150/the-tcp-three-way-handshake-packet-by-packet-3l5k</link>
      <guid>https://dev.to/nickhu150/the-tcp-three-way-handshake-packet-by-packet-3l5k</guid>
      <description>&lt;p&gt;Every networking course shows the same diagram: client, server, three arrows, "connection established". It is accurate and completely unhelpful the first time a handshake fails in production, because the diagram hides the two things you actually need — what each packet carries, and what happens when one of them never arrives.&lt;/p&gt;

&lt;p&gt;This is the handshake at packet level, with the fields that matter and the failure modes that show up in real captures.&lt;/p&gt;

&lt;h2&gt;
  
  
  Packet 1 — SYN
&lt;/h2&gt;

&lt;p&gt;The client picks a random initial sequence number (ISN) and sends a segment with the SYN flag set and no payload.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Flags: SYN
Seq:   1043 (relative), random 32-bit absolute value
Window: 64240
Options: MSS=1460, SACK permitted, Timestamps, Window scale=7
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Three things here are easy to miss.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The ISN is not zero.&lt;/strong&gt; On the wire it is a random 32-bit number, chosen per RFC 6528 so that an attacker cannot guess it and inject segments. Capture tools show a &lt;em&gt;relative&lt;/em&gt; sequence number starting at 0 because absolute numbers are unreadable; the relative view is a display convenience, not the protocol.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;SYN consumes a sequence number.&lt;/strong&gt; The first byte of actual data will be ISN+1. This matters later when you compute acknowledgement numbers by hand and are off by one.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Most of the negotiation already happened.&lt;/strong&gt; MSS, window scaling, SACK and timestamps are all offered in this first packet. If the server disagrees about any of them, the disagreement is visible in packet 2, not later.&lt;/p&gt;

&lt;h2&gt;
  
  
  Packet 2 — SYN-ACK
&lt;/h2&gt;

&lt;p&gt;The server answers with its own SYN and an acknowledgement of the client's.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Flags: SYN, ACK
Seq:   8830 (server ISN)
Ack:   1044 (client ISN + 1)
Options: MSS=1460, SACK permitted, Timestamps, Window scale=7
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;Ack: 1044&lt;/code&gt; is the load-bearing detail. It says "I received your SYN, and the next byte I expect from you is 1044" — which is the client's ISN plus one, because the SYN itself occupied a sequence number.&lt;/p&gt;

&lt;p&gt;If the server's MSS comes back as something like 1360 instead of 1460, you have found a tunnel or a middlebox clamping MSS. That single number explains a whole class of "large responses hang, small ones work" bugs.&lt;/p&gt;

&lt;h2&gt;
  
  
  Packet 3 — ACK
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Flags: ACK
Seq:   1044
Ack:   8831 (server ISN + 1)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;No payload, usually. The connection is now ESTABLISHED on both sides. The third packet is cheap — which is exactly why the handshake costs one round trip and not two.&lt;/p&gt;

&lt;p&gt;There is a detail worth knowing: the client may put data in this third packet. That is not a separate behaviour, just allowed by the protocol, and it is why some captures show a POST sitting inside the final ACK.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the handshake actually buys
&lt;/h2&gt;

&lt;p&gt;Three guarantees, in order:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;The client can reach the server&lt;/strong&gt; (packet 1 arrived).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The server can reach the client&lt;/strong&gt; (packet 2 arrived — the server learned the client's address works in both directions, and the client learns the server is listening).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Both agree on sequence numbers&lt;/strong&gt;, so retransmission, ordering and duplicate detection all work.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Notice what it does &lt;em&gt;not&lt;/em&gt; buy: any authentication, any encryption, any proof that the server is the machine you meant. That is TLS's job, and it starts only after this finishes — which is why an HTTPS connection costs one RTT for TCP and one or two more for TLS.&lt;/p&gt;

&lt;h2&gt;
  
  
  When it goes wrong
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;SYN with no SYN-ACK.&lt;/strong&gt; Either the server is not listening (you get RST instead), a firewall is dropping silently, or the route is broken. A silent drop and a listen-less port look completely different: RST comes back in milliseconds, silence takes a retransmission timeout.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;SYN-ACK with no ACK.&lt;/strong&gt; The server's kernel has a half-open connection sitting in its SYN queue, and it will retransmit the SYN-ACK on a timer (roughly 1s, 2s, 4s…) until it gives up. On the client side this usually means the final ACK was lost or the client vanished. On the server side, a pile of these is what a SYN flood looks like before SYN cookies are enabled.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Retransmitted SYN at 1s, 3s, 7s.&lt;/strong&gt; That spacing is not arbitrary — initial RTO of 1 second, doubled on each failure, per RFC 6298. If you see 1s, 2s, 4s instead, you are looking at a different stack or a middlebox generating its own retransmits.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Simultaneous open.&lt;/strong&gt; Both sides send SYN at the same time, both send SYN-ACK, and the connection establishes anyway. Rare, legal, and confusing the first time you see it in a capture.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;TCP Fast Open.&lt;/strong&gt; With a TFO cookie from a previous connection, the client can put data in the SYN itself, saving a round trip. It failed to deploy widely for reasons that are more about middleboxes than about the protocol.&lt;/p&gt;

&lt;h2&gt;
  
  
  Reading the timing
&lt;/h2&gt;

&lt;p&gt;The gap between packet 1 and packet 2 is the network RTT plus the server's SYN processing time. The gap between 2 and 3 is one-way latency plus client processing.&lt;/p&gt;

&lt;p&gt;If packet 1→2 is 200ms and 2→3 is 2ms, you have a distant server and a fast client. If 2→3 is also ~100ms, that is normal one-way delay, not a problem. What you are looking for is asymmetry: a 300ms gap on one leg and 5ms on the other means something in the middle is holding packets — a proxy, a load balancer mid-handshake, or a retransmission that your capture tool has labelled but you have not noticed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why the diagram fails
&lt;/h2&gt;

&lt;p&gt;Three reasons, and they are all about scale.&lt;/p&gt;

&lt;p&gt;It draws two hosts. Real paths have a router, a NAT, sometimes a TLS-terminating proxy that answers the SYN itself and opens a &lt;em&gt;second&lt;/em&gt; handshake to the origin. That is why you sometimes see two handshakes in one capture and assume something is wrong.&lt;/p&gt;

&lt;p&gt;It draws uniform latency. Real RTTs drift, and the handshake is where you notice first, because three packets give you two intervals to compare.&lt;/p&gt;

&lt;p&gt;It draws the fields as labels. The fields are numbers with consequences — a window scale of 0 disables scaling entirely, an MSS of 536 means something ancient is in the path, a timestamp of 0 disables PAWS.&lt;/p&gt;

&lt;p&gt;The handshake is short enough to hold in your head. That is exactly why it is worth reading once at byte level rather than at arrow level: after that, every capture you look at has a story you can actually follow.&lt;/p&gt;




&lt;p&gt;If you want to step through it interactively — every field decoded, including the options block — I built a packet-by-packet visualiser for this exchange and 32 other protocols: &lt;a href="https://protocolexplorer.run/protocols/tcp/" rel="noopener noreferrer"&gt;Protocol Explorer, TCP handshake&lt;/a&gt;. The TCP and HTTP lessons are free.&lt;/p&gt;

</description>
      <category>networking</category>
      <category>tcp</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
