Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
KevinBerthier opened this issue Sep 25, 2023 · 3 comments
Closed
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@KevinBerthier
Copy link

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 KevinBerthier added the bug: pending triage Something doesn't seem to be working, but hasn't been verified label Sep 25, 2023
@KevinBerthier
Copy link
Author

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 ElMassimo added bug Something isn't working and removed bug: pending triage Something doesn't seem to be working, but hasn't been verified labels Jun 7, 2024
@ElMassimo
Copy link
Owner

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 ElMassimo added the help wanted Extra attention is needed label Jun 7, 2024
@ElMassimo ElMassimo changed the title Some assets are randomly missing after deployment with Capistrano assets:clean incorrectly removes files created in the same build Jun 7, 2024
ElMassimo added a commit that referenced this issue Aug 12, 2024
Vite plugins can create additional files, and some of them are never
referenced in the manifest.

As a result, cleaning files can only be done *safely* by doing it
*before* building with Vite, otherwise it can potentially break an app.

Deployment setups without a CDN would actually benefit from keeping
previous builds. Otherwise, in SPA clients might request an asset or dynamic
import chunk that was removed from the server upon deployment, remaining in a
broken state until a full-page reload.

Container-based deployments don't need `clean`, and they are one of the
most common ways to deploy apps nowadays.

Taking all of this into account, I've decided to remove this task from
`vite_ruby`, preventing broken apps, and easing the maintenance burden
of something that I've never used and most users don't need.

Closes #438, #490, #404
@ElMassimo
Copy link
Owner

Closed in 824b4ef.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants