<?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: cometa-kaito</title>
    <description>The latest articles on DEV Community by cometa-kaito (@cometakaito).</description>
    <link>https://dev.to/cometakaito</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%2F4191440%2F4f7e179f-b8fa-446c-899e-22434cd183e3.png</url>
      <title>DEV Community: cometa-kaito</title>
      <link>https://dev.to/cometakaito</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/cometakaito"/>
    <language>en</language>
    <item>
      <title>Remounting a Form by key Resets Uncontrolled Inputs</title>
      <dc:creator>cometa-kaito</dc:creator>
      <pubDate>Fri, 27 Nov 2026 23:22:57 +0000</pubDate>
      <link>https://dev.to/cometakaito/remounting-a-form-by-key-resets-uncontrolled-inputs-2o89</link>
      <guid>https://dev.to/cometakaito/remounting-a-form-by-key-resets-uncontrolled-inputs-2o89</guid>
      <description>&lt;h2&gt;
  
  
  The short version
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;With an implementation that remounts the form on every submit, as &lt;code&gt;&amp;lt;form key={formGen}&amp;gt;&lt;/code&gt; does, uncontrolled inputs given only &lt;code&gt;defaultValue&lt;/code&gt; reset along with it — even on the save immediately after a validation error sent you back. What is on screen is the value you re-entered; what gets saved is the post-remount default.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What it looks like
&lt;/h2&gt;

&lt;p&gt;On the ad-slot create/edit form, someone created a slot intending capacity 5 and what was actually saved was capacity 1. The audit log shows "create capacity 1 → update capacity 5 one minute later", i.e. corrected by hand in an edit right after creation. Nothing was harmed because no bookings had come in, but the slot was published, so until it was corrected it was displayed as "1 seat left".&lt;/p&gt;

&lt;p&gt;This form is shared between create and edit and submits through React 19's &lt;code&gt;&amp;lt;form action={fn}&amp;gt;&lt;/code&gt;. The capacity input is &lt;code&gt;type="number"&lt;/code&gt; with &lt;code&gt;min={1}&lt;/code&gt; and no upper bound, and the DB constraint is only &lt;code&gt;capacity &amp;gt;= 1&lt;/code&gt;. Two mechanisms could make capacity unintentionally small: one a known issue where the wheel over a focused number field rolls it down to &lt;code&gt;min&lt;/code&gt;, the other the subject here — &lt;strong&gt;an uncontrolled input reset by the form remount&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why
&lt;/h2&gt;

&lt;p&gt;The form was designed to be rebuilt entirely, with a changing key, on every submit.&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="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;submit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;formData&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;FormData&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;isEdit&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="nf"&gt;updateLoop&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;formData&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;addLoop&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;formData&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="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&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="nf"&gt;setState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&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;r&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ok&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;isEdit&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nf"&gt;setSchoolId&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;setScopeType&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"school"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nf"&gt;setExtraSchoolIds&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="nf"&gt;setMonitorIds&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;span class="nf"&gt;setFormGen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;g&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="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;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;form&lt;/span&gt;
    &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;formGen&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;ref&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;formRef&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;action&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;submit&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;&lt;code&gt;setFormGen((g) =&amp;gt; g + 1)&lt;/code&gt; sits outside &lt;code&gt;r?.ok&lt;/code&gt;, so &lt;strong&gt;it always runs, whether the submit succeeded or failed.&lt;/strong&gt; Every time &lt;code&gt;key&lt;/code&gt; changes, React discards this &lt;code&gt;&amp;lt;form&amp;gt;&lt;/code&gt;'s DOM along with the old tree and mounts it again as a new one. That design exists to cope with React 19's &lt;code&gt;&amp;lt;form action={fn}&amp;gt;&lt;/code&gt; behaviour of "resetting uncontrolled inputs to &lt;code&gt;defaultValue&lt;/code&gt; on submit": selections held in &lt;code&gt;state&lt;/code&gt; such as school, scope and monitors are not subject to that automatic reset, so the key remount rebuilt the DOM to match them.&lt;/p&gt;

&lt;p&gt;The problem was that inputs like capacity passed their initial value with &lt;code&gt;defaultValue&lt;/code&gt; rather than &lt;code&gt;value&lt;/code&gt;.&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt;
  &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"capacity"&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;"number"&lt;/span&gt;
  &lt;span class="n"&gt;min&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="n"&gt;defaultValue&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;loop&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;capacity&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="n"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An input given only &lt;code&gt;defaultValue&lt;/code&gt; is "uncontrolled" as far as React is concerned, and its current DOM value is outside React's management. &lt;strong&gt;A remount is an operation that builds the DOM afresh, so an uncontrolled input starts again from &lt;code&gt;defaultValue&lt;/code&gt; every time.&lt;/strong&gt; Even when &lt;code&gt;addLoop&lt;/code&gt; / &lt;code&gt;updateLoop&lt;/code&gt; return a failure on a validation error, &lt;code&gt;setFormGen&lt;/code&gt; runs all the same, the form remounts, and the capacity just entered on screen is gone, back at &lt;code&gt;defaultValue&lt;/code&gt;. &lt;strong&gt;Since only an error message appears and the inputs are not blanked, it is hard to notice that the value has gone back to the default.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The preset button was affected through the same path.&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="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;applyPreset&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;kind&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"gakka"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="s2"&gt;"shinro"&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="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;formRef&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;current&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="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;f&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="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;set&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&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="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;el&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;elements&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;namedItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&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;el&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="nc"&gt;HTMLInputElement&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;el&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="nc"&gt;HTMLSelectElement&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;el&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;v&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;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;kind&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s2"&gt;"gakka"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"capacity"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"5"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"term_fee"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"200000"&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;The preset took DOM elements straight out of &lt;code&gt;formRef.current.elements&lt;/code&gt; and rewrote them with &lt;code&gt;el.value = v&lt;/code&gt;. That is direct DOM manipulation bypassing React &lt;code&gt;state&lt;/code&gt;, and it too returns to &lt;code&gt;defaultValue&lt;/code&gt; on the next remount. Press the preset and submit immediately and nothing surfaces; save after being sent back once by a validation error, and the preset's values are flattened to the defaults as well.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fixing it
&lt;/h2&gt;

&lt;p&gt;We changed the eight fields the preset touches — capacity, seconds, price, term and so on — from uncontrolled &lt;code&gt;defaultValue&lt;/code&gt; inputs to controlled &lt;code&gt;value&lt;/code&gt; + &lt;code&gt;onChange&lt;/code&gt;.&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="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;plan&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;setPlan&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;planKind&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;loop&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;planKind&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="n"&gt;capacity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;loop&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;capacity&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;slotSeconds&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;loop&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;slotSeconds&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;termFee&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;num&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;loop&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;termFee&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;monthlyFee&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;num&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;loop&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;monthlyFee&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;termMonths&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;loop&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;termMonths&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;maxTermMonths&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;num&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;loop&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;maxTermMonths&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;cardOnly&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;!!&lt;/span&gt;&lt;span class="n"&gt;loop&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cardOnly&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;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt;
  &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"capacity"&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;"number"&lt;/span&gt;
  &lt;span class="n"&gt;min&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;}&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;plan&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;capacity&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="n"&gt;onChange&lt;/span&gt;&lt;span class="o"&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;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;setPlanField&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"capacity"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="n"&gt;onWheel&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;blurOnWheel&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="n"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Controlled values are unaffected by a remount from a changing &lt;code&gt;key&lt;/code&gt;. What React manages is the &lt;code&gt;plan&lt;/code&gt; state, and even when the DOM is rebuilt, &lt;code&gt;value={plan.capacity}&lt;/code&gt; keeps referring to the same state, so a remount on a validation error preserves what was re-entered.&lt;/p&gt;

&lt;p&gt;The preset stopped writing to the DOM and became a plain &lt;code&gt;state&lt;/code&gt; update.&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="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;applyPreset&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;kind&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"gakka"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="s2"&gt;"shinro"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
  &lt;span class="nf"&gt;setPlan&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="no"&gt;EMPTY_PLAN&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;planKind&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;kind&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;slotSeconds&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"30"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;termMonths&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"12"&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="n"&gt;kind&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s2"&gt;"gakka"&lt;/span&gt;
      &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;capacity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"5"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;termFee&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"200000"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cardOnly&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&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;capacity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"30"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;termFee&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"10000"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cardOnly&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="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;&lt;code&gt;formRef&lt;/code&gt; was no longer referenced anywhere and could be deleted.&lt;/p&gt;

&lt;h2&gt;
  
  
  Preventing a repeat
&lt;/h2&gt;

&lt;p&gt;We added a static test that pulls out each &lt;code&gt;&amp;lt;input&amp;gt;&lt;/code&gt; tag and checks that the fields the preset touches (&lt;code&gt;capacity&lt;/code&gt; / &lt;code&gt;slot_seconds&lt;/code&gt; / &lt;code&gt;term_fee&lt;/code&gt; / &lt;code&gt;monthly_fee_per_monitor&lt;/code&gt; / &lt;code&gt;term_months&lt;/code&gt; / &lt;code&gt;max_term_months&lt;/code&gt; / &lt;code&gt;card_only&lt;/code&gt;) carry no &lt;code&gt;defaultValue&lt;/code&gt; or &lt;code&gt;defaultChecked&lt;/code&gt;, and that &lt;code&gt;applyPreset&lt;/code&gt; calls &lt;code&gt;setPlan&lt;/code&gt; rather than &lt;code&gt;formRef.current.elements&lt;/code&gt;.&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="nf"&gt;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"fields the preset sets are held as controlled values (a remount does not clear them)"&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="no"&gt;PRESET_FIELDS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="s2"&gt;"capacity"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"slot_seconds"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"term_fee"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"monthly_fee_per_monitor"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"term_months"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"max_term_months"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"card_only"&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;form&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;SRC&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="no"&gt;SRC&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;indexOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"function LoopForm("&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;SRC&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;indexOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"function LoopCard("&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;uncontrolled&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;inputTags&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;form&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="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;PRESET_FIELDS&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;nameOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&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="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;defaultValue&lt;/span&gt;&lt;span class="o"&gt;=|&lt;/span&gt;&lt;span class="n"&gt;defaultChecked&lt;/span&gt;&lt;span class="o"&gt;=/&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&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="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nameOf&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;uncontrolled&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sb"&gt;`uncontrolled inputs that revert on remount: ${uncontrolled.join(", ")}`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toEqual&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;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;The &lt;code&gt;&amp;lt;input&amp;gt;&lt;/code&gt; tag extraction takes one tag as everything from &lt;code&gt;&amp;lt;input&lt;/code&gt; to the next &lt;code&gt;/&amp;gt;&lt;/code&gt;. Inputs whose attributes hold an arrow function (&lt;code&gt;onChange={(e) =&amp;gt; ...}&lt;/code&gt;) contain a &lt;code&gt;&amp;gt;&lt;/code&gt; in the middle, so trying to bound them with a single regex ends up missing exactly the inputs that have an &lt;code&gt;onChange&lt;/code&gt; — the very ones under test walk straight past.&lt;/p&gt;

&lt;p&gt;At the time we made only those eight fields controlled; the remaining inputs such as slot name and published setting were still on &lt;code&gt;defaultValue&lt;/code&gt;. The same symptom on other fields of the same form is covered in &lt;a href="https://tech.rebounder.jp/en/posts/react-partial-controlled-fields-silent-revert/" rel="noopener noreferrer"&gt;Half-Controlled Forms Silently Revert on Resubmit&lt;/a&gt;.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://tech.rebounder.jp/en/posts/react-key-remount-resets-uncontrolled-input/" rel="noopener noreferrer"&gt;tech.rebounder.jp&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>css</category>
      <category>javascript</category>
      <category>frontend</category>
    </item>
    <item>
      <title>Next.js Bundling a wasm Package Breaks Only in Production</title>
      <dc:creator>cometa-kaito</dc:creator>
      <pubDate>Thu, 26 Nov 2026 23:04:32 +0000</pubDate>
      <link>https://dev.to/cometakaito/nextjs-bundling-a-wasm-package-breaks-only-in-production-2ggd</link>
      <guid>https://dev.to/cometakaito/nextjs-bundling-a-wasm-package-breaks-only-in-production-2ggd</guid>
      <description>&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;An npm package that loads a wasm file breaks when included in the standard Next.js bundle.&lt;/strong&gt; If the package resolves its wasm by relative path internally, bundling changes what that path is relative to, and it fails at runtime with a type error.&lt;/p&gt;

