One solution to this problem is to use a shared storage location for the built assets, such as an S3 bucket or a shared network drive. This way, both app servers can access the same assets without needing to rebuild them separately.
To implement this solution, you can modify your Vite configuration to output the built assets to the shared storage location instead of the local file system. For example, if you're using S3, you can use the s3-sync plugin to upload the assets to the bucket:
// vite.config.js
import { defineConfig } from 'vite'
import s3Sync from 'vite-plugin-s3-sync'
export default defineConfig({
plugins: [
s3Sync({
bucket: 'my-bucket',
key: process.env.AWS_ACCESS_KEY_ID,
secret: process.env.AWS_SECRET_ACCESS_KEY,
region: 'us-east-1',
dir: 'dist',
}),
],
})
Then, in your Laravel app, you can reference the assets using their S3 URLs instead of local file paths. For example:
<link rel="stylesheet" href="https://my-bucket.s3.amazonaws.com/dist/app.css">
This way, both app servers will be able to access the same assets regardless of which server built them.