<?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: Adam Gaskins</title>
    <description>The latest articles on DEV Community by Adam Gaskins (@adamgaskins).</description>
    <link>https://dev.to/adamgaskins</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2843313%2Fc992dac0-658b-415c-9cca-46b83aeed3cf.jpeg</url>
      <title>DEV Community: Adam Gaskins</title>
      <link>https://dev.to/adamgaskins</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/adamgaskins"/>
    <language>en</language>
    <item>
      <title>Laravel Vite - Upgrading Dependencies Leads to CORS Error</title>
      <dc:creator>Adam Gaskins</dc:creator>
      <pubDate>Mon, 10 Feb 2025 17:15:18 +0000</pubDate>
      <link>https://dev.to/adamgaskins/laravel-vite-upgrading-dependencies-leads-to-cors-error-id2</link>
      <guid>https://dev.to/adamgaskins/laravel-vite-upgrading-dependencies-leads-to-cors-error-id2</guid>
      <description>&lt;p&gt;I'm in the middle of a major site upgrade at work. Bootstrap 4 to 5, Vue 2 to 3, the whole shebang. We've been using Laravel Herd, which has been great so far. But after running &lt;code&gt;npm audit fix --force&lt;/code&gt;,  none of the Javascript assets would load on my dev site. My console was full of CORS errors:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Access to script at '&lt;a href="https://mysite.test:5173/@vite/client" rel="noopener noreferrer"&gt;https://mysite.test:5173/@vite/client&lt;/a&gt;' from origin '&lt;a href="https://mysite.test" rel="noopener noreferrer"&gt;https://mysite.test&lt;/a&gt;' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;When I ran &lt;code&gt;npm run build&lt;/code&gt; the site would load fine, only &lt;code&gt;npm run dev&lt;/code&gt; was causing the CORS errors.&lt;/p&gt;

&lt;p&gt;I dug a little bit into the packages that had been updated, and came across &lt;a href="https://github.com/vitejs/vite/blob/main/packages/vite/CHANGELOG.md#609-2025-01-20" rel="noopener noreferrer"&gt;this changelog&lt;/a&gt; in the Vite package:&lt;/p&gt;

&lt;blockquote&gt;
&lt;ul&gt;
&lt;li&gt;fix!: default server.cors: false to disallow fetching from untrusted origins &lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;The default for &lt;a href="https://vite.dev/config/server-options#server-cors" rel="noopener noreferrer"&gt;the &lt;code&gt;cors&lt;/code&gt; option&lt;/a&gt; was changed to &lt;code&gt;false&lt;/code&gt;. Since Herd serves the site from port &lt;code&gt;80&lt;/code&gt;, but serves the assets through vite at, in my case, port &lt;code&gt;5173&lt;/code&gt;, without the CORs header, this is now being blocked.&lt;/p&gt;

&lt;p&gt;I solved this by adding the following to my &lt;code&gt;vite.config.js&lt;/code&gt;. The snippet is modified from the link above to add the &lt;code&gt;.test&lt;/code&gt; domain I'm using:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nf"&gt;defineConfig&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;server&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;cors&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="na"&gt;origin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sr"&gt;/^https&lt;/span&gt;&lt;span class="se"&gt;?&lt;/span&gt;&lt;span class="sr"&gt;:&lt;/span&gt;&lt;span class="se"&gt;\/\/(?:(?:[^&lt;/span&gt;&lt;span class="sr"&gt;:&lt;/span&gt;&lt;span class="se"&gt;]&lt;/span&gt;&lt;span class="sr"&gt;+&lt;/span&gt;&lt;span class="se"&gt;\.)?&lt;/span&gt;&lt;span class="sr"&gt;localhost|mysite&lt;/span&gt;&lt;span class="se"&gt;\.&lt;/span&gt;&lt;span class="sr"&gt;test|127&lt;/span&gt;&lt;span class="se"&gt;\.&lt;/span&gt;&lt;span class="sr"&gt;0&lt;/span&gt;&lt;span class="se"&gt;\.&lt;/span&gt;&lt;span class="sr"&gt;0&lt;/span&gt;&lt;span class="se"&gt;\.&lt;/span&gt;&lt;span class="sr"&gt;1|&lt;/span&gt;&lt;span class="se"&gt;\[&lt;/span&gt;&lt;span class="sr"&gt;::1&lt;/span&gt;&lt;span class="se"&gt;\])(?:&lt;/span&gt;&lt;span class="sr"&gt;:&lt;/span&gt;&lt;span class="se"&gt;\d&lt;/span&gt;&lt;span class="sr"&gt;+&lt;/span&gt;&lt;span class="se"&gt;)?&lt;/span&gt;&lt;span class="sr"&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="cm"&gt;/* ... */&lt;/span&gt;

&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hopefully this can prevent someone else from wasting the same time I did!&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