&lt;p&gt;The fix is to exclude the package from bundling with &lt;code&gt;serverExternalPackages&lt;/code&gt; and to explicitly include the wasm itself in the deployment with &lt;code&gt;outputFileTracingIncludes&lt;/code&gt; — &lt;strong&gt;both, not one.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Symptom
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Requests issuing a document PDF return 502 in production only&lt;/li&gt;
&lt;li&gt;The same failure reproduces locally under &lt;code&gt;next build &amp;amp;&amp;amp; next start&lt;/code&gt; (production build mode)&lt;/li&gt;
&lt;li&gt;The log shows this type error
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;TypeError: The "path" argument must be of type string … Received type number
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A type error where a string path should be passed. Reviewing the calling code, the value being passed is a string.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cause
&lt;/h2&gt;

&lt;p&gt;PDF generation uses HarfBuzz (&lt;code&gt;subset-font&lt;/code&gt;) to build a font reduced to just the characters used, avoiding dropped CJK glyphs. When &lt;code&gt;subset-font&lt;/code&gt; loads its &lt;code&gt;.wasm&lt;/code&gt; file, it &lt;strong&gt;resolves it relative to its own module&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Next.js builds pack dependency code into a single runtime bundle by default. Once bundled, a module's physical location changes, and &lt;strong&gt;the base for the relative path to the wasm shifts&lt;/strong&gt;. The package believes it is passing a string path, but the value the bundler rewrote is no longer a string at runtime, and it surfaces as a &lt;code&gt;path&lt;/code&gt; type error.&lt;/p&gt;

&lt;p&gt;This breakage is &lt;strong&gt;caused by the bundling step itself&lt;/strong&gt;. &lt;code&gt;outputFileTracingIncludes&lt;/code&gt; and &lt;code&gt;serverExternalPackages&lt;/code&gt; both exist only in Next.js's build and deployment packaging, so simply running the code never touches them.&lt;/p&gt;

&lt;h2&gt;
  
  
  The fix
&lt;/h2&gt;

&lt;p&gt;Both settings, together. One alone does not fix it.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Exclude from the bundle
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// next.config.ts&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;nextConfig&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;NextConfig&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;serverExternalPackages&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"subset-font"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"harfbuzzjs"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"fontverter"&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;Now those packages skip webpack/turbopack bundling and are loaded at runtime from &lt;code&gt;node_modules&lt;/code&gt; by plain &lt;code&gt;require&lt;/code&gt;. Their internal relative path resolution stays as it is in development.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Include in the deployment
&lt;/h3&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;nextConfig&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;NextConfig&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;outputFileTracingIncludes&lt;/span&gt;&lt;span class="p"&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;span class="s2"&gt;"./src/assets/fonts/**"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"./node_modules/harfbuzzjs/**"&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;Next.js's deployment packaging traces file dependencies from static &lt;code&gt;import&lt;/code&gt;s in the code. &lt;strong&gt;Runtime dynamic reads&lt;/strong&gt; like &lt;code&gt;fs.readFileSync&lt;/code&gt; are not traced, so unless font files and the wasm are included explicitly, the files simply do not exist inside the deployed function.&lt;/p&gt;

&lt;p&gt;Specifying only &lt;code&gt;serverExternalPackages&lt;/code&gt; stops the package code being bundled, but &lt;strong&gt;does not guarantee the wasm it depends on ends up in the deployment package.&lt;/strong&gt; Only both together satisfy path resolution in the code and the existence of the file.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why it went unnoticed
&lt;/h2&gt;

&lt;p&gt;Both settings presuppose that a bundling and tracing step exists. &lt;code&gt;outputFileTracingIncludes&lt;/code&gt; is part of the mechanism that decides the deployment set by tracing; &lt;code&gt;serverExternalPackages&lt;/code&gt; is the mechanism for taking a package out of that bundling. &lt;strong&gt;This is a bug you only hit by going through that step&lt;/strong&gt;, so running the code as usual never reveals it.&lt;/p&gt;

&lt;p&gt;The relevant place in &lt;code&gt;next.config.ts&lt;/code&gt; now carries a comment with the symptom (the type error text) and the cause (wasm path resolution breaking) — where the next person adding a wasm-requiring package will read it before hitting the same wall.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://tech.rebounder.jp/en/posts/nextjs-wasm-bundling-broken/" rel="noopener noreferrer"&gt;tech.rebounder.jp&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>css</category>
      <category>javascript</category>
      <category>frontend</category>
    </item>
    <item>
      <title>Cloud Run Jobs Freeze Their Image</title>
      <dc:creator>cometa-kaito</dc:creator>
      <pubDate>Wed, 25 Nov 2026 23:10:39 +0000</pubDate>
      <link>https://dev.to/cometakaito/cloud-run-jobs-freeze-their-image-37lp</link>
      <guid>https://dev.to/cometakaito/cloud-run-jobs-freeze-their-image-37lp</guid>
      <description>&lt;h2&gt;
  
  
  Check this first
&lt;/h2&gt;

&lt;p&gt;Read the image the job actually runs.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;gcloud run &lt;span class="nb"&gt;jobs&lt;/span&gt; describe &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;job&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; --region=asia-northeast1 \
  --format=&lt;span class="s2"&gt;"value(spec.template.spec.template.spec.containers[0].image)"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then check whether your fix is contained in it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git merge-base --is-ancestor &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;fix-commit&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;image-sha&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"contained"&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"not contained"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If it is not contained, &lt;strong&gt;re-running changes nothing&lt;/strong&gt;. Worse, the job may overwrite data again using the old behaviour.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is actually happening
&lt;/h2&gt;

&lt;p&gt;The image is pinned in the job definition. When the definition lives in infrastructure-as-code, the tag is declared as a variable, and &lt;strong&gt;the job keeps running the old code until that tag is bumped and applied&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;local fix → commit → [ wall ] → job execution
                       ↑ image is still the old one
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The awkward part is that the run succeeds. Nothing appears in the logs. All you are left with is "I ran it and it did not take effect".&lt;/p&gt;

&lt;h2&gt;
  
  
  Two ways out
&lt;/h2&gt;

&lt;h3&gt;
  
  
  A. Override the environment variable at execution time
&lt;/h3&gt;

&lt;p&gt;If the code reads &lt;code&gt;process.env.X ?? default&lt;/code&gt;, you can swap the value without touching the image.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;gcloud run &lt;span class="nb"&gt;jobs&lt;/span&gt; execute &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;job&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; --region=asia-northeast1 \
  --update-env-vars=KEY=value --wait
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;No build, no apply, so this is the fast path. But &lt;strong&gt;it applies to that execution only&lt;/strong&gt; — the job definition is unchanged, and the old default comes back the next time the flag is omitted.&lt;/p&gt;

&lt;h3&gt;
  
  
  B. Update the image (the real fix)
&lt;/h3&gt;

&lt;p&gt;Build, bump the tag, apply. The exact commands depend on your setup, but &lt;strong&gt;you cannot skip the tag bump&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Preventing a repeat
&lt;/h2&gt;

&lt;p&gt;After this incident we put the sha check at the very top of the runbook. The root cause was that running the job without checking was possible at all, so &lt;strong&gt;changing the order of the steps&lt;/strong&gt; is what actually helps.&lt;/p&gt;

&lt;p&gt;We also wrote down, in the runbook, that the environment-variable override is a stopgap. Because it is fast, it is easy to walk away believing the problem is fixed.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://tech.rebounder.jp/en/posts/cloud-run-job-image-frozen/" rel="noopener noreferrer"&gt;tech.rebounder.jp&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>database</category>
      <category>postgres</category>
      <category>devops</category>
      <category>webdev</category>
    </item>
    <item>
      <title>INSTALL_FAILED_UPDATE_INCOMPATIBLE Locks a Device Owner</title>
      <dc:creator>cometa-kaito</dc:creator>
      <pubDate>Tue, 24 Nov 2026 23:10:20 +0000</pubDate>
      <link>https://dev.to/cometakaito/installfailedupdateincompatible-locks-a-device-owner-1fpb</link>
      <guid>https://dev.to/cometakaito/installfailedupdateincompatible-locks-a-device-owner-1fpb</guid>
      <description>&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;An Android device locked down as a Device Owner kiosk hits a dead end the moment its APK signing key changes: it can't be updated, and uninstalling to work around that can't be undone.&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;An APK signed with a different key safely fails &lt;code&gt;adb install -r&lt;/code&gt; (&lt;code&gt;INSTALL_FAILED_UPDATE_INCOMPATIBLE&lt;/code&gt;). The existing app is left untouched.&lt;/li&gt;
&lt;li&gt;Deciding to "just uninstall and reinstall" at that point strips the device of its Device Owner admin privilege itself.&lt;/li&gt;
&lt;li&gt;Device Owner can normally only be restored by reprovisioning from a full factory reset. On site, that's a dead end.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This came up when switching the signing key for a kiosk app flashed onto signage devices (Google TV) from a debug key used for verification to a dedicated release key.&lt;/p&gt;

&lt;h2&gt;
  
  
  Symptom
&lt;/h2&gt;

&lt;p&gt;Field devices had been running APKs signed with a debug key (&lt;code&gt;0951ef53…&lt;/code&gt;) carried over from the PoC period. Ahead of production operation, a dedicated release key — separate from the debug key — was created, and the build switched to use it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;// app/build.gradle.kts&lt;/span&gt;
&lt;span class="c1"&gt;// Release key (from 2026-10-28). Not checked into the repo; lives in ~/.kimiteras-signing/.&lt;/span&gt;
&lt;span class="c1"&gt;// Environments without it (e.g. CI) fall back to the debug key.&lt;/span&gt;
&lt;span class="c1"&gt;// Existing devices (signed with the 0951 key) can't be overwritten — signatures don't match.&lt;/span&gt;
&lt;span class="c1"&gt;// See dist/APK-MANIFEST.md.&lt;/span&gt;
&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;releaseKs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;file&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getProperty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"user.home"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s2"&gt;"/.kimiteras-signing/kimiteras-tvbridge-release.p12"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;releasePass&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;file&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getProperty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"user.home"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s2"&gt;"/.kimiteras-signing/storepass.txt"&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;releaseKs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;exists&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;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;releasePass&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;exists&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;signingConfigs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"kimiteras"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;storeFile&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;releaseKs&lt;/span&gt;
        &lt;span class="n"&gt;storeType&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"pkcs12"&lt;/span&gt;
        &lt;span class="n"&gt;storePassword&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;releasePass&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readText&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="nf"&gt;trim&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;keyAlias&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"kimiteras-tvbridge"&lt;/span&gt;
        &lt;span class="n"&gt;keyPassword&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;storePassword&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;signing&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;signingConfigs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findByName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"kimiteras"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="n"&gt;signingConfigs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getByName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"debug"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Trying to overwrite an APK already running as Device Owner in the field with a build signed by this new key, via &lt;code&gt;adb install -r&lt;/code&gt;, makes Android refuse the update because the signatures don't match. The app's own manifest spells this out too.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Signed only with the release key &lt;code&gt;kimiteras-tvbridge&lt;/code&gt; (cert SHA-256 &lt;code&gt;B2:D5:99:38:…:C1:97:DC&lt;/code&gt;, v2+v3). The key lives in &lt;code&gt;~/.kimiteras-signing/&lt;/code&gt; (outside the knowledge base, not in the repo). ⚠ Won't &lt;code&gt;install -r&lt;/code&gt; onto existing devices on the 0951 key (signature mismatch) — move them to this version when they're rebuilt. Until then, use &lt;code&gt;a90d6a95…&lt;/code&gt; for recovery (setup-monitor).&lt;/p&gt;

&lt;p&gt;— &lt;code&gt;dist/APK-MANIFEST.md&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Android's package manager requires the signing certificate to match when overwriting an app with the same &lt;code&gt;applicationId&lt;/code&gt;. If it doesn't match, &lt;code&gt;install -r&lt;/code&gt; safely fails (without corrupting data) and returns the error code &lt;code&gt;INSTALL_FAILED_UPDATE_INCOMPATIBLE&lt;/code&gt;. Up to this point, this is something that happens with ordinary Android apps too, and a straightforward reading ends at "just uninstall and reinstall."&lt;/p&gt;

