ElMassimo / vite_ruby

⚡️ Vite.js in Ruby, bringing joy to your JavaScript experience
https://vite-ruby.netlify.app/
MIT License
1.32k stars 122 forks source link

`assets:clean` incorrectly removes files created in the same build #404

Closed KevinBerthier closed 2 months ago

KevinBerthier commented 1 year ago

Description 📖

Some assets are missing on production after deployment. It happens totally randomly. A redeployment fixes the issue

Reproduction 🐞

deploy.rb

set :keep_releases, 5
set :use_sudo, false

set :log_level, :debug

set :linked_files, %w[config/database.yml config/application.yml config/secrets.yml public/.htaccess]
set :linked_dirs, %w[.bundle log storage public/system public/uploads downloads node_modules tmp/pids tmp/cache tmp/sockets tmp/dumps]

set :bundle_jobs, 2
set :keep_assets, 2
set :assets_prefix, 'vite'

vite.config.ts

import { resolve } from 'path'
import { defineConfig } from 'vite'
import VuePlugin from '@vitejs/plugin-vue'
import { visualizer } from "rollup-plugin-visualizer";
import ViteRails from 'vite-plugin-rails'
import legacy from '@vitejs/plugin-legacy'

export default defineConfig({
  plugins: [
    legacy({ renderLegacyChunks: false }), // reads .browserlistrc
    ViteRails({
      stimulus: false,
      fullReload: {
        additionalPaths: ['app/**/*', 'config/locales/**/*']
      },
    }), VuePlugin(), visualizer()
  ],
  resolve: {
    alias: {
      '@assets': resolve(__dirname, 'app/assets'),
      '@stylesheets': resolve(__dirname, 'app/assets/stylesheets'),
      '@images': resolve(__dirname, 'app/assets/images'),
      '@pdp': resolve(__dirname, 'app/javascript/apps/pdp'),
    },
  },

})
Vite Ruby Info ``` bin/vite present?: true vite_ruby: 3.3.4 vite_rails: 3.0.15 rails: 7.0.7.2 node: v20.5.1 npm: 9.8.0 yarn: 1.22.17 pnpm: ruby: ruby 3.1.3p185 (2022-11-24 revision 1a6b16756e) [arm64-darwin22] installed packages: graphiste@0.1.0 /Users/kevinberthier/code/KevinBerthier/graphiste ├─┬ @vitejs/plugin-legacy@3.0.2 │ └── vite@4.4.9 deduped ├─┬ @vitejs/plugin-vue@4.2.3 │ └── vite@4.4.9 deduped ├─┬ vite-plugin-rails@0.1.0 │ ├─┬ vite-plugin-environment@1.1.3 │ │ └── vite@4.4.9 deduped │ ├─┬ vite-plugin-full-reload@1.0.5 │ │ └── vite@4.4.9 deduped │ ├─┬ vite-plugin-ruby@3.1.3 │ │ └── vite@4.4.9 deduped │ └── vite@4.4.9 deduped ├─┬ vite-plugin-ruby@3.2.2 │ └── vite@4.4.9 deduped └── vite@4.4.9 ```
KevinBerthier commented 1 year ago

After further investigations, we found that assets:clean was flaky because the method #versions uses mtime to determine versions, but sometimes a deployment can spread on multiples mtimes/versions (3 in our case).

Our temporary solution was to increase keep_assets

Sprocket seems to clean assets the same manner but seems to be able to generate all assets with an identical mtime

ElMassimo commented 4 months ago

Hi Kevin! Thanks for reporting.

It seems like files in a Vite build have a different mtime, so the vite:clean task should be modified to cluster files within the time span that a build typically requires (modified 5 minutes apart should cover most cases).

PRs are welcome!

ElMassimo commented 2 months ago

Closed in 824b4ef.