&lt;h2&gt;
  
  
  Cause
&lt;/h2&gt;

&lt;p&gt;Device Owner is different. This is exactly the safety rule already spelled out in a 2026-06-15 on-site update procedure, written against this very failure code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;## 0. Safety rules (read first, most important)

- Never run `adb uninstall`. Uninstalling removes Device Owner, and without a factory reset there is no recovery.
- This build's signing key `0951ef53…` matches the current production key (FCM build, 2026-06-11). So devices with 6/11 installed can be updated via `install -r`.
- `install -r` safely fails if the signature doesn't match, leaving the existing app in place (`INSTALL_FAILED_UPDATE_INCOMPATIBLE`).
  If it fails, leave that device alone — record the model and IP and pull back (devices on an old key are a separate matter). Do not uninstall even if it fails.

— dist/UPDATE-2026-06-15-night-off-recovery.md
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The assumption that Android's Device Owner — built on the premise that an organization manages the whole device — is designed so it can't simply be removed the way an ordinary app can with &lt;code&gt;adb uninstall&lt;/code&gt;, is only half right. It's actually the opposite: &lt;strong&gt;uninstalling the Device Owner app itself removes the device-admin privilege along with it.&lt;/strong&gt; On most models, the only way to re-enable a Device Owner that's been removed is to reprovision from a factory-reset state with no accounts added at all; running &lt;code&gt;dpm set-device-owner&lt;/code&gt; again on site does not bring it back.&lt;/p&gt;

&lt;p&gt;In other words, for a Device Owner device, &lt;code&gt;INSTALL_FAILED_UPDATE_INCOMPATIBLE&lt;/code&gt; isn't a "one level safer" failure — it's &lt;strong&gt;the last branch point where things should stop.&lt;/strong&gt; Uninstalling before re-matching the signature means losing the foundation — the device-admin privilege — before ever solving the signature problem. Back in June, this was insurance against the rare case of a device ending up with a build signed by a different debug key. Switching to a dedicated release key in October made &lt;strong&gt;every existing device fall on this branch.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Response
&lt;/h2&gt;

&lt;p&gt;What actually got decided wasn't a one-shot switch of every existing device to the release key — it was accepting that two keys would coexist.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Newly built devices are flashed with release-key-signed APKs from the start.&lt;/li&gt;
&lt;li&gt;Existing devices (running on the 0951 key) keep running builds signed with that key.&lt;/li&gt;
&lt;li&gt;Existing devices move to the release key only when they're rebuilt anyway — i.e., when they break and need reprovisioning.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A one-shot switch would converge everything onto a single key, but doing it would require factory-resetting every existing device on site. Right now, that cost is judged higher than the cost of leaving the signing keys split.&lt;/p&gt;

&lt;h2&gt;
  
  
  Operating rule
&lt;/h2&gt;

&lt;p&gt;Before updating APKs on a fleet of Device Owner devices, there's exactly one thing to check first: &lt;strong&gt;is the APK about to be flashed signed with the same key as the one already on that device?&lt;/strong&gt; If not, &lt;code&gt;install -r&lt;/code&gt; will fail regardless, so checking first means never seeing &lt;code&gt;INSTALL_FAILED_UPDATE_INCOMPATIBLE&lt;/code&gt; at all.&lt;/p&gt;

&lt;p&gt;If &lt;code&gt;INSTALL_FAILED_UPDATE_INCOMPATIBLE&lt;/code&gt; does show up on site, there's exactly one thing to do.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Don't run &lt;code&gt;adb uninstall&lt;/code&gt;.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Leave that device alone — record its model and IP and pull back.&lt;/li&gt;
&lt;li&gt;Either flash an APK signed with the matching key, or defer it to the next time that device gets rebuilt anyway.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;"The signature doesn't match" is Android safely telling you something, and stopping there leaves the device untouched. The damage comes from the next move — the one that reads this error as a signal to "just reinstall then."&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://tech.rebounder.jp/en/posts/android-release-signing-key-switch-breaks-update-on-device-owner-kiosk/" rel="noopener noreferrer"&gt;tech.rebounder.jp&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>android</category>
      <category>iot</category>
      <category>kotlin</category>
      <category>programming</category>
    </item>
    <item>
      <title>new row violates check constraint on documents.source</title>
      <dc:creator>cometa-kaito</dc:creator>
      <pubDate>Mon, 23 Nov 2026 23:13:31 +0000</pubDate>
      <link>https://dev.to/cometakaito/new-row-violates-check-constraint-on-documentssource-3ia0</link>
      <guid>https://dev.to/cometakaito/new-row-violates-check-constraint-on-documentssource-3ia0</guid>
      <description>&lt;h2&gt;
  
  
  The short version
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;documents&lt;/code&gt; table's &lt;code&gt;source&lt;/code&gt; column had a CHECK constraint, &lt;code&gt;documents_source_check&lt;/code&gt;, allowing only &lt;code&gt;'upload'&lt;/code&gt; and &lt;code&gt;'dropbox'&lt;/code&gt;. But the automated document-archiving feature inserts quote/invoice/application/receipt snapshots with &lt;code&gt;source: 'system'&lt;/code&gt;, and the Anyflow import feature inserts signed contracts with &lt;code&gt;source: 'anyflow'&lt;/code&gt;. &lt;strong&gt;Neither value was allowed.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The result: the file upload to storage succeeded every single time, while &lt;strong&gt;the insert into the &lt;code&gt;documents&lt;/code&gt; table failed every single time&lt;/strong&gt;, with &lt;code&gt;new row for relation "documents" violates check constraint "documents_source_check"&lt;/code&gt;. Not one row of the automated document vault had ever been successfully created.&lt;/p&gt;

&lt;h2&gt;
  
  
  A failure designed to be swallowed silently
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;upsertDocument&lt;/code&gt; in &lt;code&gt;document-archive.ts&lt;/code&gt; uploads the file to storage first, then upserts the &lt;code&gt;documents&lt;/code&gt; row:&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="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;upsertDocument&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;admin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;AdminClient&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;opts&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="cm"&gt;/* ... */&lt;/span&gt; &lt;span class="n"&gt;source&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"system"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="s2"&gt;"anyflow"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="cm"&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="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;ArchiveResult&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;upErr&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;admin&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;storage&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;BUCKET&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;upload&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;opts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;storagePath&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;opts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;contentType&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;opts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;mimeType&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;upsert&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="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;upErr&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="n"&gt;ok&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;reason&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sb"&gt;`storage: ${upErr.message}`&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="c1"&gt;// ...check for an existing row by external_ref...&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;inserted&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;admin&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"documents"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;insert&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="n"&gt;fields&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;created_by&lt;/span&gt;&lt;span class="p"&gt;:&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="nf"&gt;select&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;single&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;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// 23505 (uq_documents_external race) is handled separately above&lt;/span&gt;
    &lt;span class="c1"&gt;// ...&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;ok&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;reason&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sb"&gt;`insert: ${error.message}`&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="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;ok&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;documentId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;inserted&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&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 CHECK violation (Postgres error code &lt;code&gt;23514&lt;/code&gt;) doesn't match the &lt;code&gt;23505&lt;/code&gt; unique-violation race handled just above it, so it falls straight through to &lt;code&gt;{ ok: false, reason: "insert: new row for relation \"documents\" violates check constraint \"documents_source_check\"" }&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The function that calls this, &lt;code&gt;archiveContractDocument&lt;/code&gt;, has a comment stating the intent directly:&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="cm"&gt;/**
 * Automatically archives generated documents (quote/invoice/application/receipt)
 * to the document vault.
 * ...
 * - Callers (the signature webhook, invoice issuance) must not break, so every
 *   failure — including exceptions — is returned as an ArchiveResult instead of thrown.
 * ...
 */&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;archiveExternalDocument&lt;/code&gt;, which handles external imports, carries the same stated intent:&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="cm"&gt;/**
 * Archives a signed file imported from outside (e.g. an Anyflow-signed contract)
 * to the document vault.
 * ...
 * - Failures are returned as an ArchiveResult so the caller (the webhook) is not broken.
 */&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This was not an accident — it was &lt;strong&gt;the intended design.&lt;/strong&gt; A document-archiving failure should never block the main flow, such as a payment-state transition or signature confirmation. Choosing to return a result object instead of throwing is reasonable on its own. The problem is that nothing downstream was actually reading that &lt;code&gt;ArchiveResult&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Nobody was checking &lt;code&gt;.ok&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;invoice-issue.ts&lt;/code&gt; looked like this:&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="c1"&gt;// Auto-archive the invoice snapshot to the document vault.&lt;/span&gt;
&lt;span class="c1"&gt;// Runs before checking if the email can be sent, so the issued document is&lt;/span&gt;
&lt;span class="c1"&gt;// kept even without a registered email. Idempotent — failures are ignored.&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;archived&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;archiveContractDocument&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;contractId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;kind&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"invoice"&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="c1"&gt;// ...resolve recipient...&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;sent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;sendEmail&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// ...&lt;/span&gt;
  &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="c1"&gt;// ...&lt;/span&gt;
    &lt;span class="s2"&gt;"pdf"&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;archived&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;archived&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pdf&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;
      &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="s2"&gt;"The invoice (PDF) is attached to this email. ..."&lt;/span&gt;
      &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"You can view and save the invoice as a PDF here (no login required):"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="c1"&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;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"\n"&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;span class="s2"&gt;"pdf"&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;archived&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;archived&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pdf&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;
    &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;attachments&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;filename&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"invoice.pdf"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;archived&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pdf&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;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;Nowhere in the file is &lt;code&gt;archived.ok&lt;/code&gt; ever read. Only &lt;code&gt;archived.pdf&lt;/code&gt; (the PDF bytes for the email attachment) is used. Since &lt;code&gt;dryRun&lt;/code&gt; isn't set, &lt;code&gt;archiveContractDocument&lt;/code&gt; does attempt the real storage and DB writes, but PDF generation itself doesn't depend on whether the &lt;code&gt;documents&lt;/code&gt; insert succeeded — &lt;strong&gt;so the email-sending path has no way to notice the archive is failing.&lt;/strong&gt; Invoices kept issuing and sending normally while the document vault stayed empty underneath them.&lt;/p&gt;

&lt;p&gt;The signing flow in &lt;code&gt;contract-signed.ts&lt;/code&gt; has the same pattern, and says so in its own comment:&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="c1"&gt;// 2b. Auto-archive the application snapshot to the document vault (ops spec §1).&lt;/span&gt;
&lt;span class="c1"&gt;//     A signature is frozen as proof of consent. Non-kimiteras contract types&lt;/span&gt;
&lt;span class="c1"&gt;//     are skipped inside archiveContractDocument. Idempotent — failures are swallowed.&lt;/span&gt;
&lt;span class="k"&gt;try&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;r&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;archiveContractDocument&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;contractId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;kind&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"application"&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="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ok&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"[contract-signed] application archive failed:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;reason&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;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;skipped&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// ...signature evidence-anchor processing...&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;This path does log the failure with &lt;code&gt;console.error&lt;/code&gt;, but takes no further action. Without active log monitoring, this line filling up with CHECK violations never reaches anyone.&lt;/p&gt;

&lt;h2&gt;
  
  
  The fix was just widening the constraint
&lt;/h2&gt;

&lt;p&gt;The fix, in migration 0074, only added &lt;code&gt;'system'&lt;/code&gt; and &lt;code&gt;'anyflow'&lt;/code&gt; to &lt;code&gt;documents_source_check&lt;/code&gt;'s allowed values:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;alter&lt;/span&gt; &lt;span class="k"&gt;table&lt;/span&gt; &lt;span class="n"&gt;public&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;documents&lt;/span&gt; &lt;span class="k"&gt;drop&lt;/span&gt; &lt;span class="k"&gt;constraint&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="k"&gt;exists&lt;/span&gt; &lt;span class="n"&gt;documents_source_check&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;alter&lt;/span&gt; &lt;span class="k"&gt;table&lt;/span&gt; &lt;span class="n"&gt;public&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;documents&lt;/span&gt; &lt;span class="k"&gt;add&lt;/span&gt; &lt;span class="k"&gt;constraint&lt;/span&gt; &lt;span class="n"&gt;documents_source_check&lt;/span&gt;
  &lt;span class="k"&gt;check&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;source&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;any&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'upload'&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'dropbox'&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'system'&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'anyflow'&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;text&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;It's an additive, idempotent migration (&lt;code&gt;drop constraint if exists&lt;/code&gt; then &lt;code&gt;add constraint&lt;/code&gt;); since every existing row used &lt;code&gt;'upload'&lt;/code&gt;, they all still satisfy the new constraint. No application code changed — &lt;code&gt;upsertDocument&lt;/code&gt; and &lt;code&gt;invoice-issue.ts&lt;/code&gt; were already correct code that only needed the constraint to let their rows through.&lt;/p&gt;

&lt;h2&gt;
  
  
  The hidden cost of "don't break the main flow"
&lt;/h2&gt;

&lt;p&gt;Swallowing failures silently so the main flow never breaks is a reasonable design choice on its own. In fact, the payment-transition and signature-confirmation flows never stopped once, even while the CHECK violation went unfixed. The cost is that &lt;strong&gt;the assumption "the archive must be succeeding" accumulated for months with nothing ever verifying it.&lt;/strong&gt; Multiple callers that never check &lt;code&gt;archived.ok&lt;/code&gt; can't be caught by &lt;code&gt;console.error&lt;/code&gt; logging alone — in practice, what surfaced this was literally counting the rows in the document vault. Choosing to swallow failures silently only works if the swallowed outcome (the count of &lt;code&gt;ok: false&lt;/code&gt; results) is made visible somewhere else.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://tech.rebounder.jp/en/posts/documents-source-check-constraint-missing-system-anyflow-silent-total-failure/" rel="noopener noreferrer"&gt;tech.rebounder.jp&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>database</category>
      <category>postgres</category>
      <category>devops</category>
      <category>webdev</category>
    </item>
    <item>
      <title>GCS ifGenerationMatch autoRetry Misreads 412 as Already Sent</title>
      <dc:creator>cometa-kaito</dc:creator>
      <pubDate>Mon, 23 Nov 2026 00:24:17 +0000</pubDate>
      <link>https://dev.to/cometakaito/gcs-ifgenerationmatch-autoretry-misreads-412-as-already-sent-182j</link>
      <guid>https://dev.to/cometakaito/gcs-ifgenerationmatch-autoretry-misreads-412-as-already-sent-182j</guid>
      <description>&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;A write that uses a GCS object's &lt;code&gt;ifGenerationMatch: 0&lt;/code&gt; to decide atomically "nothing sent today yet" can still be misread if the client's autoRetry is on: when the first write succeeds server-side but only its response is lost, the retry's 412 Precondition Failed gets treated as "already sent."&lt;/strong&gt; The fix was &lt;code&gt;new Storage({ retryOptions: { autoRetry: false } })&lt;/code&gt; — turn off autoRetry entirely, let the failure throw, and claim again on the next run.&lt;/p&gt;

&lt;h2&gt;
  
  
  Symptom
&lt;/h2&gt;

&lt;p&gt;A Job sends a "✅ TV monitoring running" digest to Slack once per weekday morning. It runs every minute, and only the first successful run inside the window (08:30–09:29 JST on weekdays) is supposed to send. "Already sent today" was recorded as an empty GCS object at &lt;code&gt;tv-liveness/digest/&amp;lt;YYYY-MM-DD&amp;gt;&lt;/code&gt;.&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="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;decideDailyDigest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;now&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Date&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="nc"&gt;DigestMarkerPort&lt;/span&gt; &lt;span class="o"&gt;|&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="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;DigestDecision&lt;/span&gt;&lt;span class="o"&gt;&amp;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="o"&gt;!&lt;/span&gt;&lt;span class="nf"&gt;isDailyDigestWindow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;now&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="n"&gt;send&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&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;day&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;jstDay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;now&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;marker&lt;/span&gt; &lt;span class="o"&gt;===&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="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;isDailyDigestMinute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;now&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="n"&gt;send&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;day&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;claimed&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&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;send&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&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;try&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="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;marker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;claim&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;day&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="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;send&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;day&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;claimed&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="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;send&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&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;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;warn&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="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"tv.health_check.digest_marker_error"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"unknown"&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;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;isDailyDigestMinute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;now&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="n"&gt;send&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;day&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;claimed&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&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;send&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&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;The record is created atomically with &lt;code&gt;ifGenerationMatch: 0&lt;/code&gt; (create only if the object doesn't exist yet), so overlapping per-minute runs or the occasional retry should still only send once. But on days when the network was briefly unstable, the digest sometimes never arrived at all. GCS had the object recording "already sent," while Slack had never actually received it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cause
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;claim()&lt;/code&gt; was written to swallow any 412 Precondition Failed as "already recorded today = already sent."&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="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;createGcsDigestMarker&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bucketName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;DigestMarkerPort&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;bucket&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Storage&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="nf"&gt;bucket&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bucketName&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;objectFor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;day&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;bucket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;file&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sb"&gt;`tv-liveness/digest/${day}`&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="k"&gt;async&lt;/span&gt; &lt;span class="nf"&gt;claim&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;day&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;objectFor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;day&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;save&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="n"&gt;resumable&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="n"&gt;contentType&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"text/plain"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="n"&gt;preconditionOpts&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;ifGenerationMatch&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="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="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// 412 Precondition Failed = already recorded today (already sent or in flight).&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;code&lt;/span&gt;&lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="kt"&gt;number&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;code&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;412&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="n"&gt;err&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="k"&gt;async&lt;/span&gt; &lt;span class="nf"&gt;release&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;day&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;objectFor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;day&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;delete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;ignoreNotFound&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="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;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;new Storage()&lt;/code&gt; has client-side autoRetry on by default. If the first &lt;code&gt;save()&lt;/code&gt; succeeds server-side but only its success response is lost to a network issue, the client sees no response and automatically resends the same write. That second write can no longer satisfy &lt;code&gt;ifGenerationMatch: 0&lt;/code&gt;, because the object from the first attempt already exists — so it gets a 412.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;claim()&lt;/code&gt; unconditionally reads that 412 as "already recorded today" and returns &lt;code&gt;false&lt;/code&gt;. &lt;code&gt;decideDailyDigest&lt;/code&gt; treats that &lt;code&gt;false&lt;/code&gt; as "no need to send this run (some other run already did)," so &lt;code&gt;send: false&lt;/code&gt; wins and nothing goes to Slack. But the object had already existed since the moment the first write succeeded server-side, so nobody can claim for that day ever again — the digest for that day is gone for good. A single transient lost response erased an entire day's digest.&lt;/p&gt;

&lt;h2&gt;
  
  
  The fix
&lt;/h2&gt;

&lt;p&gt;Client-side autoRetry on the &lt;code&gt;Storage&lt;/code&gt; client was turned off, so a lost response now throws instead of being silently retried.&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="cm"&gt;/** GCS implementation. Auth via Workload Identity (ADC); no key file is used (rule 5). */&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;createGcsDigestMarker&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bucketName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;DigestMarkerPort&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// autoRetry is off: if the first attempt succeeds server-side and only its response is lost,&lt;/span&gt;
  &lt;span class="c1"&gt;// the client's own retry gets a 412 and misreads it as "already sent," losing that day's digest.&lt;/span&gt;
  &lt;span class="c1"&gt;// Letting the failure throw and claiming again on the next minute is safer.&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;bucket&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Storage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;retryOptions&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;autoRetry&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&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="nf"&gt;bucket&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bucketName&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;objectFor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;day&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;bucket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;file&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sb"&gt;`tv-liveness/digest/${day}`&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="k"&gt;async&lt;/span&gt; &lt;span class="nf"&gt;claim&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;day&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;objectFor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;day&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;save&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="n"&gt;resumable&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="n"&gt;contentType&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"text/plain"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="n"&gt;preconditionOpts&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;ifGenerationMatch&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="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="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// 412 Precondition Failed = already recorded today (already sent or in flight).&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;code&lt;/span&gt;&lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="kt"&gt;number&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;code&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;412&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="n"&gt;err&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="k"&gt;async&lt;/span&gt; &lt;span class="nf"&gt;release&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;day&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;objectFor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;day&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;delete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;ignoreNotFound&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="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;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;claim()&lt;/code&gt;'s own 412 handling was not changed. What was turned off is only the client library's internal behavior of automatically resending the same write when a response is lost. Now a lost response propagates as a thrown exception all the way to &lt;code&gt;decideDailyDigest&lt;/code&gt;'s catch, which already falls back to "this run failed to record, so send based on a single-run check if we're inside the window." Because the Job itself runs again every minute, it simply claims again next minute — retry responsibility sits with the outer scheduler, not inside the client library.&lt;/p&gt;

&lt;h2&gt;
  
  
  Preventing a repeat
&lt;/h2&gt;

&lt;p&gt;The sources for this incident don't record any additional mechanical check being added — what can be written here is only the structural reason this misread was hard to notice.&lt;/p&gt;

&lt;p&gt;With autoRetry on, the path from a lost response to a 412 was fully absorbed inside &lt;code&gt;claim()&lt;/code&gt;'s own &lt;code&gt;catch&lt;/code&gt;, which just returns &lt;code&gt;false&lt;/code&gt; and moves on. It never reached &lt;code&gt;decideDailyDigest&lt;/code&gt;'s own catch (the logging path that does &lt;code&gt;console.warn&lt;/code&gt; with &lt;code&gt;tv.health_check.digest_marker_error&lt;/code&gt;), so the only thing visible in logs from the outside was an ordinary-looking &lt;code&gt;send: false&lt;/code&gt; decision. A transient lost response and a genuinely normal case — some other run having already sent — were indistinguishable in the logs.&lt;/p&gt;

&lt;p&gt;Turning off autoRetry means a lost response is no longer swallowed inside the client and instead throws, landing on the existing &lt;code&gt;digest_marker_error&lt;/code&gt; log path. A transient lost response and a normal "already sent" can now be told apart inside logging that already existed, without adding new monitoring.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://tech.rebounder.jp/en/posts/gcs-client-autoretry-turns-idempotent-write-into-false-412/" rel="noopener noreferrer"&gt;tech.rebounder.jp&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>database</category>
      <category>postgres</category>
      <category>devops</category>
      <category>webdev</category>
    </item>
    <item>
      <title>middleware matcher negative lookahead is a prefix match</title>
      <dc:creator>cometa-kaito</dc:creator>
      <pubDate>Sun, 22 Nov 2026 00:19:33 +0000</pubDate>
      <link>https://dev.to/cometakaito/middleware-matcher-negative-lookahead-is-a-prefix-match-19mi</link>
      <guid>https://dev.to/cometakaito/middleware-matcher-negative-lookahead-is-a-prefix-match-19mi</guid>
      <description>&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;The negative lookahead &lt;code&gt;(?!token)&lt;/code&gt; you write inside Next.js's &lt;code&gt;config.matcher&lt;/code&gt; is a plain JavaScript prefix match: a bare &lt;code&gt;token&lt;/code&gt; only asks "does the string start with this?"&lt;/strong&gt; Kimiteras v2's auth middleware (&lt;code&gt;apps/web/middleware.ts&lt;/code&gt;) had written &lt;code&gt;login&lt;/code&gt;, &lt;code&gt;student&lt;/code&gt;, &lt;code&gt;api/auth&lt;/code&gt;, and &lt;code&gt;api/health&lt;/code&gt; without any boundary, so any later route that merely shared that prefix — &lt;code&gt;/api/authority&lt;/code&gt;, &lt;code&gt;/students&lt;/code&gt;, and similar protected routes that didn't exist yet — matched the same negative lookahead and was silently dropped from the middleware's auth gate. The fix was to append a &lt;code&gt;(?:/|$)&lt;/code&gt; boundary right after each token.&lt;/p&gt;

&lt;h2&gt;
  
  
  The failure mode
&lt;/h2&gt;

&lt;p&gt;Found on 2026-05-31 in PR #250 (#139 L3). Before the fix, &lt;code&gt;config.matcher&lt;/code&gt; read:&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="c1"&gt;// middleware.ts (before the fix, sha 26c1858a)&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;config&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;matcher&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="s2"&gt;"/((?!login|s/|student|signage/|guide(?:/|$)|api/auth|api/health|api/guide/|_next/static|_next/image|favicon.ico|.*\\.(?:png|jpg|jpeg|gif|svg|ico|webp|css|js|map|woff|woff2|ttf)$).*)"&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;&lt;code&gt;login&lt;/code&gt;, &lt;code&gt;student&lt;/code&gt;, &lt;code&gt;api/auth&lt;/code&gt;, and &lt;code&gt;api/health&lt;/code&gt; were still bare prefixes (&lt;code&gt;guide&lt;/code&gt; alone had already been boundaried to &lt;code&gt;guide(?:/|$)&lt;/code&gt; after an earlier, unrelated fix). With this pattern in place, adding a protected route like &lt;code&gt;/api/authority&lt;/code&gt; later meant the negative lookahead matched it too — it still &lt;em&gt;starts with&lt;/em&gt; &lt;code&gt;api/auth&lt;/code&gt; — so it fell into &lt;code&gt;config.matcher&lt;/code&gt;'s exclusion list, which means no middleware, which means no auth gate. The surrounding docstring already warned that future protected routes such as &lt;code&gt;/students&lt;/code&gt;, &lt;code&gt;/api/authority&lt;/code&gt;, &lt;code&gt;/api/healthcheck&lt;/code&gt;, or &lt;code&gt;/loginland&lt;/code&gt; could be silently dropped.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cause
&lt;/h2&gt;

&lt;p&gt;JavaScript's negative lookahead &lt;code&gt;(?!...)&lt;/code&gt; only tests whether the text starting at that position &lt;em&gt;begins with&lt;/em&gt; one of the given alternatives. With no word-boundary or end-of-string anchor, whatever follows the token is irrelevant — the lookahead already matched. &lt;code&gt;guide&lt;/code&gt; had received a &lt;code&gt;guide(?:/|$)&lt;/code&gt; boundary in an earlier PR, but the other four tokens were left untouched. Code review flagged the same shape of problem — a prefix match standing in for an exact match — and all four were boundaried together in this change.&lt;/p&gt;

&lt;h2&gt;
  
  
  The fix
&lt;/h2&gt;

&lt;p&gt;A &lt;code&gt;(?:/|$)&lt;/code&gt; boundary — slash or end of string — was appended right after each token:&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="c1"&gt;// middleware.ts (after the fix, sha 787653311)&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;config&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;matcher&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="s2"&gt;"/((?!login(?:/|$)|s/|student(?:/|$)|signage/|guide(?:/|$)|api/auth(?:/|$)|api/health(?:/|$)|api/guide/|_next/static|_next/image|favicon.ico|.*\\.(?:png|jpg|jpeg|gif|svg|ico|webp|css|js|map|woff|woff2|ttf)$).*)"&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;&lt;code&gt;s/&lt;/code&gt;, &lt;code&gt;signage/&lt;/code&gt;, and &lt;code&gt;api/guide/&lt;/code&gt; already ended in &lt;code&gt;/&lt;/code&gt;, so they were already boundaried and were left unchanged. The regression tests cover both directions: a token followed by unrelated characters now stays gated (protected), and the legitimate excluded paths — both bare and with a sub-path — stay excluded exactly as before.&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="c1"&gt;// middleware.test.ts (added, sha 787653311)&lt;/span&gt;
&lt;span class="nf"&gt;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"boundary hardening (#139 L3): tokens that were prefix matches no longer over-exclude look-alike protected routes"&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;gated&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/loginland"&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="nf"&gt;toBe&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="p"&gt;;&lt;/span&gt;
  &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;gated&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/students"&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="nf"&gt;toBe&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="p"&gt;;&lt;/span&gt;
  &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;gated&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/student-records"&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="nf"&gt;toBe&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="p"&gt;;&lt;/span&gt;
  &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;gated&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/api/authority"&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="nf"&gt;toBe&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="p"&gt;;&lt;/span&gt;
  &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;gated&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/api/auth-admin"&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="nf"&gt;toBe&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="p"&gt;;&lt;/span&gt;
  &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;gated&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/api/healthcheck"&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="nf"&gt;toBe&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="p"&gt;;&lt;/span&gt;
  &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;gated&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/api/health-internal"&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="nf"&gt;toBe&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="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="nf"&gt;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"existing auth-free paths stay excluded (bare and sub-path both)"&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;gated&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/login"&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="nf"&gt;toBe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;gated&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/student"&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="nf"&gt;toBe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;gated&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/api/auth/session"&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="nf"&gt;toBe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;gated&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/api/auth/signout"&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="nf"&gt;toBe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;gated&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/api/health"&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="nf"&gt;toBe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&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;h2&gt;
  
  
  Preventing recurrence
&lt;/h2&gt;

&lt;p&gt;This middleware only checks that a session cookie exists; it's a lightweight gate. The actual cookie verification and RLS context setup happen on the Server Component / Route Handler side, through &lt;code&gt;getCurrentUser()&lt;/code&gt; / &lt;code&gt;withSession()&lt;/code&gt;, as the final line of defense. So a matcher gap alone doesn't produce an immediate authorization bypass — but it still strips away one layer of defense-in-depth. The docstring now states the intent and the boundaried tokens explicitly, and the test suite carries cases in both directions — bare and sub-path routes stay excluded, look-alike protected routes don't — so if anyone adds a new prefix token without a boundary later, one of the two tests fails immediately.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://tech.rebounder.jp/en/posts/nextjs-middleware-matcher-negative-lookahead-prefix-boundary-bug/" rel="noopener noreferrer"&gt;tech.rebounder.jp&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>devops</category>
      <category>tooling</category>
      <category>productivity</category>
      <category>programming</category>
    </item>
    <item>
      <title>Missing frame-src in CSP Blocks YouTube, Kills Watch Logs</title>
      <dc:creator>cometa-kaito</dc:creator>
      <pubDate>Sat, 21 Nov 2026 00:01:44 +0000</pubDate>
      <link>https://dev.to/cometakaito/missing-frame-src-in-csp-blocks-youtube-kills-watch-logs-3gcn</link>
      <guid>https://dev.to/cometakaito/missing-frame-src-in-csp-blocks-youtube-kills-watch-logs-3gcn</guid>
      <description>&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Without a &lt;code&gt;frame-src&lt;/code&gt; directive in the CSP, &lt;code&gt;default-src 'self'&lt;/code&gt; blocks the iframe embed itself, so no YouTube video plays at all — even with &lt;code&gt;script-src&lt;/code&gt; allowing the YouTube domain.&lt;/strong&gt; And if &lt;code&gt;script-src&lt;/code&gt; doesn't allow that domain either, the IFrame Player API script can't load, so &lt;code&gt;window.YT&lt;/code&gt; — which the player depends on — never gets defined. That means playback isn't the only thing that breaks: the watch-time logging that monitors playback state and reports it also stops, silently. The fix was adding the exact domains YouTube actually uses to three separate directives: &lt;code&gt;script-src&lt;/code&gt;, &lt;code&gt;img-src&lt;/code&gt;, and &lt;code&gt;frame-src&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Symptom
&lt;/h2&gt;

&lt;p&gt;The CSP for a learning-video platform was defined in one place, &lt;code&gt;securityHeaders&lt;/code&gt; in &lt;code&gt;next.config.ts&lt;/code&gt;. Before the fix it looked like this:&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;securityHeaders&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
  &lt;span class="c1"&gt;// ...&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'Content-Security-Policy'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
      &lt;span class="s2"&gt;"default-src 'self'"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="s2"&gt;"script-src 'self' 'unsafe-inline' 'unsafe-eval'"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="s2"&gt;"style-src 'self' 'unsafe-inline'"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="sb"&gt;`connect-src 'self' https://*.supabase.co wss://*.supabase.co`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="s2"&gt;"img-src 'self' data:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="s2"&gt;"font-src 'self'"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="s2"&gt;"frame-ancestors 'none'"&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="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&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;There is no &lt;code&gt;frame-src&lt;/code&gt; directive at all. CSP falls back to &lt;code&gt;default-src&lt;/code&gt; for any directive that isn't specified, so an unset &lt;code&gt;frame-src&lt;/code&gt; is equivalent to &lt;code&gt;frame-src 'self'&lt;/code&gt;. Every &lt;code&gt;&amp;lt;iframe&amp;gt;&lt;/code&gt; embedding YouTube was blocked in production without exception — confirmed in production on 2026-08-16.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cause
&lt;/h2&gt;

&lt;p&gt;This CSP wasn't written with video embedding in mind from the start — not a single YouTube domain was listed anywhere. The impact wasn't limited to display. The &lt;code&gt;VideoPlayer&lt;/code&gt; component depends on the YouTube IFrame Player API, and it's built to wait for &lt;code&gt;window.YT&lt;/code&gt; to become available before constructing the player at all.&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;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;YT&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Player&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;attachPlayer&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;else&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;prev&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;onYouTubeIframeAPIReady&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;onYouTubeIframeAPIReady&lt;/span&gt; &lt;span class="o"&gt;=&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="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;prev&lt;/span&gt;&lt;span class="o"&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="nf"&gt;attachPlayer&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="o"&gt;!&lt;/span&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'yt-iframe-api'&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;s&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'script'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;     &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'yt-iframe-api'&lt;/span&gt;
    &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;src&lt;/span&gt;    &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'https://www.youtube.com/iframe_api'&lt;/span&gt;
    &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;appendChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&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;This dynamically inserts a script tag that loads &lt;code&gt;https://www.youtube.com/iframe_api&lt;/code&gt;, but the &lt;code&gt;script-src&lt;/code&gt; before the fix was just &lt;code&gt;'self' 'unsafe-inline' 'unsafe-eval'&lt;/code&gt; — no external domain allowed at all. So the &lt;code&gt;iframe_api&lt;/code&gt; script itself gets blocked as a CSP violation, and &lt;code&gt;window.YT&lt;/code&gt; never gets defined. Since &lt;code&gt;VideoPlayer&lt;/code&gt; only assembles the player once &lt;code&gt;window.YT&lt;/code&gt; exists, and only then starts measuring watch time and sending heartbeats, fixing &lt;code&gt;frame-src&lt;/code&gt; alone gets the video showing but leaves watch tracking still dead. Both directives have to be in place together before embedding and API-based watch tracking both work.&lt;/p&gt;

&lt;h2&gt;
  
  
  The fix
&lt;/h2&gt;

&lt;p&gt;The exact domains YouTube actually uses were added individually to three places: &lt;code&gt;script-src&lt;/code&gt;, &lt;code&gt;img-src&lt;/code&gt;, and &lt;code&gt;frame-src&lt;/code&gt;.&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="s2"&gt;"script-src 'self' 'unsafe-inline' 'unsafe-eval' https://www.youtube.com https://s.ytimg.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="s2"&gt;"style-src 'self' 'unsafe-inline'"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="sb"&gt;`connect-src 'self' https://*.supabase.co wss://*.supabase.co${devConnectSrc}`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="s2"&gt;"img-src 'self' data: https://i.ytimg.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="s2"&gt;"font-src 'self'"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="s2"&gt;"frame-src https://www.youtube.com https://www.youtube-nocookie.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="s2"&gt;"frame-ancestors 'none'"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Breaking that down:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;script-src&lt;/code&gt; gained &lt;code&gt;https://www.youtube.com&lt;/code&gt; (where the IFrame API itself is served from) and &lt;code&gt;https://s.ytimg.com&lt;/code&gt; (additional scripts the API loads internally)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;img-src&lt;/code&gt; gained &lt;code&gt;https://i.ytimg.com&lt;/code&gt; (where thumbnail images are served from)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;frame-src&lt;/code&gt; gained &lt;code&gt;https://www.youtube.com&lt;/code&gt; and &lt;code&gt;https://www.youtube-nocookie.com&lt;/code&gt; (the embed itself)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All of these are scoped to YouTube-specific domains — no wildcard relaxation like &lt;code&gt;unsafe-inline&lt;/code&gt; was added. A branch that allows connections to localhost only in development was also added to &lt;code&gt;connect-src&lt;/code&gt;, but it's disabled under &lt;code&gt;NODE_ENV === 'production'&lt;/code&gt;, so it has no effect on the production CSP.&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;devConnectSrc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
  &lt;span class="nb"&gt;process&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;NODE_ENV&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s1"&gt;'production'&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="s1"&gt;''&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;' http://127.0.0.1:* http://localhost:* ws://127.0.0.1:* ws://localhost:*'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Preventing a repeat
&lt;/h2&gt;

&lt;p&gt;The CSP lives in one place in &lt;code&gt;next.config.ts&lt;/code&gt;, and adding the directives themselves was straightforward. What delayed catching this is that a CSP violation isn't a build error and isn't a TypeScript type error — it only shows up when you actually play a video in a browser and check the console. Even when the embed fails, the &lt;code&gt;&amp;lt;iframe&amp;gt;&lt;/code&gt; element itself stays in the DOM, so a glance at the screen doesn't distinguish "the video isn't showing" from "it's blocked by CSP." Verifying the fix required the same real-browser check: zero CSP errors in the console, &lt;code&gt;window.YT&lt;/code&gt; resolving to an &lt;code&gt;object&lt;/code&gt;, and the player and thumbnail actually rendering — all three, confirmed on an actual device.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://tech.rebounder.jp/en/posts/haiia-csp-missing-frame-src-blocks-youtube-breaks-watch-tracking/" rel="noopener noreferrer"&gt;tech.rebounder.jp&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>css</category>
      <category>javascript</category>
      <category>frontend</category>
    </item>
    <item>
      <title>pdfjs-dist standard_fonts Missing in Next.js standalone</title>
      <dc:creator>cometa-kaito</dc:creator>
      <pubDate>Thu, 19 Nov 2026 23:38:02 +0000</pubDate>
      <link>https://dev.to/cometakaito/pdfjs-dist-standardfonts-missing-in-nextjs-standalone-3i3o</link>
      <guid>https://dev.to/cometakaito/pdfjs-dist-standardfonts-missing-in-nextjs-standalone-3i3o</guid>
      <description>&lt;h2&gt;
  
  
  The short version
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;pdfjs-dist's &lt;code&gt;standard_fonts/&lt;/code&gt; is read in a way Next.js file tracing cannot follow, so it can go missing from an &lt;code&gt;output: "standalone"&lt;/code&gt; build.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The effect of the omission is quiet. Missing fonts do not raise an exception; only text extraction from PDFs using the standard fonts (Helvetica and the rest) breaks. Making the inclusion explicit in configuration (&lt;code&gt;outputFileTracingIncludes&lt;/code&gt;) &lt;em&gt;and&lt;/em&gt; adding a separate fail-fast guard that notices at production startup if it goes missing anyway is what finally made this safe to operate.&lt;/p&gt;

&lt;h2&gt;
  
  
  What it looks like
&lt;/h2&gt;

&lt;p&gt;This repository's PDF extractor (&lt;code&gt;PdfExtractor&lt;/code&gt;) calls &lt;code&gt;getTextContent()&lt;/code&gt; per page with pdfjs-dist (the Node legacy build) to pull out text. Extracting PDFs that use the standard 14 fonts (Helvetica and so on) correctly requires passing the &lt;code&gt;standard_fonts/&lt;/code&gt; directory pdfjs-dist ships as &lt;code&gt;standardFontDataUrl&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;That directory is found without trouble in development and CI, where pdfjs-dist sits in &lt;code&gt;node_modules&lt;/code&gt; as-is. But in the &lt;code&gt;output: "standalone"&lt;/code&gt; runtime generated by &lt;code&gt;next build&lt;/code&gt; — the side that actually starts on Cloud Run — file tracing (NFT) statically analyses dependencies and narrows &lt;code&gt;.next/standalone&lt;/code&gt; to just the necessary files. &lt;code&gt;standard_fonts/&lt;/code&gt; could fall outside that narrowing.&lt;/p&gt;

&lt;p&gt;Even when it is missing, the extraction itself throws nothing. On pdfjs-dist v6, an unresolvable &lt;code&gt;standardFontDataUrl&lt;/code&gt; makes &lt;code&gt;getTextContent()&lt;/code&gt; fail with &lt;code&gt;UnknownErrorException&lt;/code&gt;, but that is easily swallowed by a try/catch on the calling side, leaving only the PDF's extracted text near-empty. (On v5 it stopped at a warning and extraction still succeeded, which made the behaviour change in v6 easy to miss.)&lt;/p&gt;

&lt;h2&gt;
  
  
  Why
&lt;/h2&gt;

&lt;p&gt;The path to &lt;code&gt;standard_fonts/&lt;/code&gt; is resolved at runtime through dynamic &lt;code&gt;file://&lt;/code&gt; access.&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="c1"&gt;// extractors.ts&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;standardFontAnchors&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="kt"&gt;string&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="p"&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;url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;pathToFileURL&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cwd&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="s2"&gt;"noop.cjs"&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;href&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;function&lt;/span&gt; &lt;span class="nf"&gt;standardFontsDirFrom&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;anchor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createRequire&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;anchor&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;pkgJsonPath&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;require&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"pdfjs-dist/package.json"&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;pkgRoot&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pkgJsonPath&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="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pkgJsonPath&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="s2"&gt;"package.json"&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="k"&gt;return&lt;/span&gt; &lt;span class="sb"&gt;`${pkgRoot}standard_fonts`&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;&lt;code&gt;standardFontAnchors()&lt;/code&gt; holds two candidate starting points because of how Next.js's bundler works. In development and vitest, &lt;code&gt;import.meta.url&lt;/code&gt; points at the source's real location and resolves directly, but &lt;strong&gt;in a production server bundled by Turbopack, &lt;code&gt;import.meta.url&lt;/code&gt; points at a virtual chunk location like &lt;code&gt;.next/server/chunks/...&lt;/code&gt;&lt;/strong&gt;, and a relative search from there cannot find pdfjs-dist. Hence the fallback that also tries a &lt;code&gt;createRequire&lt;/code&gt; anchored at &lt;code&gt;process.cwd()&lt;/code&gt; (the standalone runtime's root).&lt;/p&gt;

&lt;p&gt;The problem is that &lt;strong&gt;this path resolution via &lt;code&gt;createRequire(...).resolve()&lt;/code&gt; is invisible to Next.js's file tracing&lt;/strong&gt;. NFT decides what to include by analysing static import statements and &lt;code&gt;require()&lt;/code&gt; calls; it does not follow dynamic access to a string path assembled at runtime. As a result the &lt;code&gt;standard_fonts/&lt;/code&gt; directory itself can be missing from the &lt;code&gt;.next/standalone&lt;/code&gt; output, or present but empty.&lt;/p&gt;

&lt;p&gt;The awkward part is that the path resolution (&lt;code&gt;require.resolve("pdfjs-dist/package.json")&lt;/code&gt;) can succeed even while the files are missing. The &lt;code&gt;pdfjs-dist&lt;/code&gt; package itself is included for other reasons (the code's own &lt;code&gt;require&lt;/code&gt;), so &lt;code&gt;package.json&lt;/code&gt; is found. But if only the neighbouring &lt;code&gt;standard_fonts/&lt;/code&gt; directory falls outside the inclusion, the path assembles while nothing is there. So this code does not judge by whether the path resolves; it checks that the real files exist.&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="c1"&gt;// extractors.ts&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;hasStandardFontData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dir&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;existsSync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dir&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nf"&gt;readdirSync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dir&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;STANDARD_FONT_FILE_RE&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&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="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&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;h2&gt;
  
  
  Fixing it
&lt;/h2&gt;

&lt;p&gt;The response is two-stage. One is making the inclusion explicit in Next.js configuration; the other is being able to notice at production startup if it goes missing anyway.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;① State the &lt;code&gt;standard_fonts/&lt;/code&gt; inclusion explicitly to file tracing in &lt;code&gt;next.config.ts&lt;/code&gt;&lt;/strong&gt;&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="c1"&gt;// next.config.ts&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;nextConfig&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;NextConfig&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;output&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"standalone"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;serverExternalPackages&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"pdfjs-dist"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;outputFileTracingRoot&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;monorepoRoot&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;outputFileTracingIncludes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s2"&gt;"/api/**"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"./node_modules/**/pdfjs-dist/standard_fonts/**"&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;The keys of &lt;code&gt;outputFileTracingIncludes&lt;/code&gt; are route patterns, and the value globs the extra files that route uses. Here &lt;code&gt;./node_modules/**/pdfjs-dist/standard_fonts/**&lt;/code&gt; absorbs &lt;code&gt;pnpm&lt;/code&gt;'s nested directory structure (&lt;code&gt;.pnpm/pdfjs-dist@*/node_modules/pdfjs-dist/...&lt;/code&gt;) with a wildcard. The route key is the broad &lt;code&gt;/api/**&lt;/code&gt; rather than individual API paths because Next.js's dynamic route segments (square brackets like &lt;code&gt;[id]&lt;/code&gt;) are interpreted as glob character classes in the file-tracing world and do not match as intended.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;serverExternalPackages: ["pdfjs-dist"]&lt;/code&gt; is needed at the same time. Including pdfjs-dist in the Next.js bundle makes the &lt;code&gt;createRequire(import.meta.url).resolve(...)&lt;/code&gt; above try to resolve from a post-bundle chunk location and fail, and it also breaks the worker and font assets the legacy build depends on. Leaving it unbundled and letting it &lt;code&gt;require&lt;/code&gt; from the real filesystem's &lt;code&gt;node_modules&lt;/code&gt; at runtime keeps the same resolution path in both &lt;code&gt;next start&lt;/code&gt; and the standalone runtime.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;② Detect it fail-fast at production startup, should the inclusion setting go missing&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Not relying on the setting alone is because the inclusion path can be dropped again for some other reason, such as a Dockerfile rewrite. So &lt;code&gt;register()&lt;/code&gt; in &lt;code&gt;instrumentation.ts&lt;/code&gt;, called exactly once at production startup, invokes a guard that checks whether the font files resolve.&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="c1"&gt;// instrumentation.ts&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;register&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="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="o"&gt;&amp;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="nb"&gt;process&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;NEXT_RUNTIME&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="s2"&gt;"nodejs"&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="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;process&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;NODE_ENV&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="s2"&gt;"production"&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="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;assertStandardFontsAvailable&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"./lib/pdf-extract.js"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nf"&gt;assertStandardFontsAvailable&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;(In reality it imports from the internal package holding the extraction code. The point is that the dynamic import sits inside the nodejs guard, so a Node-only dependency graph is never pulled into the edge runtime.)&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="c1"&gt;// extractors.ts&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;assertStandardFontsAvailable&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;void&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="nf"&gt;resolveStandardFontDataUrl&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="kc"&gt;undefined&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="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;dir&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;locateStandardFontsDir&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="s2"&gt;"pdfjs-dist 未解決"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="sb"&gt;`pdfjs-dist standard_fonts のフォント実体を解決できません (${dir})。`&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
      &lt;span class="s2"&gt;"標準フォント PDF の text 抽出がサイレントに空になります。"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
      &lt;span class="s2"&gt;"Cloud Run standalone バンドルに standard_fonts/ が同梱されているか確認してください "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
      &lt;span class="s2"&gt;"(apps/web/next.config.ts の outputFileTracingIncludes)。Issue #311。"&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;The early return on &lt;code&gt;process.env.NODE_ENV !== "production"&lt;/code&gt; is because development and CI unit tests have the font files in &lt;code&gt;node_modules&lt;/code&gt; and would not normally throw; the guard is meant to catch the production-specific omission only. With this check in place, a missing inclusion changes from a hard-to-find degradation — "PDF text extraction quietly comes back empty" — into an easy-to-notice one: startup itself fails right after deploy.&lt;/p&gt;

&lt;h2&gt;
  
  
  Premises that are easy to miss
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;A path resolving and a file existing are different things.&lt;/strong&gt; &lt;code&gt;require.resolve()&lt;/code&gt; succeeding does not mean the adjacent directory was included. Assets that depend on dynamic &lt;code&gt;file://&lt;/code&gt; access are worth suspecting of a file-tracing miss, and checking down to whether the real files exist.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Development and CI unit tests cannot detect this kind of omission.&lt;/strong&gt; It does not reproduce in a runtime where the whole of &lt;code&gt;node_modules&lt;/code&gt; is present. The symptom only appears once a standalone build is actually run.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fixing the inclusion in configuration does not, by itself, prevent recurrence.&lt;/strong&gt; Another change, such as a Dockerfile rewrite, can drop the same inclusion path again. The setting and the startup fail-fast guard are each insufficient alone.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://tech.rebounder.jp/en/posts/pdfjs-dist-standard-fonts-nextjs-standalone-not-bundled/" rel="noopener noreferrer"&gt;tech.rebounder.jp&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>database</category>
      <category>postgres</category>
      <category>devops</category>
      <category>webdev</category>
    </item>
    <item>
      <title>supabase-js .range() Without .order() Drops Rows</title>
      <dc:creator>cometa-kaito</dc:creator>
      <pubDate>Wed, 18 Nov 2026 23:04:27 +0000</pubDate>
      <link>https://dev.to/cometakaito/supabase-js-range-without-order-drops-rows-5a6m</link>
      <guid>https://dev.to/cometakaito/supabase-js-range-without-order-drops-rows-5a6m</guid>
      <description>&lt;h2&gt;
  
  
  The short version
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Page with OFFSET/LIMIT using &lt;code&gt;.range()&lt;/code&gt; alone, without &lt;code&gt;.order()&lt;/code&gt; pinning the ordering, and rows can be duplicated or dropped at a page boundary.&lt;/strong&gt; The code that fetched a reference set paged exactly like that, and because the target column has no unique constraint to order by, a single missing reference could make an object read as "not referenced". The fix is to &lt;code&gt;.order()&lt;/code&gt; explicitly on the unique primary key &lt;code&gt;id&lt;/code&gt; before calling &lt;code&gt;.range()&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  The state it could become
&lt;/h2&gt;

&lt;p&gt;The subject is the orphan-object sweep for Supabase Storage. It works like this. Ad creatives are uploaded in two stages — "browser uploads straight to storage with a signed URL, then &lt;code&gt;finalizeCreative&lt;/code&gt; registers it in the DB" — and when the upload succeeds but registration never happens (the browser leaves, the connection fails), an object nobody references from &lt;code&gt;creatives.file_path&lt;/code&gt; is left in the bucket. The sweep collects those periodically, deleting only unreferenced objects older than &lt;code&gt;DEFAULT_ORPHAN_HOURS = 24&lt;/code&gt; (the default age threshold). The threshold's floor is &lt;code&gt;MIN_ORPHAN_HOURS = 6&lt;/code&gt;, and one run deletes at most &lt;code&gt;MAX_DELETE_PER_RUN = 500&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;partitionOrphans&lt;/code&gt;, which decides what gets deleted, is a pure function that walks the storage objects one by one, excludes anything where &lt;code&gt;referenced.has(o.path)&lt;/code&gt; is true as a legitimate creative, and otherwise compares &lt;code&gt;created_at&lt;/code&gt; against the threshold to sort it into &lt;code&gt;eligible&lt;/code&gt; (to delete) or &lt;code&gt;tooRecent&lt;/code&gt; (keep). That &lt;code&gt;referenced&lt;/code&gt; is a &lt;code&gt;Set&lt;/code&gt; built by &lt;code&gt;fetchReferencedPaths&lt;/code&gt;, which pages through every &lt;code&gt;file_path&lt;/code&gt; in the &lt;code&gt;creatives&lt;/code&gt; table, 1000 at a time (&lt;code&gt;FILE_PATH_PAGE&lt;/code&gt;) with &lt;code&gt;.range()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The pre-fix code had no &lt;code&gt;.order()&lt;/code&gt; in that fetch.&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="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;fetchReferencedPaths&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;admin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;SupabaseClient&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;errors&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&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="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;paths&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Set&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;string&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;ok&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;boolean&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="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;set&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Set&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;string&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;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;for&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="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;admin&lt;/span&gt;
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"creatives"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;select&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"file_path"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="ow"&gt;not&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"file_path"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"is"&lt;/span&gt;&lt;span class="p"&gt;,&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="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="no"&gt;FILE_PATH_PAGE&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="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;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;errors&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sb"&gt;`creatives.file_path: ${error.message}`&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="n"&gt;paths&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;set&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ok&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&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="c1"&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;&lt;code&gt;file_path&lt;/code&gt; has no unique constraint. Splitting pages with &lt;code&gt;.range()&lt;/code&gt; without specifying an order means a boundary row can appear on both pages (duplicated) or on neither (dropped). One gap in &lt;code&gt;referenced&lt;/code&gt; and the corresponding storage object is treated as "unreferenced" by &lt;code&gt;partitionOrphans&lt;/code&gt;, landing in &lt;code&gt;eligible&lt;/code&gt; for deletion if its &lt;code&gt;created_at&lt;/code&gt; is older than &lt;code&gt;DEFAULT_ORPHAN_HOURS&lt;/code&gt; (24 hours).&lt;/p&gt;

&lt;h2&gt;
  
  
  Why
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;.range()&lt;/code&gt; corresponds, through PostgREST, to PostgreSQL's &lt;code&gt;OFFSET&lt;/code&gt; / &lt;code&gt;LIMIT&lt;/code&gt;. The row order of a &lt;code&gt;SELECT&lt;/code&gt; with no &lt;code&gt;ORDER BY&lt;/code&gt; is the database's to choose, and there is no guarantee that the same query returns the same order when issued twice with different &lt;code&gt;OFFSET&lt;/code&gt;s. Paging — &lt;strong&gt;asking for the same set across several queries&lt;/strong&gt; — implicitly requires "the same order every time", and nothing satisfies that unless &lt;code&gt;.order()&lt;/code&gt; says so.&lt;/p&gt;

&lt;p&gt;This code was fail-closed: one page failing with a DB error returns &lt;code&gt;ok=false&lt;/code&gt;, and the sweep itself (&lt;code&gt;sweepCreativeOrphans&lt;/code&gt;) skips deleting anything. But what that mechanism can detect is &lt;strong&gt;an explicit error&lt;/strong&gt;. Rows quietly duplicating or dropping because the order is undefined returns no &lt;code&gt;error&lt;/code&gt;, and so falls outside that fail-closed guard.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fixing it
&lt;/h2&gt;

&lt;p&gt;We pin the unique primary key with &lt;code&gt;.order("id", { ascending: true })&lt;/code&gt; before calling &lt;code&gt;.range()&lt;/code&gt;.&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="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;admin&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"creatives"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;select&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"file_path"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="ow"&gt;not&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"file_path"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"is"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="c1"&gt;// Pin a total order on the unique primary key id before paging. OFFSET/LIMIT with no&lt;/span&gt;
  &lt;span class="c1"&gt;// ORDER BY can duplicate or drop rows at a page boundary (file_path is not unique, so it&lt;/span&gt;
  &lt;span class="c1"&gt;// cannot be the ordering key). One missing reference path means a live creative reads as&lt;/span&gt;
  &lt;span class="c1"&gt;// unreferenced and gets deleted — this is required.&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;order&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;ascending&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="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="no"&gt;FILE_PATH_PAGE&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="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;id&lt;/code&gt; is the primary key and unique, so as long as the same &lt;code&gt;.order("id")&lt;/code&gt; is specified, every page query returns a subset of the same total order. That removes any room for boundary rows to duplicate or drop, and &lt;code&gt;referenced&lt;/code&gt; is always a complete set.&lt;/p&gt;

&lt;h2&gt;
  
  
  Preventing a repeat
&lt;/h2&gt;

&lt;p&gt;The same commit added a no-op, &lt;code&gt;order: () =&amp;gt; chain&lt;/code&gt;, to the test's mock chain.&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;chain&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;select&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;chain&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="ow"&gt;not&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;chain&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="c1"&gt;// Production pins a total order with .order("id") before paging (prevents boundary drops).&lt;/span&gt;
  &lt;span class="c1"&gt;// In the mock it does not affect the slice order, so it is a no-op.&lt;/span&gt;
  &lt;span class="n"&gt;order&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;chain&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;range&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="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;number&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="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;slice&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;opts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;filePaths&lt;/span&gt; &lt;span class="o"&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="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&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="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fp&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="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;file_path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;fp&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="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&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="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;This mock does nothing when &lt;code&gt;.order()&lt;/code&gt; is called, and &lt;code&gt;range()&lt;/code&gt; simply slices the array it was given. So what the test confirms is only that "calling a method named &lt;code&gt;.order()&lt;/code&gt; does not break the chain" — it does not reproduce the duplication or the drop that an undefined order causes against a real database. If a later change to the same function removed &lt;code&gt;.order()&lt;/code&gt; again, this test would not catch it.&lt;/p&gt;

&lt;p&gt;The fix was found in implementation review and landed the same day. The commit message records no deletion actually happening in production, so it was closed before it could. The same function's docstring already stated that deletion must never run on an incomplete reference set, so the fail-closed intent was right from the start. What was missing was the premise that protects it: &lt;code&gt;.range()&lt;/code&gt; alone does not guarantee an order.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://tech.rebounder.jp/en/posts/postgrest-range-without-order-duplicate-drop-live-asset-deleted/" rel="noopener noreferrer"&gt;tech.rebounder.jp&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>database</category>
      <category>postgres</category>
      <category>devops</category>
      <category>webdev</category>
    </item>
    <item>
      <title>SecurityException Kills HDMI-CEC Power Control on Sideloads</title>
      <dc:creator>cometa-kaito</dc:creator>
      <pubDate>Tue, 17 Nov 2026 23:10:28 +0000</pubDate>
      <link>https://dev.to/cometakaito/securityexception-kills-hdmi-cec-power-control-on-sideloads-gcn</link>
      <guid>https://dev.to/cometakaito/securityexception-kills-hdmi-cec-power-control-on-sideloads-gcn</guid>
      <description>&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Android's &lt;code&gt;HdmiControlManager&lt;/code&gt; exposes HDMI-CEC control APIs (standby, wakeup, etc.) that require &lt;code&gt;signature|privileged&lt;/code&gt; permission, so calling them from a sideloaded app throws &lt;code&gt;SecurityException&lt;/code&gt; on most devices, making the call an effective no-op.&lt;/strong&gt; A fleet of signage devices (Google TVs locked down as Device Owner kiosks) wanted to turn the screen fully off at night via a genuine HDMI-CEC standby, but this constraint forced the real mechanism away from CEC and onto &lt;code&gt;DevicePolicyManager.lockNow()&lt;/code&gt; instead.&lt;/p&gt;

&lt;h2&gt;
  
  
  Symptom
&lt;/h2&gt;

&lt;p&gt;The signage devices are Google TVs mounted as hallway displays, locked into kiosk mode via Device Owner, with the screen meant to turn off at night and back on in the morning. The first approach considered was dropping the TV itself into a genuine HDMI-CEC standby. The comment in the resulting code spells out exactly why that approach doesn't work.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// CecHelper.kt
/**
 * Best-effort attempt to put the TV into real Standby / WakeUp via HDMI-CEC.
 *
 * Note:
 *  - HdmiControlManager's control APIs require signature|privileged permission,
 *    so an ordinary sideloaded app is normally rejected.
 *  - If it happens to work on some device, consider it a bonus.
 *  - Even on failure, the black-screen overlay still keeps the "looks off" appearance.
 *
 * Ideally this would call HdmiControlManager#setSystemAudioMode /
 * HdmiControlManager.OneTouchPlay (API 30+), but in most cases it just
 * throws SecurityException. For now this is absorbed via reflection + fallback.
 */
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The actual &lt;code&gt;tryStandby&lt;/code&gt; only logs whether the service exists — it never issues a single control command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fun tryStandby(context: Context) {
    runCatching {
        val svc = context.getSystemService("hdmi_control")
        if (svc != null) {
            Log.i(TAG, "hdmi_control service available: ${svc.javaClass.name}")
            // Depends on the vendor implementation. The public API has no
            // direct way to issue control commands.
        } else {
            Log.i(TAG, "hdmi_control service not available")
        }
    }.onFailure {
        Log.d(TAG, "tryStandby skipped: ${it.message}")
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;getSystemService("hdmi_control")&lt;/code&gt; itself does not return null — it does return a service object. The problem only shows up one step further, when actually issuing a "go to standby" or "wake up" control command.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cause
&lt;/h2&gt;

&lt;p&gt;Android's &lt;code&gt;HdmiControlManager&lt;/code&gt; exposes a public API for controlling a TV's power and audio routing over HDMI-CEC, but its control methods require a permission with &lt;code&gt;signature&lt;/code&gt; or &lt;code&gt;privileged&lt;/code&gt; protection level. That permission is only granted to apps signed with the system certificate or placed in the vendor's privileged partition — there is no way for a third-party sideloaded app to satisfy it.&lt;/p&gt;

&lt;p&gt;What's easy to conflate here is that the signage device is already a &lt;strong&gt;Device Owner&lt;/strong&gt; under &lt;code&gt;DevicePolicyManager&lt;/code&gt;. Device Owner is a strong "manage the whole device" privilege, but it belongs to the &lt;code&gt;dpm&lt;/code&gt; (device management) permission system, a different layer from the &lt;code&gt;signature|privileged&lt;/code&gt; permission that &lt;code&gt;HdmiControlManager&lt;/code&gt; requires. Becoming Device Owner does nothing to satisfy the signing and placement conditions the CEC control APIs demand. The comment's "consider it a bonus if it works on some device" is a hedge for the rare case where an OEM has loosened its CEC handler's permission check; the design assumption is still "rejected by default."&lt;/p&gt;

&lt;h2&gt;
  
  
  The fix
&lt;/h2&gt;

&lt;p&gt;Once CEC was ruled out as the real mechanism, turning the screen off at night was moved to the alternative that &lt;code&gt;PowerController&lt;/code&gt;'s design comment spells out. Letting the device fall into deep sleep stops polling and makes remote wake unreachable with no way to recover, so the device itself stays awake 24 hours a day, and only the &lt;strong&gt;screen (backlight)&lt;/strong&gt; is turned off, via Device Owner's &lt;code&gt;lockNow()&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// PowerController.kt
/**
 * Design decision (2026-06-08):
 *  - Letting the device fall into *deep sleep* stops polling and makes remote
 *    wake unreachable, leaving it **unrecoverable** (confirmed with the user).
 *    So the device stays awake 24h, and only the **screen (backlight)** turns off.
 *  - The real mechanism for screen-off is DevicePolicyManager.lockNow()
 *    (available as Device Owner / an active device admin). This triggers a
 *    screen lock, i.e. backlight off, while the CPU/services/polling stay alive.
 *  - HDMI-CEC (CecHelper) needs privileged permission and is almost always a
 *    no-op, so it's kept only as a *secondary* attempt alongside lockNow.
 *  - BlackScreenActivity (the black overlay) is a purely *visual* fallback for
 *    devices where lockNow doesn't take effect.
 */
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the implementation, &lt;code&gt;lockNow()&lt;/code&gt; is called as the primary mechanism, with CEC kept in the same call as a secondary attempt that's "a bonus if it works."&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fun screenOff(context: Context) {
    val locked = runCatching {
        if (canLock(context)) {
            dpm(context)?.lockNow()
            Log.i(TAG, "screenOff: lockNow() issued")
            true
        } else {
            Log.w(TAG, "screenOff: not device owner / admin -&amp;gt; lockNow skipped")
            false
        }
    }.onFailure { Log.w(TAG, "screenOff: lockNow failed: ${it.message}") }
        .getOrDefault(false)

    // Secondary: HDMI-CEC standby (a no-op on most devices, but a bonus if it works)
    runCatching { CecHelper.tryStandby(context) }

    if (!locked) {
        Log.i(TAG, "screenOff: relying on BlackScreenActivity overlay fallback")
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In case &lt;code&gt;lockNow()&lt;/code&gt; fails (the device is neither Device Owner nor an active admin), the &lt;code&gt;BlackScreenActivity&lt;/code&gt; black-screen overlay is kept as a purely visual fallback. With this three-layer fallback (lockNow → CEC assist → black overlay), every device ends up looking "off, by some means" without exception.&lt;/p&gt;

&lt;h2&gt;
  
  
  Preventing a repeat
&lt;/h2&gt;

&lt;p&gt;There is no automated test for this code. The actual safeguard against a repeat is the design-decision comment in &lt;code&gt;PowerController.kt&lt;/code&gt; (2026-06-08) and the warning note inside &lt;code&gt;CecHelper&lt;/code&gt; themselves. By leaving the conclusion — "CEC needs signature|privileged permission and is almost always a no-op; the real mechanism is lockNow" — right at the top of the code as a comment, it prevents a future reader (including the original author) from redoing the same investigation and adding back a CEC-based control implementation. Every call being wrapped in &lt;code&gt;runCatching&lt;/code&gt; is also a deliberate design choice, letting execution fall through to the next fallback on devices that aren't Device Owner or aren't supported, instead of crashing — the implementation assumes it is running in an environment that may lack permission.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://tech.rebounder.jp/en/posts/android-hdmi-cec-standby-requires-signature-permission-noop/" rel="noopener noreferrer"&gt;tech.rebounder.jp&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>android</category>
      <category>iot</category>
      <category>kotlin</category>
      <category>programming</category>
    </item>
    <item>
      <title>FLAG_KEEP_SCREEN_ON Not Working on Android: The Screensaver</title>
      <dc:creator>cometa-kaito</dc:creator>
      <pubDate>Mon, 16 Nov 2026 23:11:25 +0000</pubDate>
      <link>https://dev.to/cometakaito/flagkeepscreenon-not-working-on-android-the-screensaver-4b5d</link>
      <guid>https://dev.to/cometakaito/flagkeepscreenon-not-working-on-android-the-screensaver-4b5d</guid>
      <description>&lt;h2&gt;
  
  
  The answer
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;It's the screensaver (Daydream).&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;FLAG_KEEP_SCREEN_ON&lt;/code&gt; and disabling sleep run on a separate track from the screensaver. &lt;strong&gt;It fires in the instant the flag isn't in effect&lt;/strong&gt;, so it's the one thing left standing after you've blocked everything else.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;adb shell settings put secure screensaver_enabled 0
adb shell settings put secure screensaver_activate_on_sleep 0
adb shell settings put secure screensaver_activate_on_dock 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And &lt;strong&gt;these settings reset on reboot&lt;/strong&gt;. Stop before that step and you'll think it's fixed when it isn't.&lt;/p&gt;

&lt;h2&gt;
  
  
  The symptom
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;A display meant to stay on goes black after a while&lt;/li&gt;
&lt;li&gt;The device itself is alive — processing keeps running&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;FLAG_KEEP_SCREEN_ON&lt;/code&gt; is already set&lt;/li&gt;
&lt;li&gt;Sleep timeout is already extended&lt;/li&gt;
&lt;li&gt;It still goes dark, and &lt;strong&gt;you can't predict when&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The unpredictability is the tell. A timeout would produce a consistent interval, but the screensaver slips in during "the instant the flag isn't in effect," so there's no fixed reproduction condition to chase.&lt;/p&gt;

&lt;h2&gt;
  
  
  More than one path to black
&lt;/h2&gt;

&lt;p&gt;This is the annoying part: &lt;strong&gt;there are multiple paths to the screen turning off.&lt;/strong&gt; Blocking one leaves the others open.&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;# Disable HDMI-CEC power linkage (gets dragged down by another device's power state)&lt;/span&gt;
adb shell settings put global hdmi_control_auto_device_off_enabled 0

&lt;span class="c"&gt;# Disable auto power-off on no signal&lt;/span&gt;
adb shell settings put global no_signal_auto_power_off 0

&lt;span class="c"&gt;# Screen-off timeout&lt;/span&gt;
adb shell settings put system screen_off_timeout 2147483647

&lt;span class="c"&gt;# Sleep&lt;/span&gt;
adb shell settings put secure sleep_timeout -1

&lt;span class="c"&gt;# ★ Screensaver — the most likely culprit&lt;/span&gt;
adb shell settings put secure screensaver_enabled 0
adb shell settings put secure screensaver_activate_on_sleep 0
adb shell settings put secure screensaver_activate_on_dock 0

&lt;span class="c"&gt;# Stay awake while powered&lt;/span&gt;
adb shell settings put global stay_on_while_plugged_in 7
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It's easy to fix &lt;code&gt;screen_off_timeout&lt;/code&gt; and &lt;code&gt;sleep_timeout&lt;/code&gt; and assume you're done, but &lt;strong&gt;the screensaver is a setting distinct from both of those.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  A layer adb can't reach
&lt;/h2&gt;

&lt;p&gt;If the device is a TV used as the display, there's one more layer. &lt;strong&gt;The set's own off-timer sometimes reads a firmware-level setting of its own, and ignores writes from adb.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We hit this on one model where the power kept cutting out at 16:30. &lt;code&gt;settings list global&lt;/code&gt; showed an item like &lt;code&gt;tv_timer_power_off_timer_values&lt;/code&gt;, but writing to it did nothing — the firmware kept preferring its own setting.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;This layer can only be cleared from the device's own settings screen&lt;/strong&gt; — usually somewhere under timer, power, or energy saving. Assuming adb covers everything stops you cold right here.&lt;/p&gt;

&lt;h2&gt;
  
  
  Reboot resets all of it
&lt;/h2&gt;

&lt;p&gt;This is the part that's easiest to miss.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Values written with &lt;code&gt;settings put&lt;/code&gt; disappear on reboot.&lt;/strong&gt; A command you ran by hand is an "effective right now" fix, not a permanent one. If the device restarts overnight, the next morning you're back where you started.&lt;/p&gt;

&lt;p&gt;To make it permanent, have &lt;strong&gt;the app itself reapply the settings on every launch.&lt;/strong&gt; That requires granting a permission once, up front.&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;# Write access to Settings.Secure / Settings.Global&lt;/span&gt;
adb shell pm grant &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;package-name&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; android.permission.WRITE_SECURE_SETTINGS

&lt;span class="c"&gt;# Settings.System (screen_off_timeout) goes through appops instead&lt;/span&gt;
adb shell appops &lt;span class="nb"&gt;set&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;package-name&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; WRITE_SETTINGS allow
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;WRITE_SECURE_SETTINGS&lt;/code&gt; is a &lt;code&gt;signature|privileged|development&lt;/code&gt; permission, but &lt;strong&gt;&lt;code&gt;pm grant&lt;/code&gt; can still assign it to a regular app.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Confirming it actually took
&lt;/h3&gt;

&lt;p&gt;Without the permission, the app &lt;strong&gt;catches the exception and does nothing.&lt;/strong&gt; It doesn't crash, so a missed grant is easy to overlook.&lt;/p&gt;

&lt;p&gt;Check the actual writes in logcat.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;adb logcat -s &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;tag&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you see the &lt;strong&gt;value it actually wrote&lt;/strong&gt; — something like &lt;code&gt;sleep_timeout=-1&lt;/code&gt; or &lt;code&gt;screen_off_timeout=MAX&lt;/code&gt; — it worked. A line reading &lt;code&gt;skip:&lt;/code&gt; means a &lt;code&gt;SecurityException&lt;/code&gt;, i.e. a no-op with no permission granted.&lt;/p&gt;

&lt;p&gt;The key point here: &lt;strong&gt;don't treat "no error appeared" as proof of success.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Four layers, in summary
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Layer&lt;/th&gt;
&lt;th&gt;How to clear it&lt;/th&gt;
&lt;th&gt;Survives reboot?&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Screensaver&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;screensaver_*&lt;/code&gt; → 0&lt;/td&gt;
&lt;td&gt;❌ resets&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Sleep / screen-off&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;sleep_timeout&lt;/code&gt; / &lt;code&gt;screen_off_timeout&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;❌ resets&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;CEC / no-signal off&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;hdmi_control_*&lt;/code&gt; / &lt;code&gt;no_signal_*&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;❌ resets&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Device's own off-timer&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;From the device settings screen&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;✅ persists&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The top three layers reset on reboot, so you need something on the app side to reapply them. Only the bottom layer is out of adb's reach — but clear it once from the device UI and it stays cleared.&lt;/p&gt;

&lt;p&gt;What happened after running the same kind of device long-term is written up in &lt;a href="https://tech.rebounder.jp/en/posts/googletv-kiosk-ops/" rel="noopener noreferrer"&gt;Running a Google TV as a Device-Owner kiosk for several months&lt;/a&gt;.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://tech.rebounder.jp/en/posts/keep-screen-on-daydream/" rel="noopener noreferrer"&gt;tech.rebounder.jp&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>android</category>
      <category>iot</category>
      <category>kotlin</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
